&引用;只能加入一个iterable“;python错误
我已经看过这篇关于iterable python错误的文章: 但这是一个错误“无法分配iterable”。我的问题是为什么python会告诉我:&引用;只能加入一个iterable“;python错误,python,python-3.x,iterable,Python,Python 3.x,Iterable,我已经看过这篇关于iterable python错误的文章: 但这是一个错误“无法分配iterable”。我的问题是为什么python会告诉我: "list.py", line 6, in <module> reversedlist = ' '.join(toberlist1) TypeError: can only join an iterable 并适配器代码以使其具有输入。但当我运行它时,它显示了上面的错误。我是一个完全的新手,所以请你告诉我错误消息的意思和如何修
"list.py", line 6, in <module>
reversedlist = ' '.join(toberlist1)
TypeError: can only join an iterable
并适配器代码以使其具有输入。但当我运行它时,它显示了上面的错误。我是一个完全的新手,所以请你告诉我错误消息的意思和如何修复它
代码如下:
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)
该程序应该接受像苹果和梨这样的输入,然后生成梨和苹果这样的输出
谢谢你的帮助 splitlist1.reverse()
,与许多列表方法一样,执行适当的操作,因此返回None
。因此,ToberReversedList1
为无,因此为错误
您应该直接通过splitlist1
:
splitlist1.reverse()
reversedlist = ' '.join(splitlist1)
字符串联接必须满足要迭代的连接对象(列表、元组)
splitlist1.reverse()返回None,None对象不支持迭代。如何使包含逗号的程序反向列表?像苹果一样,梨也会变成梨,用逗号代替空格进行分割。你如何同时进行空格分割和逗号分割?这样你就可以输入apples、pear和apples-pears,而不必考虑代码。有几种方法,但最简单的可能是先用逗号替换空格,然后用逗号拆分:
splitlist1=list1.replace(“”,’).split(‘,’)
splitlist1.reverse()
reversedlist = ' '.join(splitlist1)