简体   繁体   English

Python:为什么我的“变量+ = 1”不能按预期工作?

[英]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"; 我正在尝试输出用户名的每个部分(例如“ n”,“ ne”,“ nee”,...,“ neelkamath@gmail.com”,“ e”,“ ee”,...,“ eelkamath@gmail.com”等),而不是从“ n”转到“ neelkamath@gmail.com” ,然后从“ e”转到“ eelkamath@gmail.com” ,然后从“ e”转到“ elkamath @ gmail” .com”; it skips an extra one letter each time, despite me only adding 1 to "next". 它跳过每次额外的一个字母,尽管我只加1,“下一个”。 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. 它从“ n”到“ e”(应该是一个字母)到“ l”(前面是2个字母)再到“ m”(前面是3个字母),依此类推。 Am I placing the next += 1 in an incorrect position? 我是否将next += 1放在错误的位置?

You don't need to increment next if you want to remove one character for each iteration of the while loop: 如果要为while循环的每次迭代删除一个字符,则无需再增加next一个:

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: 通过增加next可以增加要跳过的字符数:

At first iteration: after username = username[1:] username is "eelkamath@gmail.com" 第一次迭代: username = username[1:] username"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" 在第二次迭代中:在username = username[1:] username"elkamath@gmail.com"但是在username = username[2:] username"lkamath@gmail.com"

You would like to use a for-loop instead: 您想改用for循环:

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

But accumulate is not very efficient. 但是accumulate不是很有效。 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). 不是从原始值( neelkamath@gmail.com )中选择一部分,而是从已修剪的部分(在上一个循环中)。 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

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

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