[英]Error: "TypeError: list indices must be integers, not str" in Python
I am trying to import the data from another file and using that in a function but getting error:我试图从另一个文件导入数据并在函数中使用它,但出现错误:
Door_position_pattern_actual = Door_Position[d] Door_position_pattern_actual = Door_Position[d]
TypeError: list indices must be integers, not str类型错误:列表索引必须是整数,而不是 str
Code References:代码参考:
reactive_sampling_period_in_seconds = 10 * 60
Door_Position = list()
while 1:
line = f.readline()
vals = f.readline()
vals= vals.rstrip()
data1 = []
for v in vals.split(","):
data1.append(v.lstrip())
if(entry[1]=="DoorPosition"):
Door_Position = list(data1)
def door_positin_fnc_actual(self, time_in_seconds):
time_period_in_10mins = 6
index = int(math.floor(time_in_seconds%(144*reactive_sampling_period_in_seconds)/(reactive_sampling_period_in_seconds)))
for d in Door_Position :
Door_position_pattern_actual = Door_Position[d]
return Door_position_pattern_actual[index]
Output References:输出参考:
print(Door_Position)
['(1', '1', '1', '0', '1', '0', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '0', '1', '1', '1)']
I am suspecting (1 is creating prob but not able to fix it, Kindly suggest me ...!!!我怀疑(1 正在创建问题但无法修复它,请建议我......!!!
This is all wrong:这都是错误的:
for d in Door_Position : Door_position_pattern_actual = Door_Position[d]
d
is not an index, it's a value. d
不是索引,它是一个值。 So it doesn't make sense to use it in Door_Position[ ... ]
所以在Door_Position[ ... ]
使用它没有意义
The error TypeError: list indices must be integers, not str
most probably comes from here.错误类型错误TypeError: list indices must be integers, not str
很可能来自这里。
Perhaps you were looking for something like this:也许你正在寻找这样的东西:
for d in range(len(Door_Position)) :
Door_position_pattern_actual = Door_Position[d]
First you have to get rid of (
and )
characters within your list elements like this:首先,您必须删除列表元素中的(
和)
字符,如下所示:
Door_Position = [item.replace('(', '').replace(')','') for item in Door_Position]
Your Door_Position
list should now look like this:您的Door_Position
列表现在应如下所示:
['1', '1', '1', '0', '1', '0', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '0', '1', '1', '1']
Now, Door_Position is a list of strings, for it to work try Door_position_pattern_actual = Door_Position[int(d)]
现在,Door_Position 是一个字符串列表,要使其工作,请尝试Door_position_pattern_actual = Door_Position[int(d)]
Take a look at this small sample:看看这个小样本:
Python 3.4.3 (v3.4.3:9b73f1c3e601, Feb 23 2015, 02:52:03)
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> a=[1,2,3]
>>> a[1]
2
>>> a['1']
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: list indices must be integers, not str
>>> a[int('1')]
2
See?看? I get the same error, as you, when I try to pass a string as a list index.当我尝试将字符串作为列表索引传递时,我遇到了与您相同的错误。
Hope this helps you understand.希望这可以帮助您理解。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.