How to create tuple with a loop in python

后端 未结 6 1299
梦如初夏
梦如初夏 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:06

    A tuple is an immutable list. This means that, once you create a tuple, it cannot be modified. Read more about tuples and other sequential data types here.


    So, if you really need to change a tuple during run time:

    1. Convert the tuple into a list
    2. Make the necessary changes to the list
    3. Convert the list back to a tuple

    or

    1. Create a list
    2. Modify the list
    3. Convert the list into a tuple

    So, in your case:

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

提交回复
热议问题