如何以此格式向列表中添加值

时间:2017-10-24 21:58:42

标签: python-3.x arraylist

如何以下列格式输入值,因为基于此,我需要执行更多操作

for i in range(4):
   for j in range(10):
       if count < tickets:
          list[i][j] = loginId 
          #what to do instead of this statement#what is expect is, values should be assigned to each element in the list in this way how to achieve this
       else:
           break

我应该使用什么而不是list[i][j]来将所有值分配给列表。

2 个答案:

答案 0 :(得分:0)

完整的计划问题

def fun():

    check=6
    loginID=1234
    count=0
    a=[[None]*5]*4
    for i in range(2):
        for j in range(2):
            if count<check:
                a[i][j]=loginID
                count+=1
            else:
                break
                print(a)

fun()

输出

  

[[1234,1234],[1234,1234]]

我觉得难以为矩阵赋值。请以这种格式为我提供解决方案

答案 1 :(得分:0)

根据您要求的输出,我修改了代码

def fun():
    check=6
    loginID=1234
    count=0
    a=[]
    for i in range(2):
        a.append([])
        for j in range(2):
            if count<check:
                a[i].insert(j, loginID)
                count+=1
            else:
                print(a)
                break
fun()

您可以修改for循环范围中的值以进行更多自定义。

修改:以矩阵形式打印上面的列表a

for p in a:
    for q in p:
        print(q, end=' ')
    print()