list.extend and list comprehension

后端 未结 6 1785
北恋
北恋 2020-12-10 01:05

When I need to add several identical items to the list I use list.extend:

a = [\'a\', \'b\', \'c\']
a.extend([\'d\']*3)

Result



        
6条回答
  •  醉梦人生
    2020-12-10 01:12

    import operator
    a = [['a',2], ['b',2], ['c',1]]
    nums = [[x[0]]*x[1] for x in a]
    nums = reduce(operator.add, nums)
    

提交回复
热议问题