簡體   English   中英

While循環Python中的While循環

[英]While-loop within a while-loop python

我正在嘗試在while循環內編寫一個while循環,由於某種原因,它無法正常工作。 我知道我可能在這里錯過了一些瑣碎的事情,但是我只是不明白它是如何工作的!

循環的目的是比較兩個字符串,以查看它們是否包含相同的三個連續單詞。 我首先將兩個字符串分成各自的3個單詞字符串組合的列表,然后將它們存儲在列表中的string和stringscompare。 然后,我遍歷字符串中的每個3字字符串,比較字符串中的每個3字字符串。

對於某些人來說,這似乎很漫長,但是我只是一個新手程序員,任何改進將不勝感激。

因此,目前,第二個while循環一直貫穿整個過程,但是對於字符串中的第一個字符串,第一個while循環僅循環一次。 如果字符串匹配,我希望它從兩個循環中都斷開,但是這些循環也位於for循環中,這是我不希望其中斷的更大的循環。

例如

'這是一個字符串'
'這是另一個字符串'-不匹配
'這是一個字符串列表'-匹配'this is a'
'列表是一個字符串'-應該匹配'is a string'但當前不匹配

strings = <list of 3 word strings> [...,...,...]
stringscompare = <list of 3 word strings to compare>
v=0, x=0
while v < len(strings) and stop == False:
    while x < len(stringscompare) and stop == False:
        if re.search(strings[v], stringscompare[x]):
            same.append(dict)
            stop = True
        x += 1
    v +=1

您永遠不會在外循環內重置x 結果,它在外循環的第一次迭代之后將始終等於或大於len(stringscompare)

外部循環中將其設置為0

v = 0
while v < len(strings) and stop == False:
    x = 0
    while x < len(stringscompare) and stop == False:

其他觀察:

不要使用stop == False ,在not stop會做。

您可以只使用for循環,然后中斷兩次:

for s in strings:
    for sc in stringscompare:
        if re.search(s, sc):
            same.append(dict)
            break
    else:
        continue
    # only reached if the inner loop broke out
    break

或使用帶有生成器表達式的any()來查找第一個匹配項:

 if any(re.search(s, sc) for s in strings for sc in stringscompare):
     same.append(dict)

您使用兩次相同的變量stop我認為您應該在循環后將stop設置為false:

strings = <list of 3 word strings> [...,...,...]
stringscompare = <list of 3 word strings to compare>
v=0, x=0
while v < len(strings) and stop == False:
    while x < len(stringscompare) and stop == False:
        if re.search(strings[v], stringscompare[x]):
            same.append(dict)
            stop = True
        x += 1
    stop = False
    v +=1

我建議像這樣。

strings = [] # <list of 3 word strings> [...,...,...]
stringscompare = [] # <list of 3 word strings to compare>

stop = False

for x in strings:
    if stop:
        break

    for v in stringscompare:
        if re.search(v, x):
            stop = True
            break

for循環在這里似乎更合適。 這是您要達到的目標嗎?

好的,這里的代碼最明顯的問題是,一旦內部循環結束,您就不必再將X的值設置為0了。 我將說明:假設“字符串”和“字符串比較”的長度均為3。當您第一次進入big while循環時,v = 0和x = 0。 當我們進入第二個循環時,x = 0。 當我們離開第二個循環時,如果找不到匹配項,則x = length(stringscompare)= 3。

現在,當我們回到第一個while循環時,它實際上確實又進入了循環-但是它什么也沒做,因為不能滿足第二個循環的子句-X仍然是3!

您可以通過在循環2的結尾處重置X來解決此問題:

strings = <list of 3 word strings> [...,...,...]
stringscompare = <list of 3 word strings to compare>
v=0, x=0
while v < len(strings) and stop == False:
    while x < len(stringscompare) and stop == False:
        if re.search(strings[v], stringscompare[x]):
            same.append(dict)
            stop = True
        x += 1
    x = 0 #resetting X
    v +=1

您可以通過調試輕松跟蹤此類問題。 有很多調試方法,這實際上是一種藝術:P我為您推薦的是:

  1. 如果您使用的是Eclipse,PyCharm等,則可以使用內置的調試器,這很棒。 您可以通過添加斷點來停止程序的任何位置,查看所有變量的值,等等。例如,在這種情況下,我將在第一個循環語句上放置一個斷點(“ while v <len ...” )。 這樣,您可以輕松地看到第一個循環實際上正在運行,而第二個循環就是問題所在。

  2. 印刷是您的朋友。 像這樣簡單的事情:

 v=0, x=0 while v < len(strings) and stop == False: print "About to enter second loop..." while x < len(stringscompare) and stop == False: if re.search(strings[v], stringscompare[x]): same.append(dict) stop = True x += 1 v +=1 

您還可以打印變量btw的值。

還有許多其他方法可以執行您要執行的操作(三角圖,高級搜索方法,也許是更復雜的數據結構等)。 但是就目前而言-我認為對您來說最好的辦法是以最直接的方式解決問題-專注於編寫高質量的代碼(命名變量,縮進,注釋等)。 令人討厭的,“頂級性能”的最新技巧將在以后出現:)

祝好運並玩得開心點!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM