Reputation: 13
I'm having a bit of trouble with making this list of lists
triangles= [[0, 30, 31], [2, 32, 38], [3, 24, 46], [4, 14, 27], [10, 18, 48], [12, 21, 35], [15, 39, 45], [17, 32, 38], [19, 32, 38], [21, 43, 44], [29, 43, 44]]
into a list like this:
[(0,30),(30,31),(0,31),(2,32),(32,38),(2,38).. etc]
I have already tried this:
c_list=list(nx.clique.enumerate_all_cliques(G))
triangles=[x for x in c_list if len(x)==3]
for [u,v,w] in triangles:
print((u,v),(v,w),(u,w))
since 'triangles' is a list of nodes that make triangles in a graph and I need the edges so that I can graph the triangles. However, I can't use this code because it doesn't have a type. So how do I turn it into a list of tuples?
Upvotes: 1
Views: 93
Reputation: 376
With a small change you to your code you can have the list of tuples:
triangles= [[0, 30, 31], [2, 32, 38], [3, 24, 46], [4, 14, 27], [10, 18, 48], [12, 21, 35], [15, 39, 45], [17, 32, 38], [19, 32, 38], [21, 43, 44], [29, 43, 44]]
for [u,v,w] in triangles:
element = [(u,v),(v,w),(u,w)]
print(type(element))
for item in element:
print(type(item))
output will be like:
<class 'list'>
<class 'tuple'>
<class 'tuple'>
<class 'tuple'>
<class 'list'>
<class 'tuple'>
<class 'tuple'>
<class 'tuple'>
...
Upvotes: 0
Reputation: 577
triangles = [[0, 30, 31], [2, 32, 38], [3, 24, 46], [4, 14, 27], [10, 18, 48], [12,21, 35], [15, 39, 45], [17, 32, 38], [19, 32, 38], [21, 43, 44], [29, 43, 44]]
edge = []
for points in triangles:
edge.append(tuple(points[0:2]))
edge.append(tuple(points[1:3]))
edge.append(tuple(points[:3:2]))
print(edge)
Yeils output:
[(0, 30), (30, 31), (0, 31), (2, 32), (32, 38), (2, 38), (3, 24), (24, 46), (3, 46), (4, 14), (14, 27), (4, 27), (10, 18), (18, 48), (10, 48), (12, 21), (21, 35), (12, 35), (15, 39), (39, 45), (15, 45), (17, 32), (32, 38), (17, 38), (19, 32), (32, 38), (19, 38), (21, 43), (43, 44), (21, 44), (29, 43), (43, 44), (29, 44)]
Upvotes: 0
Reputation: 8180
>>> c_list = [[0, 30, 31], [2, 32, 38], [3, 24, 46], [4, 14, 27], [10, 18, 48], [12, 21, 35], [15, 39, 45], [17, 32, 38], [19, 32, 38], [21, 43, 44], [29, 43, 44]]
>>> import itertools
>>> [t for L in c_list for t in itertools.combinations(L, 2)]
[(0, 30), (0, 31), (30, 31), (2, 32), (2, 38), (32, 38), (3, 24), (3, 46), (24, 46), (4, 14), (4, 27), (14, 27), (10, 18), (10, 48), (18, 48), (12, 21), (12, 35), (21, 35), (15, 39), (15, 45), (39, 45), (17, 32), (17, 38), (32, 38), (19, 32), (19, 38), (32, 38), (21, 43), (21, 44), (43, 44), (29, 43), (29, 44), (43, 44)]
The list comprehension is simple:
L
of 3 elements (the triangle edges) in c_list
;L
with itertools.combinations
;Upvotes: 2
Reputation: 1243
triangles = [[0, 30, 31], [2, 32, 38], [3, 24, 46], [4, 14, 27], [10, 18, 48], [12, 21, 35], [15, 39, 45], [17, 32, 38], [19, 32, 38], [21, 43, 44], [29, 43, 44]]
y = list()
for i in triangles:
y.append((i[0], i[1]))
y.append((i[1], i[2]))
y.append((i[0], i[2]))
print(y)
output: [(0, 30), (30, 31), (0, 31), (2, 32), (32, 38), (2, 38), (3, 24), (24, 46), (3, 46), (4, 14), (14, 27), (4, 27), (10, 18), (18, 48), (10, 48), (12, 21), (21, 35), (12, 35), (15, 39), (39, 45), (15, 45), (17, 32), (32, 38), (17, 38), (19, 32), (32, 38), (19, 38), (21, 43), (43, 44), (21, 44), (29, 43), (43, 44), (29, 44)]
Upvotes: 3