Python 交换列表中的第一个和最后一个项目

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19666772/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 14:17:55  来源:igfitidea点击:

Swapping first and last items in a list

pythonlistpython-2.7python-3.xswap

提问by user2891763

How can I go about swapping numbers in a given list?

如何交换给定列表中的数字?

For example:

例如:

list = [5,6,7,10,11,12]

I would like to swap 12with 5.

我想125.

Is there an built-in Python function that can allow me to do that?

是否有内置的 Python 函数可以让我这样做?

回答by Ashwini Chaudhary

>>> lis = [5,6,7,10,11,12]
>>> lis[0], lis[-1] = lis[-1], lis[0]
>>> lis
[12, 6, 7, 10, 11, 5]

Order of evaluationof the above expression:

上述表达式的求值顺序

expr3, expr4 = expr1, expr2

First items on RHS are collected in a tuple, and then that tuple is unpackedand assigned to the items on the LHS.

RHS 上的第一个项目被收集在一个元组中,然后该元组被解包并分配给 LHS 上的项目。

>>> lis = [5,6,7,10,11,12]
>>> tup = lis[-1], lis[0]
>>> tup
(12, 5)
>>> lis[0], lis[-1] = tup
>>> lis
[12, 6, 7, 10, 11, 5]

回答by jramirez

Use the index of the number you want to change.

使用您要更改的号码的索引。

In [38]: t = [5,6,7,10,11,12]

In [40]: index5 = t.index(5) # grab the index of the first element that equals 5

In [41]: index12 = t.index(12) # grab the index of the first element that equals 12

In [42]: t[index5], t[index12] = 12, 5 # swap the values

In [44]: t
Out[44]: [12, 6, 7, 10, 11, 5]

Then you can make a quick swapping function

然后你可以做一个快速交换功能

def swapNumbersInList( listOfNums, numA, numB ):
    indexA = listOfNums.index(numA)
    indexB = listOfNums.index(numB)

    listOfNums[indexA], listOfNums[indexB] = numB, numA

# calling the function
swapNumbersInList([5,6,7,10,11,12], 5, 12)

回答by Don

Another way (not so cute):

另一种方式(不那么可爱):

mylist   = [5, 6, 7, 10, 11, 12]
first_el = mylist.pop(0)   # first_el = 5, mylist = [6, 7, 10, 11, 12]
last_el  = mylist.pop(-1)  # last_el = 12, mylist = [6, 7, 10, 11]
mylist.insert(0, last_el)  # mylist = [12, 6, 7, 10, 11]
mylist.append(first_el)    # mylist = [12, 6, 7, 10, 11, 5]

回答by Ashif Abdulrahman

you can swap using this code,

您可以使用此代码进行交换,

list[0],list[-1] = list[-1],list[0]

回答by Harrison Croaker

array = [5,2,3,6,1,12]
temp = ''
lastvalue = 5

temp = array[0]
array[0] = array[lastvalue]
array[lastvalue] = temp

print(array)

Hope this helps :)

希望这可以帮助 :)

回答by aghd

You can use "*" operator.

您可以使用“*”运算符。

my_list = [1,2,3,4,5,6,7,8,9]
a, *middle, b = my_list
my_new_list = [b, *middle, a]
my_list
[1, 2, 3, 4, 5, 6, 7, 8, 9]
my_new_list
[9, 2, 3, 4, 5, 6, 7, 8, 1]

Read herefor more information.

阅读此处了解更多信息。

回答by iBee

This is what finally worked for me.

这就是最终对我有用的方法。

def swap(the_list):
    temp = the_list[0]
    the_list[0] = the_list[-1]
    the_list[-1] = temp
    return the_list