List comprehension on a nested list?

后端 未结 12 1200
情话喂你
情话喂你 2020-11-22 07:57

I have this nested list:

l = [[\'40\', \'20\', \'10\', \'30\'], [\'20\', \'20\', \'20\', \'20\', \'20\', \'30\', \'20\'], [\'30\', \'20\', \'30\', \'50\', \'         


        
12条回答
  •  清歌不尽
    2020-11-22 08:29

        deck = [] 
        for rank in ranks:
            for suit in suits:
                deck.append(('%s%s')%(rank, suit))
    

    This can be achieved using list comprehension:

    [deck.append((rank,suit)) for suit in suits for rank in ranks ]
    

提交回复
热议问题