简体   繁体   English

为什么在打印列表时没有出现错误时出现 IndexError: string index out of range?

[英]why getting IndexError: string index out of range when i get no error when i print the list?

i am getting error for 2 functions where 2 functions are almost same.我收到 2 个函数的错误,其中 2 个函数几乎相同。 in 1 function i just print and in another function i assign each word to a temp variable from list indices instead of printing.在 1 function 中,我只是打印,在另一个 function 中,我将每个单词分配给列表索引中的临时变量,而不是打印。 the function that prints gives no error but the function that assigns words instead of printing gives index out of bound error,why?打印的 function 没有错误,但分配单词而不是打印的 function 给出索引超出范围错误,为什么? please check the_magic() and the_magic1() functions below:请检查下面的 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
for i  in list(str3):
        temp = i
        if(temp == str2):
            str3 = str2
        else:
            str3 = str3[-1]
    return str3

The functions are NOT doing the same.功能不一样。

# 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.

You should explain what you intended to program with that.你应该解释你打算用它来编程什么。

Comments to your code对您的代码的注释

    str1 = str1
    str2 =  str2
    str3 = []

In Python, strings are always copied, so no need to fear call by reference problems when dealing with strings.在 Python 中,字符串总是被复制的,所以在处理字符串时不必担心call by reference问题。 Simplify it to:将其简化为:

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

Obviously you are searching which words in str1's words contain any of the words given in str2.显然,您正在搜索 str1 的单词中的哪些单词包含 str2 中给出的任何单词。 The rest is still not understandable. rest 还是看不懂。

暂无
暂无

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

相关问题 我在尝试打印大文件时收到 IndexError: list index out of range error in pandas - I'm getting IndexError: list index out of range error in pandas when trying print a huge file 为什么收到此错误“ IndexError:字符串索引超出范围” - Why am I getting this Error “IndexError: string index out of range” IndexError: string index out of range - 当索引不在调用列表范围之外时,我如何得到这个错误? - IndexError: string index out of range - How do I get this error when the index does not go outside the range of called list? 当我的代码似乎在范围内时,为什么会出现 IndexError: list index out of range? - Why am I getting an IndexError: list index out of range when my code appears to be within the range? 为什么会出现“ IndexError:列表索引超出范围”错误? - Why do I get a “IndexError: list index out of range” error? 使用串联的数据框时,为什么会出现“ IndexError:字符串索引超出范围” - Why am I getting an ‘IndexError: string index out of range’ when I use a concatenated dataframe 为什么在云上训练时会收到“IndexError: list index out of range”? - Why am I getting "IndexError: list index out of range" when training on the cloud? 当我运行此命令“cascPath = sys.argv[1]”时,我收到错误 IndexError: list index out of range - When i run this command " cascPath = sys.argv[1] " i get error IndexError: list index out of range 当使用while循环创建“list”时,我得到此错误:IndexError:列表赋值索引超出范围 - I get this error when using a while loop to make a “list” IndexError: list assignment index out of range 将图像转换为CSV文件时,为什么会出现IndexError:列表索引超出范围? - Why do I get IndexError: List index out of range when turning images into CSV file?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM