我有一个单词列表,我的输入是列表前半部分的单词之一(在这种情况下,此列表是德语歌曲)。现在,我将这个单词的长度跳到列表中这个单词的长度,f.e。列表中的第一个单词为“ Es”,长度为2。现在我们从“ Es”开始计数2次,然后按单词“ zwei”着陆。我应该浏览此列表,并检查单词是否再次出现在列表的后半部分。
程序可以运行,但是问题出在输入上。它花了一个字,看看该理论是否有效。 这是歌曲列表:“ hin”是上半部分的最后一个单词
song = [
"Es", "gingen", "zwei", "Parallelen",
"ins", "Endlose", "hinaus",
"zwei", "kerzengerade", "Seelen",
"und", "aus", "solidem", "Haus",
"Sie", "wollten", "sich", "nicht", "schneiden",
"bis", "an", "ihr", "seliges", "Grab",
"Das", "war", "nun", "einmal", "der", "beiden",
"geheimer", "Stolz", "und", "Stab",
"Doch", "als", "sie", "zehn", "Lichtjahre",
"gewandert", "neben", "sich", "hin", #End of the first half of the song
"da", "wards", "dem", "einsamen", "Paare",
"nicht", "irdisch", "mehr", "zu", "Sinn",
"Warn", "sie", "noch", "Parallelen",
"Sie", "wusstens", "selber", "nicht",
"sie", "flossen", "nur", "wie", "zwei", "Seelen",
"zusammen", "durch", "ewiges", "Licht",
"Das", "ewige", "Licht", "durchdrang", "sie",
"da", "wurden", "sie" "eins", "in", "ihm",
"die", "Ewigkeit", "verschlang", "sie",
"als", "wie", "zwei", "Seraphim"]
我希望输入的内容是列表前半部分的所有单词(在这种情况下为歌曲),而不仅仅是一个单词。因此,它只是打印出每一行中每个单词的结果(在这种情况下,结果是列表)。
我希望它能在理论的前半部分立即打印出每个单词的每个结果。这将比输出:
理论有效/无效
结果1
理论有效/无效
Result2
理论有效/无效
Result3
依此类推...
代码如下:
with open('C:/Users/xy/Desktop/BWINF/parallelen.txt', 'r') as f:
song = f.read()
noneed = "–?,.;:"
for char in noneed:
song = song.replace(char, "")
song = song.split()
def Parallelen(listSong):
originalWord = input("Enter a word: ")
originalWordSaved = originalWord
theorie_list = [] # The list for found words
index = song.index(originalWord) # Get the index of the first instance of "word"
indexOriginal = song.index(originalWordSaved)
wordCount = song.count(originalWord)
while True:
if indexOriginal > 42:
print("Word is in the second half")
break
if wordCount <= 1:
print("Word appears only 1 time and therefore can't appear one more time")
print("Theorie doesn't work")
break
try:
theorie_list.append(listSong[index])
theorie_list.append(len(listSong[index]))
index += len(listSong[index])
if listSong[index] == originalWordSaved:
theorie_list.append(listSong[index])
theorie_list.append(len(listSong[index]))
print("Theorie works")
break
except:
print("Theorie doesn't work")
break
return theorie_list
print(Parallelen(song))
答案 0 :(得分:1)
这是处理列表第一部分中每个单词的函数的版本。该问题指出该程序可以正常运行,因此主要逻辑未更改。
这些是已进行的更改:
input
语句for
循环originalWordSaved
和originalIndex
变量while
循环,因为每个单词只需要运行一次IndexError
脱离try / except块的位置IndexError
,因为这是代码可能引发的唯一错误:应避免使用裸露的except
语句def Parallelen(listSong):
# Magic number: length of list is 89
halfway = 42
for originalWord in listSong[: halfway + 1]:
print("\nProcessing {!r}".format(originalWord))
theorie_list = [] # The list for found words
index = song.index(originalWord)
# Get the index of the first instance of "word"
index = song.index(originalWord)
if index > halfway:
print("Word is in the second half")
continue
wordCount = song.count(originalWord)
# The word must appear at least once
if wordCount == 1:
print("Word appears only 1 time and therefore can't appear one more time")
print("Theorie doesn't work")
continue
while True:
theorie_list.append(listSong[index])
theorie_list.append(len(listSong[index]))
index += len(listSong[index])
try:
if listSong[index] == originalWord:
theorie_list.append(listSong[index])
theorie_list.append(len(listSong[index]))
print("Theorie works")
break
except IndexError:
print("Theorie doesn't work")
break
print(theorie_list)
return
Parallelen(listSong)