简体   繁体   English

从这个Python元组列表中创建一个新列表?

[英]Make a new list from this Python list of tuples?

The dimensions of an a4 piece of paper are 8.5x11 inches, and the number of pixels which can fit inside these inches increases with dots per inch. 一张a4纸的尺寸为8.5x11英寸,这些英寸内可容纳的像素数随每英寸点数的增加而增加。

I made a Python list of tuples which expresses this as, using 我制作了一个Python元组列表,将其表示为

>>> [(i,8.5*i,11*i) for i in range(18)]

which gives 这使

[(0, 0.0, 0), (1, 8.5, 11), (2, 17.0, 22), (3, 25.5, 33), (4, 34.0, 44), (5, 42.5, 55), (6, 51.0, 66), (7, 59.5, 77), (8, 68.0, 88), (9, 76.5, 99), (10, 85.0, 110), (11, 93.5, 121), (12, 102.0, 132), (13, 110.5, 143), (14, 119.0, 154), (15, 127.5, 165), (16, 136.0, 176), (17, 144.5, 187)]

I then want to make a new list that contains only the factor by which the quantities in the first list grow, namely the sequence {0..18}. 然后,我想创建一个仅包含第一个列表中数量增长所依据的因子的新列表,即序列{0..18}。

But I want to do this by performing some operation on the first list. 但是我想通过对第一个列表执行一些操作来做到这一点。 How can I do it? 我该怎么做?

选择每个元组的第一个元素:

[t[0] for t in originallist]

Or use the generator form to avoid materializing a list before you need it: 或使用生成器表单来避免在需要列表之前实现一个列表:

gen = (t[0] for t in originallist)

and when you need the list: 以及何时需要列表:

list(gen)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM