简体   繁体   English

两对连续值列表 python

[英]Two pairs of consecutive values list python

If you have a list in python like this: list1 = [(1,2), (3,1), (4,1), (1,2), (3,1)]如果您在 python 中有这样的列表: list1 = [(1,2), (3,1), (4,1), (1,2), (3,1)]
If you have list1 in python and you want to find if two pairs of consecutive values in a list in python are equal like (1,2) and (3,1) is repeated twice and you want to update a variable lets say i to 2 based on this.如果您在 python 中有list1并且您想查找 python 中列表中的两对连续值是否相等,例如 (1,2) 和 (3,1) 重复两次,并且您想更新一个变量让i说2 基于此。 How would you do this in python?您将如何在 python 中执行此操作?

Here is how I got it, looped through the original list and made mini lists of every each pair, then I checked to see if that pair occurs twice.这是我得到它的方法,遍历原始列表并为每一对制作迷你列表,然后我检查该对是否出现两次。

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