如果我有一个像这样的[ [100], [500], [300] ]列表,用python从其中提取数字的最好方法是什么?
result = [ 100, 500, 300 ]发布于 2012-03-15 08:44:44
l=[[100], [500], [300]]
result=[item for sublist in l for item in sublist]来自wikibooks
def flatten(seq, list = None):
"""flatten(seq, list = None) -> list
Return a flat version of the iterator `seq` appended to `list`
"""
if list == None:
list = []
try: # Can `seq` be iterated over?
for item in seq: # If so then iterate over `seq`
flatten(item, list) # and make the same check on each item.
except TypeError: # If seq isn't iterable
list.append(seq) # append it to the new list.
return list谷歌是你的朋友..。
发布于 2012-03-15 08:44:51
x = [ [100] , [500] , [300] ]
y = [ i[0] for i in x]
#or
from itertools import chain
y = list(chain.from_iterable(x))https://stackoverflow.com/questions/9712488
复制相似问题