lis中特定数量的输入元素

2024-09-28 05:40:03 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个单词列表,我的输入是列表前半部分中的一个单词(在本例中,这个列表是一首德语歌曲)。现在我取这个单词的长度,跳到列表中这个单词的长度,例如,列表中的第一个单词是“Es”,长度是2。现在我们从“Es”数两次,然后用单词“zwei”着陆。我应该检查一下这个单子,看看这个词是否又出现在下半部分。在

程序正常,但问题在于输入。它只需要一个词,看看这个理论是否有效。 这是歌曲列表:“欣”是上半场的最后一个词

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"] 

我希望我的输入是列表前半部分的所有单词(本例中是歌曲),而不是一个单词。所以它只输出每行中每个单词的结果(在本例中是一个列表)。在

This is how the output now looks

我希望它能立即打印出前半部分每个单词的每个结果,如果理论成立的话。这将比输出结果更重要:

理论有效/无效

结果1

理论有效/无效

结果2

理论有效/无效

结果3

等等。。。在

代码如下:

^{pr2}$

Tags: 列表essong理论歌曲单词und本例
1条回答
网友
1楼 · 发布于 2024-09-28 05:40:03

下面是这个函数的一个版本,它处理列表第一部分中的每个单词。问题说明程序正常工作,所以主逻辑没有改变。在

以下是所做的更改:

  • 删除input语句
  • 在列表的第一部分添加for循环
  • 打印正在处理的字(为了调试和可读性,可以删除)
  • 删除多余的originalWordSavedoriginalIndex变量
  • 将过滤逻辑(单词在列表的后半部分,或者出现不到两次)移出while循环,因为它只需要每个单词运行一次
  • 将代码移动到不能导致将IndexError移出try/except块
  • 特别是trap 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)

相关问题 更多 >

    热门问题