只能加入可迭代的

只能加入可迭代的

本文介绍了“只能加入可迭代的" python错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经看过有关可迭代的python错误的帖子:

I've already looked at this post about iterable python errors:

只能重复". Python错误

但这与错误无法分配可迭代"有关.我的问题是为什么python告诉我:

But that was about the error "cannot assign an iterable". My question is why is python telling me:

 "list.py", line 6, in <module>
    reversedlist = ' '.join(toberlist1)
TypeError: can only join an iterable

我不知道我在做什么错!我正在关注此线程:

I don't know what I am doing wrong! I was following this thread:

字符串的反向单词顺序与不允许使用str.split()

特别是这个答案:

>>> 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'

和适配器代码以使其具有输入.但是当我运行它时,它说了上面的错误.我是一个新手,所以请您告诉我错误消息的含义以及如何解决.

and adapter the code to make it have an input. But when I ran it, it said the error above. I am a complete novice so could you please tell me what the error message means and how to fix it.

下面的代码:

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)

程序应接受苹果和梨之类的输入,然后产生输出梨和苹果.

The program should take an input like apples and pears and then produce an output pears and apples.

我们将不胜感激!

推荐答案

splitlist1.reverse()就地执行操作,因此返回None.因此tobereversedlist1因此为None,因此是错误.

splitlist1.reverse(), like many list methods, acts in-place, and therefore returns None. So tobereversedlist1 is therefore None, hence the error.

您应直接通过splitlist1:

splitlist1.reverse()
reversedlist = ' '.join(splitlist1)

这篇关于“只能加入可迭代的" python错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-28 12:11