How to create tuple with a loop in python

后端 未结 6 1287
梦如初夏
梦如初夏 2021-02-13 17:24

I want to create this tuple:

a=(1,1,1),(2,2,2),(3,3,3),(4,4,4),(5,5,5),(6,6,6),(7,7,7),(8,8,8),(9,9,9)

I tried with this

a=1,1,         


        
6条回答
  •  甜味超标
    2021-02-13 18:25

    Use an extra comma in your tuples, and just join:

    a = ((1,1,1),)
    for i in range(2,10):
        a = a + ((i,i,i),)
    

    Edit: Adapting juanpa.arrivillaga's comment, if you want to stick with a loop, this is the right solution:

    a = [(1,1,1)]
    for i in range (2,10):
        a.append((i,i,i))
    a = tuple(a)   
    

提交回复
热议问题