这里要放置太多代码,所以我只显示问题出在哪里:
date = [day,month,year,time]
entrylist = [name,guess,date,email,phone]
entry = ''.join(entrylist)
print(entry)
答案 0 :(得分:1)
使用''.join(list)可以正常工作。
>>> entrylist = ['name','guess','date','email','phone']
>>> entry = ''.join(entrylist)
>>> print(entry)
nameguessdateemailphone
>>> entry = ' '.join(entrylist)
>>> print(entry)
name guess date email phone
>>>
如果列表列表需要加入,请使用以下格式
>>> a = [[1, 2, "sekar"],[3, "hello", "stack"],["overflow" ,4, "hi"]]
>>> ''.join(str(r) for v in a for r in v)
'12sekar3hellostackoverflow4hi'
>>> ' '.join(str(r) for v in a for r in v)
'1 2 sekar 3 hello stack overflow 4 hi'
>>>
如果您想将列表列表与变量结合起来,请参见下文
>>> a = ['stack']
>>> b = ['over']
>>> c = ['flow']
>>> finallist = a + b + c
>>> ''.join(finallist)
'stackoverflow'
如果列表中包含数字值,则必须先将其转换为字符串,然后再进行连接,否则将抛出异常,如下所示。
>>> a = [1, 2, "sekar"]
>>> b = [3, "hello", "stack"]
>>> c = ["overflow" ,4, "hi"]
>>> finallist = a + b + c
>>> " ".join(x for x in finallist)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: sequence item 0: expected str instance, int found
>>> " ".join(str(x) for x in finallist)
'1 2 sekar 3 hello stack overflow 4 hi'
>>>