Better way to swap elements in a list?

后端 未结 14 1250
臣服心动
臣服心动 2020-12-23 13:32

I have a bunch of lists that look like this one:

l = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

I want to swap elements as follows:

fi         


        
相关标签:
14条回答
  • 2020-12-23 14:02

    An(other) alternative:

    final_l = list() # make an empty list
    for i in range(len(l)): # for as many items there are in the original list
        if i % 2 == 0: # if the item is even
            final_l.append(l[i+1]) # make this item in the new list equal to the next in the original list
        else: # else, so when the item is uneven
            final_l.append(l[i-1]) # make this item in the new list equal to the previous in the original list
    

    This assumes that the original list has an even number of items. If not, a try-except can be added:

    final_l = list()
    for i in range(len(l)):
        if i % 2 == 0:
            try: # try if we can add the next item
                final_l.append(l[i+1])
            except:  # if we can't (because i+1 doesnt exist), add the current item
                final_l.append(l[i])
        else:
                final_l.append(l[i-1])
    
    0 讨论(0)
  • 2020-12-23 14:02
    newList = [(x[2*i+1], x[2*i]) for i in range(0, len(x)/2)]
    

    Now find a way to unzip the tuples. I won't do all of your homework.

    0 讨论(0)
  • 2020-12-23 14:07

    Here a single list comprehension that does the trick:

    In [1]: l = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
    
    In [2]: [l[i^1] for i in range(len(l))]
    Out[2]: [2, 1, 4, 3, 6, 5, 8, 7, 10, 9]
    

    The key to understanding it is the following demonstration of how it permutes the list indices:

    In [3]: [i^1 for i in range(10)]
    Out[3]: [1, 0, 3, 2, 5, 4, 7, 6, 9, 8]
    

    The ^ is the exclusive or operator. All that i^1 does is flip the least-significant bit of i, effectively swapping 0 with 1, 2 with 3 and so on.

    0 讨论(0)
  • 2020-12-23 14:09

    You can use the pairwise iteration and chaining to flatten the list:

    >>> from itertools import chain
    >>>
    >>> list(chain(*zip(l[1::2], l[0::2])))
    [2, 1, 4, 3, 6, 5, 8, 7, 10, 9]
    

    Or, you can use the itertools.chain.from_iterable() to avoid the extra unpacking:

    >>> list(chain.from_iterable(zip(l[1::2], l[0::2])))
    [2, 1, 4, 3, 6, 5, 8, 7, 10, 9]
    
    0 讨论(0)
  • 2020-12-23 14:09

    One of the possible answer using chain and list comprehension

    >>> l = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
    >>> list(chain([(l[2*i+1], l[2*i]) for i in range(0, len(l)/2)]))
    [(2, 1), (4, 3), (6, 5), (8, 7), (10, 9)]
    
    0 讨论(0)
  • 2020-12-23 14:09

    New to stack overflow. Please free to leave a comment or feedback on this solution. swap = [2, 1, 4, 3, 5]

    lst = []
    for index in range(len(swap)):
            if index%2 == 0 and index < len(swap)-1:
                swap[index],swap[index+1]  = swap[index+1],swap[index]
            lst.append(swap[index])
    print(lst)
    
    
    out = [1, 2, 3, 4, 5]
    
    0 讨论(0)
提交回复
热议问题