Python3 TypeError:列表索引必须是整数或切片,而不是str [英] Python3 TypeError: list indices must be integers or slices, not str

查看:137
本文介绍了Python3 TypeError:列表索引必须是整数或切片,而不是str的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的任务是将字符串'AAAABBBCCDAABBB'放入这样的列表中: ['A','B','C','D','A','B']

我现在正在处理2个小时,但是我找不到解决方案.到目前为止,这是我的代码:

  list = []字符串='AAAABBBCCDAABBB'我= 1对于我的字符串:list.append(i)打印(列表)对于列表中的元素:如果list [element] == list [element-1]:list.remove(list [element])打印(列表) 

我是编程的新手,并且总是显示错误"TypeError:列表索引必须是整数或切片,而不是str" ...

我已经更改了比较

  if list [element] == list [element-1] 

 如果list [element]是list [element-1] 

但是错误保持不变.我已经在Google上搜索过几次,但总有一些列表不需要字符串格式,但我需要它(对吗?).

感谢您的帮助! NoAbL

解决方案

首先不要在内置python语句或数据结构(如 list tuple )后为变量命名.代码>甚至是您导入的模块的名称,这也适用于文件.例如,命名文件 socket.py 并导入 socket 模块肯定会导致错误(我将让您自己尝试).

在您的代码中 element 是一个字符串,可迭代的索引必须是数字而不是字符串,这样您就可以告诉python

将位置2的物品给我.

但是现在您要说的是在 A 位置给我该商品,而且即使是英语,这也是无效的,不用说编程语言.

如果要在遍历循环时获取可迭代对象的索引,则应该使用 enumerate 函数,或者可以这样做

适用于范围内的i的

 (len(list)) 

并遍历列表的长度范围,无论如何您实际上并不需要这些元素.

这是您想要做的一种简单的方法

  s =字符串='AAAABBBCCDAABBB'ls = []对于我在s中:如果ls:如果i!= ls [-1]:ls.append(i)别的:ls.append(i)打印(ls) 

i have the task to get the String 'AAAABBBCCDAABBB' into a list like this: ['A','B','C','D','A','B']

I am working on this for 2 hours now, and i can't get the solution. This is my code so far:

list = []

string = 'AAAABBBCCDAABBB'

i = 1

for i in string:
    list.append(i)

print(list)

for element in list:
    if list[element] == list[element-1]:
        list.remove(list[element])

print(list)

I am a newbie to programming, and the error "TypeError: list indices must be integers or slices, not str" always shows up...

I already changed the comparison

if list[element] == list[element-1]

to

if list[element] is list[element-1]

But the error stays the same. I already googled a few times, but there were always lists which didn't need the string-format, but i need it (am i right?).

Thank you for helping! NoAbL

解决方案

First of all don't name your variables after built in python statements or data structures like list, tuple or even the name of a module you import, this also applies to files. for example naming your file socket.py and importing the socket module is definitely going to lead to an error (I'll leave you to try that out by yourself)

in your code element is a string, indexes of an iterable must be numbers not strings, so you can tell python

give me the item at position 2.

but right now you're trying to say give me the item at position A and that's not even valid in English, talk-less of a programming language.

you should use the enumerate function if you want to get indexes of an iterable as you loop through it or you could just do

for i in range(len(list))

and loop through the range of the length of the list, you don't really need the elements anyway.

Here is a simpler approach to what you want to do

s = string = 'AAAABBBCCDAABBB'

ls = []

for i in s:
    if ls:
        if i != ls[-1]:
            ls.append(i)
    else:
        ls.append(i)

print(ls)

这篇关于Python3 TypeError:列表索引必须是整数或切片,而不是str的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