简体   繁体   English

Python 3x TypeError:列表索引必须是整数或切片,而不是 str

[英]Python 3x TypeError: list indices must be integers or slices, not str

I'm currently making a program which will compress a user sentence into two variables unique_words and positions.我目前正在制作一个程序,它将用户句子压缩成两个变量 unique_words 和位置。 Unique words will be all the unique words from the user's sentence, so for example "the cat sat on the mat" the unique words will be "the, cat, sat, on, mat" and the positions will be 123415. I'm saving both of these in a text file.唯一词将是用户句子中的所有唯一词,例如“the cat sat on the mat”,唯一词将是“the、cat、sat、on、mat”,位置为 123415。我是将这两个保存在一个文本文件中。

I'm fairly new to coding, therefore I preferably would not want to use dictionaries as I believe it would make it so my code will not be able to keep capitalisation and punctuation.我对编码相当陌生,因此我最好不想使用字典,因为我相信它会使我的代码无法保留大写和标点符号。

Then what I want to be able to do is extract these back from the text file, and store them in two separate variables, so for example I want my code to look something like so:然后我想要能够做的是从文本文件中提取这些,并将它们存储在两个单独的变量中,例如,我希望我的代码看起来像这样:

list_1 = [the, cat, sat, on, mat]
list_2 = [1, 2, 3, 4, 1, 5]
list_3 = []

for i in list_2:
  list_3.append(list_1[i])

However when I do this in my actual code:但是,当我在实际代码中执行此操作时:

for l in positions_File:
        orig_sntnce.append(uqwordsFile[l])
    print(orig_sntnce)`

I get the error TypeError: list indices must be integers or slices, not str I think I need to somehow convert my list that contains the unique words into a 'slice'?我收到错误类型错误TypeError: list indices must be integers or slices, not str我想我需要以某种方式将包含唯一单词的列表转换为“切片”? I'm really stuck on how to do this?我真的被困在如何做到这一点? I've tried splitting the list but I think this would again, make it so I will not be able to keep punctuation and capitalisation of the original sentence.我试过拆分列表,但我认为这会再次使我无法保留原始句子的标点符号和大写字母。

list_1 元素应该是字符串,即 list_1 = ['the', 'cat', 'sat', 'on', 'mat'],缺少单引号/双引号,还可以看到 list_2 有 5 作为元素导致错误,因为 list_1 只有 5 个元素,因此它只能从 0,4 索引。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Python3-TypeError:列表索引必须是整数或切片,而不是str-List - Python3 - TypeError: list indices must be integers or slices, not str - List “TypeError:list indices必须是整数或切片,而不是str” - “TypeError: list indices must be integers or slices, not str” TypeError:列表索引必须是整数或切片,而不是 str - TypeError: List indices must be integers or slices and not str 类型错误:列表索引必须是整数或切片,而不是 str - TypeError: list indices must be integers or slices, not str “TypeError:列表索引必须是整数或切片,而不是 str” - "TypeError: list indices must be integers or slices, not str" TypeError:列表索引必须是整数或切片而不是 str - TypeError: list indices must be integers or slices not str 类型错误:列表索引必须是整数或切片,而不是 str - TypeError: list indices must be integers or slices, not str 使用 Python TypeError 解析 JSON:列表索引必须是整数或切片,而不是 str - Parsing JSON with Python TypeError: list indices must be integers or slices, not str Python JSON TypeError 列表索引必须是整数或切片,而不是 str - Python JSON TypeError list indices must be integers or slices, not str TypeError:列表索引必须是整数或切片,而不是 str 使用 Python - TypeError: list indices must be integers or slices, not str Using Python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM