简体   繁体   English

为什么它插入不止一次?

[英]Why does it insert it more than once?

Trying to insert " _ " before uppercase letters but it inserts more than once.尝试在大写字母前插入“_”,但插入不止一次。

Instead of ["h", "e", "l", "l", "o", " _ ", "W", "o", "l", "r", "d"] it prints ['h', 'e', 'l', 'l', 'o', ' _ ', ' _ ', ' _ ', ' _ ', 'W', 'o', 'l', 'r', 'd']而不是 ["h", "e", "l", "l", "o", "_", "W", "o", "l", "r", "d"] 它打印 [ 'h'、'e'、'l'、'l'、'o'、'_'、'_'、'_'、'_'、'W'、'o'、'l'、'r ','d']

new = ["h", "e", "l", "l", "o", "W", "o", "r", "l", "d"]

for i in range(len(new)):
    print(i)
    if new[i].isupper():
        new.insert(i, "_")

print(new)

Because after you insert '_' at index i , the uppercase letter ( W ) is moved to the right and thus gets the index i+1 .因为在索引iinsert '_'后,大写字母 ( W ) 将向右移动,从而获得索引i+1 Now the for loop iteration completes, and i gets incremented and becomes i+1 , but guess what is at index i+1 ?现在 for 循环迭代完成, i递增并变为i+1 ,但猜猜i+1处的索引是什么? (Hint: W ) So, this is a cycle that only exhausts when the for loop iterates len(new) times. (提示: W )所以,这是一个只有在 for 循环迭代len(new)次时才会耗尽的循环。 Below is what the execution might look like:下面是执行的样子:

  • (i=0): new[i] is 'h' (i=0): new[i] 是 'h'
  • (i=1): new[i] is 'e' (i=1): new[i] 是 'e'
  • (i=2): new[i] is 'l' (i=2): new[i] 是 'l'
  • (i=3): new[i] is 'l' (i=3): new[i] 是 'l'
  • (i=4): new[i] is 'o' (i=4): new[i] 是 'o'
  • (i=5): new[i] is 'W' --> insert '_' --> 'W' moves to new[i+1] (i=5): new[i] is 'W' --> insert '_' --> 'W' 移动到new[i+1]
  • (i=6): new[i] is 'W' --> insert '_' --> and this pattern repeats (i=6): new[i] is 'W' --> insert '_' --> 这个模式重复

A simple solution would be to run backwards.一个简单的解决方案是向后运行。 As now, insert '_' will push 'W' to the right, while we are moving to the left;和现在一样, insert '_'会将 'W' 推到右边,而我们正在向左移动; therefore, we won't end up in the cycle like before.因此,我们不会像以前那样陷入循环。

new = ["h", "e", "l", "l", "o", "W", "o", "r", "l", "d"]
start = len(new) - 1 # last element
end = -1 # one prior to the first element
         # remember, range end is non-inclusive,
         # so it goes till index 0
step = -1 # decrement to move backwards
for i in range(start, end, step):
    if new[i].isupper():
        new.insert(i, "_")
print(new)

You can create a new list:您可以创建一个新列表:

new = ["h", "e", "l", "l", "o", "W", "o", "r", "l", "d"]

result = []
for i in range(len(new)):
    if new[i].isupper():
        result.append("_")
    result.append(new[i])

print(result)

In your code, each call to new.insert(i, "_") changes new so that the uppercase letter that was previously at index i is now at index i + 1 , since you have just inserted the character _ at index i and pushed he uppercase letter (and every item that comes after it in the list new ) one position higher.在您的代码中,对new.insert(i, "_")的每次调用都会更改new以便以前在索引i处的大写字母现在位于索引i i + 1处,因为您刚刚在索引处插入了字符_并且将他的大写字母(以及列表中new出现的每个项目)推高了一个位置。

One way to fix this would be to loop through new in reverse order using python's built-in functionreversed() :解决此问题的一种方法是使用 python 的内置函数reversed()以相反的顺序循环遍历new

new = ["h", "e", "l", "l", "o", "W", "o", "r", "l", "d"]

for i in reversed(range(len(new))):
    #print(i)
    if new[i].isupper():
        new.insert(i, "_")

print(new)

Now, after examining the character at index i , detecting that it's uppercase, and inserting _ at i , the next loop iteration examines the character at position i - 1 , which was unaffected by the prior iteration's call to new.insert(i, "_") .现在,在检查索引i处的字符、检测到它是大写字母并在i处插入_之后,下一个循环迭代检查位置i - 1处的字符,该位置不受先前迭代对new.insert(i, "_")的调用的影响new.insert(i, "_")

Output:输出:

['h', 'e', 'l', 'l', 'o', '_', 'W', 'o', 'r', 'l', 'd']

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

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