Python for and if on one line

后端 未结 6 2079
情书的邮戳
情书的邮戳 2021-01-31 16:51

I have a issue with python.

I make a simple list:

>>> my_list = [\"one\",\"two\",\"three\"]

I want create a \"single line code

6条回答
  •  爱一瞬间的悲伤
    2021-01-31 17:12

    When you perform

    >>> [(i) for i in my_list if i=="two"]
    

    i is iterated through the list my_list. As the list comprehension finishes evaluation, i is assigned to the last item in iteration, which is "three".

提交回复
热议问题