如何提取与字典重复的元组的一部分,并将元组的第二部分作为值? [英] 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?
问题描述
这是代码的一部分我有问题:
import math
layer = qgis.utils .iface.activeLayer()
iter = layer.getFeatures()
dict = {}
#iterate over features
为iter中的功能:
#print feature.id()
geom = feature.geometry()
coord = geom.asPolyline()
points = geom.asPolyline()
#get Endpoints
first = points [0]
last = points [-1]
#Assemble特征
dict [feature.id()] = [first,last]
print dict
这是我的结果:
{0L:[( 355277,6.68901e + 06),(355385,6.68906e + 06)],1L:[(355238,6.68909e + 06),(355340,6.68915e + 06)],2L:[(355340,6.68915e + 06 ),3L:[(355340,6.68915e + 06),(355364,6.6891e + 06)],4L:[(355364,6.6891e + 06),(355385,6.68906),(355452,6.68921e + 06) E + 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)]}
如你所见,很多行都有一个共同的终点:(355385,6.68906e +06)由7L,4L和0L共享。
我想创建一个新的字典,获取共享点作为关键字,并将第二个点作为值。
例如:{(355385,6.68906e + 06):[(355277,6.68901e + 06),(355364,6.6891e + 06),(355501,6.68912 e + 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现在我们知道,同时在两个
#集中都存在一个元组,但是这一个将是新字典
#的关键,我们需要从集合中的第二个元组成为这个值新的键
#所以我们可以从集合中减去键元组来获取另一个元组
d = set(dict [i])。difference(c)
#现在我们需要得到元组通过执行列表(c)从集合
#返回,我们得到列表
#,我们的元组是列表中的第一个元素,因此列表(c)[0]
c = list )[0]
词典[c] =列表(d)[0]
else:pass
此代码只将一个元组附加到字典中的键。如果你想为每个键多个值,你可以修改它,以便每个键都有一个值列表,这可以通过简单地修改:
#some_value不能是一个集合,它可以通过c = list(c)[0]
key = some_value
dictionary.setdefault(key,[])
词典[key] .append(value)
所以,正确的答案是:
dictionary = {}
for a in a:
for j in a:
c = set(a [i])。intersection(set(a [j]))
if len(c)== 1:
d = set(a [i] (c)
c = list(c)[0]
value = list(d)[0]
如果字典中的c和值不在字典[c]中:
dictionary [c] .append(value)
elif c不在字典中:
dictionary.setdefault(c,[])
词典[c] .append(value)
else:pass
I'm pretty new to Python and Qgis, right now I'm just running scripts but I my end-goal is to create a plugin.
Here's the part of the code I'm having problems with:
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
This is my result : {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)]}
As you can see, many of the lines have a common endpoint:(355385,6.68906e+06) is shared by 7L, 4L and 0L for example.
I would like to create a new dictionary, fetching the shared points as a key, and having the second points as value.
eg : {(355385,6.68906e+06):[(355277,6.68901e+06), (355364,6.6891e+06), (355501,6.68912e+06)]}
I have been looking though list comprehension tutorials, but without much success: most people are looking to delete the duplicates, whereas I would like use them as keys (with unique IDs). Am I correct in thinking set() would still be useful?
I would be very grateful for any help, thanks in advance.
Maybe this is what you need?
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
This code attaches only one tuple to the key in dictionary. If you want multiple values for each key, you can modify it so that each key would have a list of values, this can be done by simply modifying:
# 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)
So, the correct answer would be:
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
这篇关于如何提取与字典重复的元组的一部分,并将元组的第二部分作为值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!