简体   繁体   中英

Python: Why does my “variable += 1” not work as expected?

username = "neelkamath@gmail.com"
usernamelist = []
next = 0
while len(username) > 0:
    for part in accumulate(username):
        usernamelist.append(part)
    next += 1
    username = username[next:]
print(usernamelist)

I'm trying to output every part of the username (like "n", "ne", "nee", ... , "neelkamath@gmail.com", "e", "ee", ... , "eelkamath@gmail.com" etc.) but instead of going from "n" to "neelkamath@gmail.com" and then from "e" to "eelkamath@gmail.com" and then from "e" to "elkamath@gmail.com"; it skips an extra one letter each time, despite me only adding 1 to "next". It goes from "n" to "e" (one letter ahead, as it's supposed to) to "l" (2 letters ahead) to "m" (3 letters ahead) and so on. Am I placing the next += 1 in an incorrect position?

You don't need to increment next if you want to remove one character for each iteration of the while loop:

username = "neelkamath@gmail.com"
usernamelist = []
while len(username) > 0:
    for part in accumulate(username):
        usernamelist.append(part)
    username = username[1:]
print(usernamelist)

By incrementing next you increment the number of characters to be skipped:

At first iteration: after username = username[1:] username is "eelkamath@gmail.com"

At second iteration: after username = username[1:] username is "elkamath@gmail.com" but after username = username[2:] username is "lkamath@gmail.com"

You would like to use a for-loop instead:

username = "neelkamath@gmail.com"
usernamelist = []
for start in range(len(username)):
    usernamelist.extend(accumulate(username[start:]))
print(usernamelist)

But accumulate is not very efficient. For large strings, use slicing:

username = "neelkamath@gmail.com"
usernamelist = [
    username[s:e]
    for s, e in combinations(range(len(username)+1), 2)
]
print(usernamelist)

The problem is that you are modifying «username» from an already modified value .

To better understand this, think that the line:

username = username[next:]

is not picking a part of the original value ( neelkamath@gmail.com ), but from the already trimmed (in the previous loop). It's all about what's your reference for the next iteration.

This example may help:

from itertools import accumulate
username1 = "neelkamath@gmail.com"
username2 = "neelkamath@gmail.com"
usernamelist = []
next = 0
while len( username2 ) > 0:
    for part in accumulate( username2 ):
        usernamelist.append( part )
    next += 1
    username2 = username1[next:]
    print( username1 )
    print( username2 )
print(usernamelist)

a = "neelkamath@gmail.com" for i in range(0, len(a) + 1): print a[:i]

输出量

n ne nee neel neelk neelka neelkam neelkama neelkamat neelkamath neelkamath@ neelkamath@g neelkamath@gm neelkamath@gma neelkamath@gmai neelkamath@gmail neelkamath@gmail. neelkamath@gmail.c neelkamath@gmail.co neelkamath@gmail.com

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