[英]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
.因为在索引
i
处insert
'_'
后,大写字母 ( 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:下面是执行的样子:
insert '_'
--> 'W' moves to new[i+1]
insert '_'
--> 'W' 移动到new[i+1]
insert '_'
--> and this pattern repeats 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.