proper use of list comprehensions - python

后端 未结 5 916
予麋鹿
予麋鹿 2020-12-10 04:32

Normally, list comprehensions are used to derive a new list from an existing list. Eg:

>>> a = [1, 2, 3, 4, 5]
>>> [i for i in a if i >          


        
5条回答
  •  再見小時候
    2020-12-10 05:17

    In the example you give it would make the most sense to do:

    b = [i for i in a]
    

    if for some reason you wanted to create b. In general, there is some common sense that must be employed. If using a comprehension makes your code unreadable, don't use it. Otherwise go for it.

提交回复
热议问题