簡體   English   中英

從字符串列表中提取數字-python

[英]Extract numbers from a string list - python

如果我有一個類似[ [100], [500], [300] ] ,python中從中提取數字的最佳方法是什么?

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

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

這個問題

l=[[100], [500], [300]]
result=[item for sublist in l for item in sublist]

維基書

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

Google是您的朋友...

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM