Python for and if on one line

后端 未结 6 2052
情书的邮戳
情书的邮戳 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:28

    Found this one:

    [x for (i,x) in enumerate(my_list) if my_list[i] == "two"]
    

    Will print:

    ["two"]
    

提交回复
热议问题