[英]How to poplulate a tuple list, preserving its structure in Python using for loop?
我有一个清单
form.country.choices = [('1', '1'), ('2', '2')]
我需要填充此列表,并将其结构保留到 100。
IE
form.country.choices = [('1', '1'), ('2', '2'), ('3', '3').....('100', '100')]
我试过了
newlist = []
for x in range(10):
innerlist = []
for y in range(1,100):
innerlist.append(y)
newlist.append(innerlist)
print(newlist)
这行不通。
我想保留结构。 请帮忙
您可以使用列表推导来做到这一点,迭代从 1 到 100 的字符串化数字:
res = [(n, n) for n in map(str, range(1,101))]
Output:
[('1', '1'), ('2', '2'), ('3', '3'), ('4', '4'), ..., ('98', '98'), ('99', '99'), ('100', '100')]
这可能对您有所帮助:
l=[]
for i in range(1,101):
l.append((str(i),str(i)))
Output 将是:
[('1', '1'), ('2', '2'), ('3', '3').....('100', '100')]
new_list = [(str(i),str(i)) for i in range(0,101)]
output
[('1', '1'), ('2', '2'), ('3', '3').....('100', '100')]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.