繁体   English   中英

如何使用 for 循环从 python 中的另一个列表创建一个包含特定字符串的列表?

[英]How to create a list with perticular strings from another list in python using for loop?

我有一个列表 Class“state_wise_data”。

列表是

[
array(['Andaman and Nicobar Islands', 5201, 5050, 62, 89,'12-04-2021 22.58'], dtype=object), array(['Andhra Pradesh', 928664, 898238, 7311, 23115, '12-04-2021 20.49'], dtype=object), 
....
]

我从中提取了第一个值。

for value in state_wise_data:
     print(value[0])

Output

Andaman and Nicobar Islands
Andhra Pradesh
.
.

现在我想以这种特殊格式创建一个列表

form.country.choices = [('Andaman and Nicobar Islands', 'Andaman and Nicobar Islands'), (' Andhra Pradesh', ' Andhra Pradesh'),.....('n', 'n')]

我试过列表理解......

例如:

res = [(n, n) for n in map(str, range(1,10))]

哪个会产生

[('1', '1'), ('2', '2'), ('3', '3'), ('4', '4'), ..., ('10', '10')]

同样,我能得到

[('Andaman and Nicobar Islands', 'Andaman and Nicobar Islands'), (' Andhra Pradesh', ' Andhra Pradesh'),.....('n', 'n')]

请帮忙?

它就像ABC一样简单。

lists = [('nothing','your'),('everything','their'),('something','our')]

[(item[0],item[0]) for item in lists]

为了使其更快,您可以使用 numpy 并立即切片 numpy 阵列

暂无
暂无

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

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