如何在python 3.5中将多个列表合并到一个列表中?

时间:2017-02-07 15:29:10

标签: python-3.x anaconda

我正在尝试将多个列表合并到一个列表中

e.g:['of', 'participants:', '25']['participants:', '25', 'we']['25', 'we', 'require']

代码后的

:“['of', 'participants:', '25'],['participants:', '25', 'we'],['25', 'we', 'require']

for wrd in d:
    child=child.lower()
    child=child.replace(' ','')
    dat =child
    r = re.compile(dat)
    s=str(wrd)
    test=[sentence in dat for sentence in s]
    if r.search(s) and any(test):
        print(s)

1 个答案:

答案 0 :(得分:0)

嗯,我不能完全确定你在做什么。使用正则表达式查看代码。也许最好使用字典。如果你必须使用一个列表,你可以通过循环将它合并到一个元组中。如果有帮助,请告诉我。

>>> list_one = ['of', 'participants:', '25']
>>> list_two = ['participants:', '25', 'we']
>>> list_three = ['25', 'we', 'require']
>>> thing = ()
>>> thing += tuple([list_one])
>>> thing += tuple([list_two])
>>> thing += tuple([list_three])
>>> thing
(['of', 'participants:', '25'], ['participants:', '25', 'we'], ['25', 'we', 'require'])
>>> for i in range(len(thing)):
...     print thing[i]
... 
['of', 'participants:', '25']
['participants:', '25', 'we']
['25', 'we', 'require']