在python中查找对称对

时间:2016-01-14 17:02:49

标签: python list iteration tuples python-2.x

我有以下代码来查找和打印来自给定输入的所有对称对。

'''
 Given a list of number pairs.
 If pair(i,j) exist, and pair(j,i) exist report all such pairs.
'''

def find_all_symmetric_pairs(inp_dic):

    for key in inp_dic:
        val = inp_dic[key]

        if inp_dic[val] == key:
            return key,val
    return -1,-1


inp_dic = {'1':'3','2':'6','3':'5','7':'4','5':'3','8':'7'}

print type(inp_dic)
key,value = find_all_symmetric_pairs(inp_dic)

print "key:"+str(key)
print "value:"+str(value)

输出:

 key:3
 value:5

但如果我将输入更改为

inp_dic = {(1,3),(2,6),(3,5),(7,4),(5,3),(8,7)}

inp_dic = {{1,3},{2,6},{3,5},{7,4},{5,3},{8,7}}

迭代很困难。我如何实现同样的目标?

5 个答案:

答案 0 :(得分:4)

这似乎是一个有用的解决方案:

d = {'1':'3','2':'6','3':'5','7':'4','5':'3','8':'7'}
pairs = [(key, value) for key, value in d.items()]
answer = [(x, y) for (x, y) in pairs if (y, x) in pairs]
print(answer)

<强>输出

[('3', '5'), ('5', '3')]

如果我们将pairs设为set而不是list,也可能会更快:

d = {'1':'3','2':'6','3':'5','7':'4','5':'3','8':'7'}
pairs = {(key, value) for key, value in d.items()}
answer = [(x, y) for (x, y) in pairs if (y, x) in pairs]
print(answer)

<强>输出

[('3', '5'), ('5', '3')]

答案 1 :(得分:2)

使用生成器https://wiki.python.org/moin/Generators

def find_all_symmetric_pairs(inp_dic):

    for key in inp_dic:
        val = inp_dic[key]

        if inp_dic[val] == key:
            yield key,val

答案 2 :(得分:1)

好吧,对于一个你正在将inp_dic的类型更改为一组元组或一组集合 - 这显然不应该工作,因为集合是不可用的:

>>> input_set = {{1,3}, {3,3}}
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: unhashable type: 'set'

但你可以像这样迭代一组元组:

input_set = {(1,3), (42,23)}
for a, b in input_set:
    print(a, b)

答案 3 :(得分:1)

您的函数<div class="button"> <div class="button-text"> Very Long Button Text </div> </div> <div class="button"> <div class="button-text scaled"> Very Long Button Text </div> </div>实际上只找到一个对称对。你可以将它变成一个真正迭代所有键的生成器函数:

find_all_symmetric_pairs

或者您可以通过附加到列表来手动生成:

def find_all_symmetric_pairs(inp_dic):

    for key in inp_dic:
        val = inp_dic[key]

        if inp_dic[val] == key:
            yield key,val
    return -1,-1

for key, val in find_all_symmetric_pairs(inp_dic):
    print "key: " + str(key)
    print "value: " + str(val)

请记住,def find_all_symmetric_pairs(inp_dic): keys = [] vals = [] for key in inp_dic: val = inp_dic[key] if inp_dic[val] == key: keys.append(key) vals.append(val) return keys, vals for key, val in zip(*find_all_symmetric_pairs(inp_dic)): print "key: " + str(key) print "value: " + str(val) 会创建一组元组,而inp_dic = {(1,3),(2,6),(3,5),(7,4),(5,3),(8,7)}会尝试创建一组集合,这是非法的,因为集合是可变的,因此不可删除。第一种情况需要比使用字典更复杂的迭代,因为您必须搜索所有元组以获得匹配的键或值。要解决这个问题,您可以使用字典理解轻松转换为字典:

inp_dic = {{1,3},{2,6},{3,5},{7,4},{5,3},{8,7}}

答案 4 :(得分:0)

我通过以下方式解决了问题。因此回答我自己的问题。

def find_all_symmetric_pairs(inp_dic):

    for key in inp_dic.iterkeys():
        val = inp_dic.get(key)

        if inp_dic.get(val) == key:
            yield key,val
    return 


inp_dic = [(1,3),(2,6),(3,5),(7,4),(5,3),(8,7)]

inp_dic = dict(inp_dic)

for key, val in find_all_symmetric_pairs(inp_dic):
print "key: " + str(key)
print "value: " + str(val)