gpt4 book ai didi

python - 在 python 中返回一个由 .split() 元素组成的列表

转载 作者:太空宇宙 更新时间:2023-11-04 08:13:04 24 4
gpt4 key购买 nike

在下面的代码中,getreport 是一个文本项,由/t/n 格式化。我正在尝试输出电话号码列表,但返回的列表是这样的:['5','5','5','7','8','7', .. .] 等,而不是像 ['5557877777'] 这样的东西。这里有什么问题?

def parsereport(getreport):
listoutput = []
lines = re.findall(r'.+?\n' , getreport) #everything is in perfect lines
for m in lines:
line = m
linesplit = line.split('\t') # Now I have a list of elements for each line
phone = linesplit[0] # first element is always the phone number ex '5557777878'
if is_number(linesplit[10]) == True:
num = int(linesplit[10])
if num > 0:
listoutput.extend(phone)

我尝试对 print(phone) 进行测试,它看起来很棒并返回“5557877777”等行,但返回列表 = ['5','5',etc] 并且数字被分开。

return listoutput

最佳答案

您将使用 listoutput.append() 函数代替 listoutput.extend()

>>> p='12345'
>>> l=[]
>>> l.extend(p)
>>> l
['1', '2', '3', '4', '5']
>>> ll = []
>>> ll.append(p)
>>> ll
['12345']

extend function:通过附加给定列表中的所有项目来扩展列表

关于python - 在 python 中返回一个由 .split() 元素组成的列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18909037/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com