在python的2d列表中的位置查找重复项

nnvyjq4y  于 2021-09-29  发布在  Java
关注(0)|答案(2)|浏览(366)

我有一个由kml文件生成的路线编号和坐标的2d列表。顺序为“routenumber”,“坐标”,例如:

[['90', '54.93920,25.52,0.0'], ['93', '37.326,19.39,0.0'], ['94', '-110.67,24.395,0.0'], ['95', '-102.154599,17.915081,0.0'], ['96', '-109.177574,25.537,0.0'], ['97', '54.93920,25.52,0.0'], ['98', '55.319,25.506,0.0'], ['911', '54.939206,25.5249,0.0'], ['914', '54.93920,25.52,0.0'], ['915', '54.9169,25.5031,0.0'], ['916', '55.3709,25.35949,0.0'], ['917', '54.939206,25.5249,0.0'], ['920', '56.4641,25.21,0.0'], ['921', '56.4916,25.376,0.0']]

对于列表中的每条路线,我试图找到具有相同坐标的路线,例如。

Output:
54.939206,25.5249,0.0 is seen in 911, 917
54.93920,25.52,0.0 is seen in 90, 97, 914

因此,在一个简单的层次上,可以在第二个内部识别重复的东西?”第列

[['1','10'],['2','50'],['3','10'],['4','0'],['5','50']]

并向我提供:

Output:
10 is seen in 1, 3
50 is seen in 2, 5

我已经试过下面的方法,但现在正纠结于是否应该用字典来代替。正如您可能知道的,我对python相当陌生。我在论坛上找不到任何关于比较2d列表中某个位置的特定项目的内容(这让我想知道如何使用字典)

for idx, (route,mcoords) in enumerate(endcoordlist):
    #print(idx, route, coords)
    if any(mcoords in coords for idx, (route,coords) in enumerate(endcoordlist)):    
        print(idx, route, coords)

任何帮助都将不胜感激。
更新:感谢sushanth提供的代码。为了便于阅读,我对它做了一些修改,以便于像我这样的傻瓜理解正在发生的事情。

for v in endcoordlist:
    if groups.get(v[1]):
        print(v[0]," is a duplicate with coordinates:",v[1])
        groups[v[1]].append(v[0])

    else:
        groups[v[1]] = [v[0]]
        print(v[1]," is unique")

for k, v in groups.items():
    print(f"{k} seen in {','.join(v)}")
xkftehaa

xkftehaa1#

这里有一个解决方案,你可以试一试,
申报 dict 将坐标视为 key &路线作为 values 为了口述。

groups = {}

for v in input_:
    if groups.get(v[1]):
        groups[v[1]].append(v[0])
    else:
        groups[v[1]] = [v[0]]

for k, v in groups.items():
    print(f"{k} seen in {','.join(v)}")
54.93920,25.52,0.0 seen in 90,97,914
37.326,19.39,0.0 seen in 93
...
2j4z5cfb

2j4z5cfb2#

如果根据坐标对输入列表进行排序,则只需在排序的列表上迭代,查找彼此相等的相邻值。这可能不会提供您想要的输出格式,但它确实演示了如何做到这一点。可能还有更优雅的方式:-

RN = [['90', '54.93920,25.52,0.0'], ['93', '37.326,19.39,0.0'], ['94', '-110.67,24.395,0.0'], ['95', '-102.154599,17.915081,0.0'], ['96', '-109.177574,25.537,0.0'], ['97', '54.93920,25.52,0.0'], ['98', '55.319,25.506,0.0'],
      ['911', '54.939206,25.5249,0.0'], ['914', '54.93920,25.52,0.0'], ['915', '54.9169,25.5031,0.0'], ['916', '55.3709,25.35949,0.0'], ['917', '54.939206,25.5249,0.0'], ['920', '56.4641,25.21,0.0'], ['921', '56.4916,25.376,0.0']]

RNS = sorted(RN, key=lambda x: x[1])
m = set()
for i in range(len(RNS) - 1):
    if RNS[i][1] == RNS[i + 1][1]:
        coord = RNS[i][1]
        m.add(RNS[i][0])
        m.add(RNS[i + 1][0])
    else:
        if len(m) > 0:
            print(coord, m)
            m = set()
if len(m) > 0:
    print(coord, m)

相关问题