[英]How do I make this list of lists into a list of tuples?
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. 因为“ 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?
那么如何将其转换为元组列表?
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)]
输出:
[(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)]
>>> 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
; c_list
中c_list
3个元素(三角形边缘)的每个列表L
; L
with itertools.combinations
; itertools.combinations
为每个L
创建边的组合; 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: Yeils输出:
[(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)]
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'>
...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.