迭代python中的元组并将它们与约束进行比较

时间:2017-04-30 16:27:48

标签: python tuples

我有一个像这样的元组列表:

myList = [(0, 0, 0, 0),
          (0, 0, 0, 1),
          (0, 0, 1, 0),
          (0, 1, 0, 0),
          (0, 1, 0, 1), 
          (1, 0, 1, 0), 
          (1, 0, 1, 1), 
          (1, 1, 0, 1), 
          (1, 1, 1, 0), 
          (1, 1, 1, 1)]

我想迭代元组并将每个元组与每个其他元组进行比较,如果第一个元素发生了变化则返回true,并且最多还有一个元素也会返回;否则返回false。

例如:

cmp((0, 0, 0, 0), (0, 0, 0, 1)) -> false, the first element did not change  
cmp((0, 0, 1, 0), (1, 0, 1, 0)) -> true, the first element changed  
cmp((0, 0, 0, 0), (1, 0, 1, 0)) -> true, the first element and only one other changed  
cmp((0, 0, 0, 0), (1, 1, 1, 0)) -> false, too many elements changed

编辑:我解决了感谢heyu91 我使用的最终代码看起来像这样

G=Graph()
G.add_vertices(myList)
for x in myList:
    for y in myList:
        if x!=y and (x[0]!=y[0]) and (sum((x[1]!=y[1], x[2]!=y[2], x[3]!=y[3]))<=1):
            G.add_edge(x,y)

1 个答案:

答案 0 :(得分:0)

我不知道输出的格式,所以我返回一个如下所示的元组列表:

[(x, y, (x[0]!=y[0]) and (sum((x[1]!=y[1], x[2]!=y[2], x[3]!=y[3]))<=1)) \ for x in myList \ for y in myList \ if x!=y]

返回:

