python操作列表的方法
(Multiple assignment trick:)
person1 = ['John', 'Male', '35']
person1 = ['John', 'Male', '35']
Suppose we want to assign the contents of the above list to three different variables, namely, name
, gender
and age
.
假设我们要将上述列表的内容分配给三个不同的变量,即name
, gender
和age
。
One time-consuming way to do it would be to do it individually:
一种耗时的方法是单独进行:
name = person1[0]
gender = person1[1]
age = person1[2]
But instead, we ought to be using Python’s multiple assignment solution:
但是,相反,我们应该使用Python的多重分配解决方案:
name, gender, age = person1
This automatically assigns each variable with the corresponding value from the list.
这将自动为列表中的每个变量分配相应的值。
We can also use it assign multiple the values to multiple variables in just one line of code.
我们还可以使用它在一行代码中将多个值分配给多个变量。
name, gender, age = ‘Jen’, ‘Female’, 21
This technique is also used to swap the value of variables without the need of an extra temp
variable
此技术还用于交换变量的值,而无需额外的temp
变量
a = 'Hello'
b = 'Bye'
a, b = b, a
A list is a mutable data type. It can have values added, deleted or updated.
列表是可变数据类型。 它可以具有添加,删除或更新的值。
(.index() Method:)
This method tells us at what index a given entry is at.
此方法告诉我们给定条目位于什么索引处。
numbers = [3, 10, 12, 21, 57, 2, 5, 3]numbers.index(3)#output: 0
For duplicate entries, the method returns the very first instance of the specified value.
对于重复的条目,该方法返回指定值的第一个实例。
(Adding to a list:)
numbers = [3, 10, 12, 21, 57, 2, 5, 3]numbers.append(22)#output: [3, 10, 12, 21, 57, 2, 5, 3, 22]
The .append()
method adds a value at the end of a list.
.append()
方法在列表的末尾添加一个值。
numbers = [3, 10, 12, 21, 57, 2, 5, 3, 22]numbers.insert(1, 5)#output: [3, 5, 10, 12, 21, 57, 2, 5, 3, 22]
the .insert()
method takes two arguments, the index position and the value to be added. It automatically shifts the other values to accommodate the new entry.
.insert()
方法采用两个参数,即索引位置和要添加的值。 它会自动移动其他值以适应新条目。
(Deleting from a list:)
fruits = ['banana', 'apple', 'blueberries', 'mango', 'grapes', 'spinach']fruits.remove('spinach')#output: ['banana', 'apple', 'blueberries', 'mango', 'grapes']
The .remove()
method takes one argument — the item to be deleted. If it’s not in the list, it’ll throw an error. If there are multiple same name values, then the first instance of that value will be removed. This method will remove the value specified no matter where it is in the list.
.remove()
方法采用一个参数-要删除的项目。 如果不在列表中,则会引发错误。 如果存在多个相同的名称值,则将删除该值的第一个实例。 此方法将删除指定的值,无论它在列表中的什么位置。
fruits = ['banana', 'apple', 'blueberries', 'mango', 'grapes']del fruits[1]#output: ['banana', 'blueberries', 'mango', 'grapes']
The del
method takes the index at which the item to be deleted is.
del
方法获取要删除的项目所在的索引。
(Sorting a list:)
fruits = ['banana', 'blueberries', 'mango', 'grapes']fruits.sort()#output: ['banana', 'blueberries', 'grapes', 'mango']
We can add an argument to the .sort()
method to sort in reverse alphabetical order: fruits.sort(reverse = True)
.
我们可以在.sort()
方法中添加一个参数以按字母相反的顺序进行排序: fruits.sort(reverse = True)
。
When it comes to sorting a mix of numbers and strings, the .sort()
method fails.
如果要对数字和字符串的混合进行排序, .sort()
方法将失败。
.sort()
uses ASCII-betical order which means that capital letters appear first in the sorted list. So ‘Z’ will be placed before ‘a’.
.sort()
使用ASCII字母顺序,表示大写字母在排序列表中排在首位。 因此,“ Z”将放置在“ a”之前。
names = ['joe', 'John', 'Zelda', 'Amy']names.sort()#output: ['Amy', 'John', 'Zelda', 'joe']
This isn’t true sorting. To sort in true alphabetical order:
这不是真正的排序。 要以真实的字母顺序排序:
names.sort(key = str.lower)#output: ['Amy', 'joe', 'John', 'Zelda']
(Conclusion:)
Lists are mutable data types that can be added to, deleted from and changed. The above are few of the basic set of built-in methods that we can use on lists.
列表是可变的数据类型,可以添加,删除和更改。 以上是我们可以在列表上使用的一些基本的内置方法集。
翻译自: https://medium.com/swlh/python-list-methods-and-operations-2f8388193717
python操作列表的方法