簡體   English   中英

兩對連續值列表 python

[英]Two pairs of consecutive values list python

如果您在 python 中有這樣的列表: list1 = [(1,2), (3,1), (4,1), (1,2), (3,1)]
如果您在 python 中有list1並且您想查找 python 中列表中的兩對連續值是否相等,例如 (1,2) 和 (3,1) 重復兩次,並且您想更新一個變量讓i說2 基於此。 您將如何在 python 中執行此操作?

這是我得到它的方法,遍歷原始列表並為每一對制作迷你列表,然后我檢查該對是否出現兩次。

lst = [(1,2), (3,1), (4,1), (1,2), (3,1)]

#iterate through list and append each pair of values
tmp = []
for i in range(0, len(lst)-1):
    tmp.append(lst[i] + lst[i+1])

#see if any of the pairs appear twice, if so add to new list and get the length of that list to be your count
output =  [x for x in tmp if tmp.count(x) > 1]
i = len(output)
print(i)

暫無
暫無

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

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