[((0, 0, 0, 0), (0, 0, 0, 1), False),
 ((0, 0, 0, 0), (0, 0, 1, 0), False),
 ((0, 0, 0, 0), (0, 1, 0, 0), False),
 ((0, 0, 0, 0), (0, 1, 0, 1), False),
 ((0, 0, 0, 0), (1, 0, 1, 0), True),
 ((0, 0, 0, 0), (1, 0, 1, 1), False),
 ((0, 0, 0, 0), (1, 1, 0, 1), False),
 ((0, 0, 0, 0), (1, 1, 1, 0), False),
 ((0, 0, 0, 0), (1, 1, 1, 1), False),
 ((0, 0, 0, 1), (0, 0, 0, 0), False),
 ((0, 0, 0, 1), (0, 0, 1, 0), False),
 ((0, 0, 0, 1), (0, 1, 0, 0), False),
 ((0, 0, 0, 1), (0, 1, 0, 1), False),
 ((0, 0, 0, 1), (1, 0, 1, 0), False),
 ((0, 0, 0, 1), (1, 0, 1, 1), True),
 ((0, 0, 0, 1), (1, 1, 0, 1), True),
 ((0, 0, 0, 1), (1, 1, 1, 0), False),
 ((0, 0, 0, 1), (1, 1, 1, 1), False),
 ((0, 0, 1, 0), (0, 0, 0, 0), False),
 ((0, 0, 1, 0), (0, 0, 0, 1), False),
 ((0, 0, 1, 0), (0, 1, 0, 0), False),
 ((0, 0, 1, 0), (0, 1, 0, 1), False),
 ((0, 0, 1, 0), (1, 0, 1, 0), True),
 ((0, 0, 1, 0), (1, 0, 1, 1), True),
 ((0, 0, 1, 0), (1, 1, 0, 1), False),
 ((0, 0, 1, 0), (1, 1, 1, 0), True),
 ((0, 0, 1, 0), (1, 1, 1, 1), False),
 ((0, 1, 0, 0), (0, 0, 0, 0), False),
 ((0, 1, 0, 0), (0, 0, 0, 1), False),
 ((0, 1, 0, 0), (0, 0, 1, 0), False),
 ((0, 1, 0, 0), (0, 1, 0, 1), False),
 ((0, 1, 0, 0), (1, 0, 1, 0), False),
 ((0, 1, 0, 0), (1, 0, 1, 1), False),
 ((0, 1, 0, 0), (1, 1, 0, 1), True),
 ((0, 1, 0, 0), (1, 1, 1, 0), True),
 ((0, 1, 0, 0), (1, 1, 1, 1), False),
 ((0, 1, 0, 1), (0, 0, 0, 0), False),
 ((0, 1, 0, 1), (0, 0, 0, 1), False),
 ((0, 1, 0, 1), (0, 0, 1, 0), False),
 ((0, 1, 0, 1), (0, 1, 0, 0), False),
 ((0, 1, 0, 1), (1, 0, 1, 0), False),
 ((0, 1, 0, 1), (1, 0, 1, 1), False),
 ((0, 1, 0, 1), (1, 1, 0, 1), True),
 ((0, 1, 0, 1), (1, 1, 1, 0), False),
 ((0, 1, 0, 1), (1, 1, 1, 1), True),
 ((1, 0, 1, 0), (0, 0, 0, 0), True),
 ((1, 0, 1, 0), (0, 0, 0, 1), False),
 ((1, 0, 1, 0), (0, 0, 1, 0), True),
 ((1, 0, 1, 0), (0, 1, 0, 0), False),
 ((1, 0, 1, 0), (0, 1, 0, 1), False),
 ((1, 0, 1, 0), (1, 0, 1, 1), False),
 ((1, 0, 1, 0), (1, 1, 0, 1), False),
 ((1, 0, 1, 0), (1, 1, 1, 0), False),
 ((1, 0, 1, 0), (1, 1, 1, 1), False),
 ((1, 0, 1, 1), (0, 0, 0, 0), False),
 ((1, 0, 1, 1), (0, 0, 0, 1), True),
 ((1, 0, 1, 1), (0, 0, 1, 0), True),
 ((1, 0, 1, 1), (0, 1, 0, 0), False),
 ((1, 0, 1, 1), (0, 1, 0, 1), False),
 ((1, 0, 1, 1), (1, 0, 1, 0), False),
 ((1, 0, 1, 1), (1, 1, 0, 1), False),
 ((1, 0, 1, 1), (1, 1, 1, 0), False),
 ((1, 0, 1, 1), (1, 1, 1, 1), False),
 ((1, 1, 0, 1), (0, 0, 0, 0), False),
 ((1, 1, 0, 1), (0, 0, 0, 1), True),
 ((1, 1, 0, 1), (0, 0, 1, 0), False),
 ((1, 1, 0, 1), (0, 1, 0, 0), True),
 ((1, 1, 0, 1), (0, 1, 0, 1), True),
 ((1, 1, 0, 1), (1, 0, 1, 0), False),
 ((1, 1, 0, 1), (1, 0, 1, 1), False),
 ((1, 1, 0, 1), (1, 1, 1, 0), False),
 ((1, 1, 0, 1), (1, 1, 1, 1), False),
 ((1, 1, 1, 0), (0, 0, 0, 0), False),
 ((1, 1, 1, 0), (0, 0, 0, 1), False),
 ((1, 1, 1, 0), (0, 0, 1, 0), True),
 ((1, 1, 1, 0), (0, 1, 0, 0), True),
 ((1, 1, 1, 0), (0, 1, 0, 1), False),
 ((1, 1, 1, 0), (1, 0, 1, 0), False),
 ((1, 1, 1, 0), (1, 0, 1, 1), False),
 ((1, 1, 1, 0), (1, 1, 0, 1), False),
 ((1, 1, 1, 0), (1, 1, 1, 1), False),
 ((1, 1, 1, 1), (0, 0, 0, 0), False),
 ((1, 1, 1, 1), (0, 0, 0, 1), False),
 ((1, 1, 1, 1), (0, 0, 1, 0), False),
 ((1, 1, 1, 1), (0, 1, 0, 0), False),
 ((1, 1, 1, 1), (0, 1, 0, 1), True),
 ((1, 1, 1, 1), (1, 0, 1, 0), False),
 ((1, 1, 1, 1), (1, 0, 1, 1), False),
 ((1, 1, 1, 1), (1, 1, 0, 1), False),
 ((1, 1, 1, 1), (1, 1, 1, 0), False)]