如何从for循环列表中获取python中的一个列表

时间:2019-09-12 14:24:50

标签: python

我有一个li的列表,我想将此列表的int表示为x并能够在另一个函数中使用x [1],x [2],x [3]。但我无法将它们打印出来以进行循环。

set @string:='11';
select CASE CAST(@string AS SIGNED) when '11' THEN 'yes' ELSE 'no' END as filed;

我希望将这些列表用作此for循环中的x [1],x [2],x [3]。

li =[]
li =[['0', '5', '8', '15', '20'], ['0', '6', '7', '14', '19'], ['0', '8', '10', '12', '18']]
for i in li:
    x=[]
    for j in i:
        m=[int(j)for j in (i)]
    x.append(m)
    print(x)

输出为:

x[1]=[0, 5, 8, 15, 20]
x[2]=[0, 6, 7, 14, 19]
x[3]=[0, 8, 10, 12, 18]

2 个答案:

答案 0 :(得分:-1)

您可以使用列表推导来创建列表,并使用map函数将字符串转换为整数:

x = [list(map(int, element)) for element in li]

答案 1 :(得分:-1)

li =[]
li =[['0', '5', '8', '15', '20'], ['0', '6', '7', '14', '19'], ['0', '8', '10', '12', '18']]
x={}
count = 0
for i in li:
    count += 1
    x[count] = i
print(x)
#output: {1: ['0', '5', '8', '15', '20'], 2: ['0', '6', '7', '14', '19'], 3: ['0', '8', #`enter code here`'10', '12', '18']}

This way you can use
#x[1]=[0, 5, 8, 15, 20]
#x[2]=[0, 6, 7, 14, 19]
#x[3]=[0, 8, 10, 12, 18]


for i in x.keys():
    print(x[i])