【问题标题】:why getting IndexError: string index out of range when i get no error when i print the list?为什么在打印列表时没有出现错误时出现 IndexError: string index out of range?
【发布时间】:2020-06-07 07:18:25
【问题描述】:

我收到 2 个函数的错误 其中2个功能几乎相同。 在 1 个函数中,我只是打印,在另一个函数中,我将每个单词分配给列表索引中的临时变量,而不是打印。打印的函数没有错误,但是分配单词而不是打印的函数给出了索引超出范围的错误,为什么? 请检查下面的 the_magic() 和 the_magic1() 函数:

def the_magic(str1,str2):
    str1 = str1
    str2 =  str2
    str3 = []

    for n in str1.split():
        for m in str2.split():
            if m in n:
                str3.append(n)
    if(len(str3)<1):
        str3 = ' '
    print(str3)
    for i  in range(len(str3)):
        print(str3[i])
    return str3

print(str3[i]) gives no error but ...

def the_magic1(str1,str2):
    str1 = str1
    str2 =  str2
    str3 = []

    for n in str1.split():
        for m in str2.split():
            if m in n:
                str3.append(n)
    if(len(str3)<1):
        str3 = ' '
    print(str3)
    for i  in range(len(str3)):
        temp = str3[i]
        if(temp == str2):
            str3 = str2
        else:
            str3 = str3[-1]
    return str3

temp = str3[i] giving error for the_magic1()
# function call :

a = "_MileyCyrus it wont let me do it twitter keeps saying over twitter capacity or something that bird keeps coming up."
b = 'it'
c = " "
b = b.split()
b = b[-1] 
str4=the_magic(a,b) # no error, returns str3 and  also prints each word
str4=the_magic1(a,b) #it gives error : IndexError: string index out of range

【问题讨论】:

  • 这段代码充满了不必要的东西——比如:str1=str1str2=str2,奇怪的东西,比如声明str3=[],然后是str3=' ',分配b='it',然后拆分它然后将其自身的最后一个元素分配给它......还有不是代码的文本而不在代码中注释它们......还有什么代码你应该在你的方法中使用?目的是什么?请删除所有不相关的内容以使其成为minimal reproducible example 并解释您的目标。谢谢。
  • 为什么在你的 magic1 方法中将str2(一个字符串)分配给str3(一个列表)?您在迭代 str3 的“以前的长度作为列表”时更改了它的内容,这完全没有意义,抱歉。
  • @MobassirHossen 你的 function the_magic1 应该做什么,因为 str3 列表正在重新分配 str2 的值,但迭代次数保持不变
  • def meta_magic(word_list, text): + words_containing_text = [word for word in word_list.split() if text in word]' + 'print(*words_containing_text, sep="\n") + return words_containing_text
  • 这是为了调试目的,这里是park提供的解决方案的更新代码::pastebin.com/13KbaHFV

标签: python python-3.x string list


【解决方案1】:
for i  in list(str3):
        temp = i
        if(temp == str2):
            str3 = str2
        else:
            str3 = str3[-1]
    return str3

【讨论】:

  • @HankPark 最好将i 替换为characterword 以使事情更明确。也不再需要temp
【解决方案2】:

功能不一样。

# instead of printing you have:
    for i  in range(len(str3)):
        temp = str3[i]
        if(temp == str2):
            str3 = str2
        else:
            str3 = str3[-1]
# in the second function.
# problem is, as soon as first word in str3 - which is not a string
# but this list:
# ['it', 'it', 'twitter', 'twitter', 'capacity']
# so if you loop through the words and as soon as one word
# is identical to what is given by `b` (which is `it` in this example)
# str3 (the list) gets set to "it" (str2, here b).
# and if not, str3 gets set to the last word in the list.
# in both cases str3 is not a list any more.

你应该解释你打算用它来编程什么。

对代码的注释

    str1 = str1
    str2 =  str2
    str3 = []

在 Python 中,字符串总是被复制的,因此在处理字符串时无需担心call by reference 问题。 将其简化为:

    str3 = []
    for n in str1.split():
        for m in str2.split():
            if m in n:
                str3.append(n)

显然,您正在搜索 str1 的单词中的哪些单词包含 str2 中给出的任何单词。 剩下的还是看不懂。

【讨论】:

  • 它现在也会给出同样的错误,因为循环正在处理 str3 的长度,如果它不再是一个列表,那么它会抛出一个错误
  • 这并不是为了更正,而只是显示他的代码中发生的事情。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-09-23
  • 1970-01-01
  • 2023-03-27
  • 1970-01-01
  • 2022-08-20
  • 2020-02-03
相关资源
最近更新 更多