【发布时间】:2015-11-15 15:01:30
【问题描述】:
我已经看过这篇关于可迭代 python 错误的帖子:
"Can only iterable" Python error
但那是关于“无法分配可迭代”的错误。我的问题是为什么 python 告诉我:
"list.py", line 6, in <module>
reversedlist = ' '.join(toberlist1)
TypeError: can only join an iterable
我不知道我做错了什么!我在关注这个帖子:
Reverse word order of a string with no str.split() allowed
特别是这个答案:
>>> s = 'This is a string to try'
>>> r = s.split(' ')
['This', 'is', 'a', 'string', 'to', 'try']
>>> r.reverse()
>>> r
['try', 'to', 'string', 'a', 'is', 'This']
>>> result = ' '.join(r)
>>> result
'try to string a is This'
并调整代码以使其具有输入。但是当我运行它时,它说上面的错误。我是一个新手,请告诉我错误消息的含义以及如何解决它。
代码如下:
import re
list1 = input ("please enter the list you want to print")
print ("Your List: ", list1)
splitlist1 = list1.split(' ')
tobereversedlist1 = splitlist1.reverse()
reversedlist = ' '.join(tobereversedlist1)
yesno = input ("Press 1 for original list or 2 for reversed list")
yesnoraw = int(yesno)
if yesnoraw == 1:
print (list1)
else:
print (reversedlist)
程序应该接受像苹果和梨这样的输入,然后产生一个输出梨和苹果。
我们将不胜感激!
【问题讨论】:
标签: python python-3.x iterable