I have a 2d list of route numbers and coordinates that I've produced from out of a KML file. The order is 'routenumber','coordinates' e.g.:
[['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']]
For each route in the list I'm trying to find routes with the same coordinates e.g.
Output:
54.939206,25.5249,0.0 is seen in 911, 917
54.93920,25.52,0.0 is seen in 90, 97, 914
so at a simple level something that can identify duplicates in the second inner? 'column' of
[['1','10'],['2','50'],['3','10'],['4','0'],['5','50']]
and provide me with:
Output:
10 is seen in 1, 3
50 is seen in 2, 5
I have tried the following but am now tying myself in knots and wondering if I should be using a dictionary instead. I'm fairly new to python as you can probably tell. Nothing that I can find on the forums here talks about comparing a specific item at a position in a 2d list (which makes me wonder about using a dictionary)
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)
Any help would be greatly appreciated.
UPDATE: Thanks to sushanth for the code. I have adapted it slightly for readability/noobs like me to understand what's going on.
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)}")
Here is a solution you can give it a try,
Declare a dict
considering coordinates as the key
& route as list of values
for the dict.
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
...