Convert a list of strings to a list of tuples in python

后端 未结 3 482
囚心锁ツ
囚心锁ツ 2021-01-19 19:17

I have a list of strings in this format:

[\'5,6,7\', \'8,9,10\']

I would like to convert this into the format:

[(5,6,7), (8         


        
3条回答
  •  面向向阳花
    2021-01-19 19:52

    Your question requires the grouping of elements. Hence, an appropriate solution would be:

    l = ['5','6','7', '8','9','10']
    [(lambda x: tuple(int(e) for e in x))((i,j,k)) for (i, j, k) in zip(l[0::3], l[1::3], l[2::3])]
    

    This outputs:

    [(5, 6, 7), (8, 9, 10)]
    

    As desired.

提交回复
热议问题