如何提取作为字典键的重复元组的一部分,并将元组的第二部分作为值?

How do I extract part of a tuple that's duplicate as key to a dictionary, and have the second part of the tuple as value?

我是 Python 和 Qgis 的新手,现在我只是 运行 脚本,但我的最终目标是创建一个插件。

这是我遇到问题的部分代码:

import math

layer = qgis.utils.iface.activeLayer()
iter = layer.getFeatures()
dict = {}

#iterate over features
for feature in iter:
    #print feature.id()
    geom = feature.geometry()
    coord = geom.asPolyline()
    points=geom.asPolyline()
#get Endpoints
    first = points[0]
    last = points[-1]

#Assemble Features
dict[feature.id() ]= [first, last]

print dict

这是我的结果: {0L: [(355277,6.68901e+06), (35538​​5,6.68906e+06)], 1L: [(355238,6.68909e+06), (355340,6.68915e+06)], 2L: [(355340 ,6.68915e+06), (355452,6.68921e+06)], 3L: [(355340,6.68915e+06), (355364,6.6891e+06)], 4L: [(355364,6.6891e+06) , (35538​​5,6.68906e+06)], 5L: [(355261,6.68905e+06), (355364,6.6891e+06)], 6L: [(355364,6.6891e+06), (355481,6.68916e +06)], 7L: [(35538​​5,6.68906e+06), (355501,6.68912e+06)]}

如您所见,许多线都有一个共同的端点:(35538​​5,6.68906e+06) 例如由 7L、4L 和 0L 共享。

我想创建一个新字典,获取共享点作为键,并将第二个点作为值。

例如:{(35538​​5,6.68906e+06):[(355277,6.68901e+06), (355364,6.6891e+06), (355501,6.68912e+06)]}

我一直在查看列表理解教程,但收效甚微:大多数人都希望删除重复项,而我想将它们用作键(具有唯一 ID)。我认为 set() 仍然有用吗?

如果有任何帮助,我将不胜感激,在此先感谢。

也许这就是您需要的?

dictionary = {}
for i in dict:
    for j in dict:
        c = set(dict[i]).intersection(set(dict[j]))
        if len(c) == 1:
            # ok, so now we know, that exactly one tuple exists in both
            # sets at the same time, but this one will be the key to new dictionary
            # we need the second tuple from the set to become value for this new key
            # so we can subtract the key-tuple from set to get the other tuple
            d = set(dict[i]).difference(c)
            # Now we need to get tuple back from the set
            # by doing list(c) we get list
            # and our tuple is the first element in the list, thus list(c)[0]
            c = list(c)[0]
            dictionary[c] = list(d)[0]
        else: pass

此代码仅将一个元组附加到字典中的键。如果您希望每个键有多个值,您可以修改它,以便每个键都有一个值列表,这可以通过简单地修改来完成:

# some_value cannot be a set, it can be obtained with c = list(c)[0]
key = some_value
dictionary.setdefault(key, [])
dictionary[key].append(value)

所以,正确答案应该是:

dictionary = {}
for i in a:
        for j in a:
            c = set(a[i]).intersection(set(a[j]))
            if len(c) == 1:
                d = set(a[i]).difference(c)
                c = list(c)[0]
                value = list(d)[0]
                if c in dictionary and value not in dictionary[c]:
                    dictionary[c].append(value)
                elif c not in dictionary:
                    dictionary.setdefault(c, [])
                    dictionary[c].append(value)

            else: pass

查看此代码:

dict={0L: [(355277,6.68901e+06), (355385,6.68906e+06)], 1L: [(355238,6.68909e+06), (355340,6.68915e+06)], 2L: [(355340,6.68915e+06), (355452,6.68921e+06)], 3L: [(355340,6.68915e+06), (355364,6.6891e+06)], 4L: [(355364,6.6891e+06), (355385,6.68906e+06)], 5L: [(355261,6.68905e+06), (355364,6.6891e+06)], 6L: [(355364,6.6891e+06), (355481,6.68916e+06)], 7L: [(355385,6.68906e+06), (355501,6.68912e+06)]}
dictionary = {}
list=[]
for item in dict :
   list.append(dict[0])
   list.append(dict[1])

b = []

[b.append(x) for c in list for x in c if x not in b]
print b # or set(b)
res={}

for elm in b :
   lst=[]
   for item in dict :
       if dict[item][0] == elm :
           lst.append(dict[item][1])
       elif dict[item][1] == elm :
           lst.append(dict[item][0])
   res[elm]=lst

print res