我是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),(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)]}
如您所见,许多线路都有一个共同的端点:(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()仍然有用?
我会非常感谢任何帮助,提前谢谢。
答案 0 :(得分:0)
也许这就是你需要的东西?
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
答案 1 :(得分:0)
请参阅此代码:
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