2012-03-15 121 views
1

如果我有一个像这样的列表[ [100], [500], [300] ],python中从数字中提取数字的最佳方法是什么?从字符串列表中提取数字 - python

result = [ 100, 500, 300 ] 
+0

哪里是 “字符串列表”? – 2012-03-15 00:45:16

回答

2

this question

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 

谷歌是你的朋友...

+0

好吧,我这样做: 结果= [我[1:len(i)-1]我在l] 谢谢:) – Vanddel 2012-03-15 00:54:52

+0

不客气! :-) – hochl 2012-03-15 10:12:24

3
x = [ [100] , [500] , [300] ] 
y = [ i[0] for i in x] 

#or 
from itertools import chain 
y = list(chain.from_iterable(x)) 
+0

或(相当于第一个)'进口经营者; y = map(operator.itemgetter(0),x)':-) – ephemient 2012-03-15 01:01:54