Create list of single item repeated N times

前端 未结 6 1185
南笙
南笙 2020-11-22 03:57

I want to create a series of lists, all of varying lengths. Each list will contain the same element e, repeated n times (where n = len

6条回答
  •  野性不改
    2020-11-22 04:47

    As others have pointed out, using the * operator for a mutable object duplicates references, so if you change one you change them all. If you want to create independent instances of a mutable object, your xrange syntax is the most Pythonic way to do this. If you are bothered by having a named variable that is never used, you can use the anonymous underscore variable.

    [e for _ in xrange(n)]
    

提交回复
热议问题