简体   繁体   English

通过具有不同可能字符的字符串进行迭代

[英]Iterating through a string with different possible characters

I just signed up here because I am taking an online course in Python and have been using this site to help me through the course. 我刚刚在这里报名,因为我正在参加Python的在线课程并且一直在使用这个网站来帮助我完成课程。 I am; 我是; however, stuck. 卡住了。

I am not posting my actual homework assignment, but rather just an element of my code I am having a difficult time with... 我没有发布我的实际作业,而只是我的代码的一个元素我很难与...

I am trying to iterate through a string using a list containing letters in the alphabet. 我试图使用包含字母表中字母的列表迭代字符串。 I want to have each letter in the list iterate through the word at different indexes. 我想让列表中的每个字母迭代不同索引处的单词。 For example: 例如:

word = "panda" char_list = ['a','b','c'] etc... Output should be aanda, panda, paada ...... Followed by banda,pbnda,pabda,... word =“panda”char_list = ['a','b','c']等...输出应该是aanda,panda,paada ......其次是banda,pbnda,pabda,...

My code iterates through the word using only the first character in the list. 我的代码只使用列表中的第一个字符迭代单词。 Sorry, I am super NEW to coding in general... 对不起,我对编码总体来说太新了......

index = 0
word = "panda"
possible_char = ['a', 'b', 'c', 'd', 'o']
for char in possible_char:
    while index < len(word):
        new_word = word[:index] + char + word[index + 1:]
        print (new_word)
        index = index + 1

You were very close. 你非常接近。 You just need to reset the index to zero. 您只需将索引重置为零。 So after the for loop your first command should be index=0 . 所以在for循环之后你的第一个命令应该是index=0

Your while loop only works for the first iteration of the outer for loop because index does not get reset and remains at len(word) after it finshes the first time. 你的while循环只适用于外部for循环的第一次迭代,因为index不会被重置并在第一次完成后保持在len(word) Try moving the line where you initialize it to 0 inside the outer loop: 尝试将初始化的行移动到外部循环内的0

for char in possible_chars:
    index = 0
    while index < len(word):
        #...
index = 0
word = "panda"
possible_char = ['a', 'b', 'c', 'd', 'o']
for char in possible_char:
    index = 0
    while index < len(word):
        new_word = word[:index] + char + word[index + 1:]
        print (new_word)
        index = index + 1

you have to re-initialize index on the forloop, just to start all over again on the word 你必须在forloop上重新初始化索引,只是为了重新开始这个词

You just need to reset the index back to 0 after you are done iterating with each char . 在完成对每个char的迭代后,您只需将索引重置为0。

index = 0
word = "panda"
possible_char = ['a', 'b', 'c', 'd', 'o']
for char in possible_char:
   index=0
   while index < len(word):
      new_word = word[:index] + char + word[index + 1:]
      print (new_word)
      index = index + 1

You forgot to initialise the index counter in the for loop: 你忘了在for循环中初始化索引计数器:

index = 0
word = "panda"
possible_char = ['a', 'b', 'c', 'd', 'o']
for char in possible_char:
    index = 0
    while index < len(word):
        new_word = word[:index] + char + word[index + 1:]
        print (new_word)
        index = index + 1

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

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