我如何通过划分 python 中的元组列表来创建新的整个列表
how I create a new whole list by dividing a list of tuples in python
我有一个这样的列表:
[[(62, 57),
(59, 23),
(49, 5),
(138, 10),
(37, 52),
(145, 3),
(13, -1),
(30, 5),
(15, 4)],
[(31, 1),
(74, 0),
(85, 8),
(85, 25),
(45, 4),
(38, 0),
(37, 0),
(16, 0),
(102, -1)]]
我需要用括号之间的数字除以创建另一个列表,但我不知道该怎么做。
我需要这样的东西:
[1.08, 2.56, 9.8, and so on]
ps。如果其中一个数字是 0 过滤它。
使用列表理解:
my_list = [[(62, 57), (59, 23), (49, 5), (138, 10), (37, 52), (145, 3), (13, -1), (30, 5), (15, 4)],
[(31, 1), (74, 0), (85, 8), (85, 25), (45, 4), (38, 0), (37, 0), (16, 0), (102, -1)]]
[[a/b for a,b in l if b] for l in my_list]
输出:
[[1.087719298245614, 2.5652173913043477, 9.8, 13.8, 0.7115384615384616, 48.333333333333336, -13.0, 6.0, 3.75],
[31.0, 10.625, 3.4, 11.25, -102.0]]
注意。如果要确保两个数字都不为零,请使用 if a and b
我有一个这样的列表:
[[(62, 57),
(59, 23),
(49, 5),
(138, 10),
(37, 52),
(145, 3),
(13, -1),
(30, 5),
(15, 4)],
[(31, 1),
(74, 0),
(85, 8),
(85, 25),
(45, 4),
(38, 0),
(37, 0),
(16, 0),
(102, -1)]]
我需要用括号之间的数字除以创建另一个列表,但我不知道该怎么做。
我需要这样的东西:
[1.08, 2.56, 9.8, and so on]
ps。如果其中一个数字是 0 过滤它。
使用列表理解:
my_list = [[(62, 57), (59, 23), (49, 5), (138, 10), (37, 52), (145, 3), (13, -1), (30, 5), (15, 4)],
[(31, 1), (74, 0), (85, 8), (85, 25), (45, 4), (38, 0), (37, 0), (16, 0), (102, -1)]]
[[a/b for a,b in l if b] for l in my_list]
输出:
[[1.087719298245614, 2.5652173913043477, 9.8, 13.8, 0.7115384615384616, 48.333333333333336, -13.0, 6.0, 3.75],
[31.0, 10.625, 3.4, 11.25, -102.0]]
注意。如果要确保两个数字都不为零,请使用 if a and b