如何使用 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])
输出
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 数组进行切片
我有一个列表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])
输出
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 数组进行切片