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

2024-04-20 13:10:01 发布

您现在位置:Python中文网/ 问答频道 /正文

在下面的代码中,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) 

我试着把打印(电话)测试,它看起来很棒,返回行'5557877777'等,但返回列表=['5','5',等]和数字被分开。你知道吗

return listoutput

Tags: 代码in文本number列表forifis
2条回答
>>> Numbers = ['1', '2', '3', '4', '5']
>>> NumbersJoined = []
>>> NumbersJoined.append(''.join(Numbers))
>>> print NumbersJoined
['12345']

您将使用listoutput.append()函数而不是listoutput.extend()

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

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

相关问题 更多 >