簡體   English   中英

如何使用 for range 循環遍歷 Python 中的兩個列表?

[英]How to use a for range loop to iterate through two lists in Python?

需要一些幫助,請使用帶有 range 和 len 的 for 循環。 兩個問題。

假設我有一個由“W”或“L”組成的列表,表示游戲贏了或輸了,第二個列表用整數表示得分(並且位置對應於同一場比賽),那么我怎么能寫一個函數(不使用 zip 功能)它會告訴用戶例如“我們贏了多少場比賽並且得到了 3 分”?以及“我們贏了多少場比賽並且至少得到 9 分”?

起初我嘗試合並兩個列表和位置,即位置 0 的值類似於 L4。 然后我可以搜索特定標准,但是當搜索大於或小於特定數字的點數時,這會變得棘手(至少贏 x 點)。

現在我正在考慮遍歷一個循環,比如 i in for i in range(x)) 和 len。 但是我很困惑這如何與兩個列表一起工作,而且我對編碼和掌握語法仍然很陌生。

感謝您的投入和時間

我的代碼是一場災難....

def score_check(lst1, lst2):
    for 'W' in range(lst1[i] if 3 in lst2[i]):
    .........
    .....
    return result 

def main():
    record = ['L', 'W', 'W', 'W', 'W', 'W', 'W', 'W', 'W', 'W', 'L', 'W', 'W']
    points = [4, 1, 3, 2, 7, 4, 3, 10, 8, 14, 7, 6, 7]

    check = score_check(record, points)
    print(check)

main()

預期成績:

2 wins with 3 points scored and
2 wins with at least 9 points scored

所以,我認為最好的選擇是使用zip 但是,由於您不想使用它,您可以這樣做:

def score_check(lst1, lst2):
    lst_size_1 = len(lst1)
    lst_size_2 = len(lst2)
    if (lst_size_1 !=lst_size_2):
        return

    for i in range(lst_size):
        print (lst1[i])
        print (lst2[i])

祝你好運!

如果您不能使用zip那么您可以只使用元素的索引。

record = ['L', 'W', 'W', 'W', 'W', 'W', 'W', 'W', 'W', 'W', 'L', 'W', 'W']
points = [4, 1, 3, 2, 7, 4, 3, 10, 8, 14, 7, 6, 7]

combined = [{'points': points[i], 'result': record[i]} for i in range(len(record))]

# this way is cleaner
#combined = [{'points': p, 'result': r} for r, p in zip(record, points)] 

現在combined是一個字典列表,其中包含原始列表中record和點entries鍵/值對。 它假設兩個列表的長度相同,但這似乎是一個合理的假設,否則您的數據不是真正有效的。 zip將通過在耗盡兩個列表中較短的列表時停止來無縫處理此問題。 如果此代碼嘗試訪問任一列表中不存在的索引,則會拋出此錯誤。

如果你現在想查詢這個列表,你可以只使用filter內置函數並向它傳遞一個謂詞來決定什么在結果中,什么不在。

例子

print('matches where score was greater than 3')
for result in filter(lambda r: r['points'] > 3, combined):
    print(result)

這包括點值大於 3 的任何條目。您可以輕松地使其< (小於)、 >= (大於或等於)或== (等於)任何您想要的數字。

您還可以將結果參數組合到過濾的內容中

print('matches where score was less than 5 but won')
for result in filter(lambda r: r['points'] < 5 and r['result'] == 'W', combined):
    print(result)

注意:過濾函數總是線性的(O(n))。 它們總是遍歷所有條目。 因此,對於較大的結果集,其效率低下且速度緩慢。 調整它的一種方法是按分數對列表進行排序並使用二分搜索(在 python 中的bisect模塊)來縮小搜索范圍。 如果您根據分數進行搜索,這只會產生影響。 如果您想搜索 W/L 結果,那么您又回到了線性。

所以不允許使用 zip。 列表理解怎么樣:

def score_check(results, points):
    wins = [points[i] for i, v in enumerate(results) if v == 'W']
    exactly_three = len([win for win in wins if win == 3])
    at_least_nine = len([win for win in wins if win >= 9])

    return (f'{exactly_three} wins with 3 points scored',
            f'{at_least_nine} wins with at least 9 points scored')

record = ['L', 'W', 'W', 'W', 'W', 'W', 'W', 'W', 'W', 'W', 'L', 'W', 'W']
points = [4, 1, 3, 2, 7, 4, 3, 10, 8, 14, 7, 6, 7]

print(*score_check(record, points), sep='\n')

2 wins with 3 points scored
2 wins with at least 9 points scored

當函數參數是兩個列表時,只有獲得所需的輸出文本和條件必須硬編碼,這不是函數的最佳用法。

暫無
暫無

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

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