Python 2.7 如何提取元组中';s重复作为字典的键,并将元组的第二部分作为值?
我对Python和Qgis非常陌生,现在我只运行脚本,但我的最终目标是创建一个插件 以下是我遇到问题的代码部分:Python 2.7 如何提取元组中';s重复作为字典的键,并将元组的第二部分作为值?,python-2.7,dictionary,duplicates,qgis,Python 2.7,Dictionary,Duplicates,Qgis,我对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() c
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
这是我的结果:
[3555385,6.689066666666.68906e+66E+66666E+06]3.1L:[[[355 238,6.686.68909e+06)、(353535353407,6.687.6.687.6.687.6.68907.6.686.686.6890666E+6+6+6 6 6 6.6.6.6.666666E+6+6+6+6 6 6 6 6 6 6.6 6 6 6.6.6+6+6 6 6 6 6 6 6 6 6 6.6 6 6 6.686+6+6 6.6 6 6 6 6 6 6 6 6 6.689.689.6+6+6+6.6 6.6 6 6.689.6+6+6+6.689 9 9.6 6 6 6 6 6 6 6 6 6 6.6 6 6 6[(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.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
也许这就是你需要的
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
请参阅此代码:
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
非常接近!你的方法确实把正确的点作为关键!非常感谢。[0]的作用是什么?代码的哪一部分规定了要附加到当前键的值?实际上我使用的是您的旧代码,您的编辑使情况变得更好:现在正确的键附加到正确的值,但每个键都应该附加多个值;有了这个片段,它只更新了其中一个。也许你可以这样试试?很抱歉,在以前的版本中,我输入了“差”而不是“交叉点”,这是错误的OK great(感谢#),所以现在列表中有两个正确的值。但它仍然缺少一些值。事实上(也许我应该把这个放在正文中)每个关键点至少有三个值点,这意味着可能有五个或六个值点(我们处理的是街道交叉口)。例如,值355364由3L、4L、5L和6L共享,但355385仅由0L、7L、4L共享。我认为现在它可以有尽可能多的值,在我的测试中它是准确的,4个值、3个值和3个值。非常接近!你的方法确实把正确的点作为关键!非常感谢。[0]的作用是什么?代码的哪一部分规定了要附加到当前键的值?实际上我使用的是您的旧代码,您的编辑使情况变得更好:现在正确的键附加到正确的值,但每个键都应该附加多个值;有了这个片段,它只更新了其中一个。也许你可以这样试试?很抱歉,在以前的版本中,我输入了“差”而不是“交叉点”,这是错误的OK great(感谢#),所以现在列表中有两个正确的值。但它仍然缺少一些值。事实上(也许我应该把这个放在正文中)每个关键点至少有三个值点,这意味着可能有五个或六个值点(我们处理的是街道交叉口)。例如,值355364由3L、4L、5L和6L共享,但355385仅由0L、7L、4L共享。我认为现在它可以有尽可能多的值,在我的测试中,它是准确的,4个值、3个值和3个值。嗨,感谢您的回复。当打印b时,我得到值355277,它不应该是键的一部分。可能是因为这是第一个迭代的术语?嗨,谢谢你的回复。当打印b时,我得到值355277,它不应该是键的一部分。可能是因为这是第一个迭代的术语?Re您最近删除的问题:Re您最近删除的问题: