本文介绍了访问Python列表类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我写了一个包含str数据的代码
I write a code that has str data
def characters(self, content):
self.contentText = content.split()
# self.contentText is List here
我正在将self.contentText列表发送给另一个模块,如下所示:
I am sending self.contentText list to another module as:
self.contentText = Formatter.formatter(self.contentText)
在这种方法中,我正在编写以下代码:
In this method, I am writing below code:
remArticles = remArticles = {' a ':'', ' the ':'', ' and ':'', ' an ':'', '& nbsp;':''}
for i in remArticles:
print type(contentText)
print "1"
contentText = contentText.replace(i, remArticles[i])
print type(contentText)
我收到错误消息:
contentText = contentText.replace(i, remArticles[i])
AttributeError: 'list' object has no attribute 'replace'
如果我将其更改为:
for i in remArticles:
contentText = contentText.pop(i)
我遇到以下错误:
contentText = contentText.pop(i)#replace(i, remArticles[i])
AttributeError: 'str' object has no attribute 'pop'
推荐答案
contextText
确实是一个列表,因此没有replace
方法.第二个错误是由以下事实引起的:您用返回值contextText.pop(i)
覆盖了contextText
(返回弹出的值),因此在循环contextText
的第一次迭代之后不再是列表而是字符串
contextText
is indeed a list and as such has no replace
method. The second error is caused by the fact that you overwrite contextText
with the return value of contextText.pop(i)
(which returns the popped value), so that after the first iteration of the loop contextText
is no longer a list but a string.
这篇关于访问Python列表类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!