简体   繁体   中英

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. in 1 function i just print and in another function i assign each word to a temp variable from list indices instead of printing. the function that prints gives no error but the function that assigns words instead of printing gives index out of bound error,why? please check the_magic() and the_magic1() functions below:

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. 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. The rest is still not understandable.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM