-2

目前我的代码如下所示:

data = ""

pattern1 = re.compile('')
pattern2 = re.compile('')
pattern3 = re.compile('')

items = re.findall(pattern1, data 
mainlist = []
for item in items:
    forename = re.findall(pattern2, item)
    surname = re.findall(pattern3, item)
    mainlist.append(surname)

这种布局的唯一问题是我得到的列表如下:

[['Smith', 'Patricks', 'Clark'], ['Austin', 'Hamilton', 'Day', 'Sidders'], ['Bennet']]

我希望我的清单如下:

['Smith', 'Patricks', 'Clark', 'Austin', 'Hamilton', 'Day', 'Sidders', 'Bennet']

有任何想法吗?

在此先感谢-Hy

4

1 回答 1

3

使用extend

for item in items:
    forename = re.findall(pattern2, item)
    surname = re.findall(pattern3, item)
    mainlist.extend(surname)

myList.extend(L)添加 on 的各个L元素myList。它类似于:

for element in L:
    myList.append(element)
于 2013-07-30T14:39:07.277 回答