How to create tuple with a loop in python

后端 未结 6 2311
萌比男神i
萌比男神i 2021-02-13 17:59

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:13

    If I were to imitate something like this, I would have done it in the following way:

    a = tuple((n,n,n) for n in range(1,10))
    print(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))
    

    This is the most simple and pythonic way to do this specific job.

提交回复
热议问题