split list into 2 lists corresponding to every other element

后端 未结 3 1187
广开言路
广开言路 2021-01-12 03:23

I know there are many chunky ways to do this, but I am looking for a slick pythonic way to accomplish the following. Given a list of numbers:

a = [0,1,2,3,4,         


        
3条回答
  •  谎友^
    谎友^ (楼主)
    2021-01-12 03:39

    Try This :

    a = [0,1,2,3,4,5,6,7,8,9]
    >>> b=[i for x,i in enumerate(a) if x%2==1]
    >>> c=[i for x,i in enumerate(a) if x%2==0]
    >>> b
      [1, 3, 5, 7, 9]
    >>> c
      [0, 2, 4, 6, 8]
    

提交回复
热议问题