Der*_*rek 11 python iterable python-3.x
我已经看过这篇关于可迭代python错误的帖子了:
但这是关于"无法分配可迭代"的错误.我的问题是为什么python告诉我:
"list.py", line 6, in <module>
reversedlist = ' '.join(toberlist1)
TypeError: can only join an iterable
Run Code Online (Sandbox Code Playgroud)
我不知道我做错了什么!我正在关注这个帖子:
特别是这个答案:
>>> 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'
Run Code Online (Sandbox Code Playgroud)
并适配代码使其具有输入.但当我跑它时,它说上面的错误.我是一个完整的新手所以,请你告诉我错误信息的含义以及如何解决它.
代码如下:
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)
Run Code Online (Sandbox Code Playgroud)
该程序应该像苹果和梨一样输入,然后产生输出梨和苹果.
帮助将不胜感激!
Dan*_*man 13
splitlist1.reverse()
和许多列表方法一样,就地行动,因此返回None
.因此tobereversedlist1
是无,因此错误.
你应该splitlist1
直接通过:
splitlist1.reverse()
reversedlist = ' '.join(splitlist1)
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
34844 次 |
最近记录: |