Python清单

List is a data structure in Python which contains mutable,ordered sequence of elements.

List是Python中的数据结构,其中包含可变的有序元素序列。

Creating a list:

创建列表:

l1 = [1, 2, 3]
l2 = [‘apple’, ‘banana’, ‘orange’, ‘grapes’, ‘mango’]

List constructor: list() List constructor takes sequence types and converts to list.It is used to convert tuple to list.t1=(1,2,3)l1=list(t1)print (l1)Output:[1, 2, 3]

列表构造函数:list()列表构造函数采用序列类型并转换为列表。用于将元组转换为列表。t1 =(1,2,3)l1 = list(t1)print(l1) 输出: [1、2 ,3]

Accessing elements from list: List indices start from 0.

从列表访问元素:列表索引从0开始。

l1[0] -> 1 (first element in list l1) l2[2] -> orange (third element in list l2) l1[-1] -> 3 (Negative index -1 means starting from the end of the list) l1[-2] -> 2

l1 [0]-> 1(列表l1中的第一个元素)l2 [2]->橙色(列表l2中的第三个元素)l1 [-1]-> 3(负索引-1表示从列表末尾开始) l1 [-2]-> 2

List Slicing: We can specify range of indexes. start and stop. stop index is not included.

列表切片:我们可以指定索引范围。 开始和停止。 停止索引不包括在内。

l1=[0,1,2,3,4,5]
print (l1[1:4])Output:[1, 2, 3]

Updating List: Inserting elements into the list. insert method will insert element in to the list at specified index.l1=[1,2,3]

更新列表:将元素插入列表。 insert方法将元素插入指定索引处的列表中。l1= [1,2,3]

#It will insert element 4 at index 3.

#它将在索引3处插入元素4。


l1.insert(3,4)
print (l1)

#It will update the element in second index to ‘a’

#它将第二个索引中的元素更新为“ a”

l1[2]=’a’ 
print (l1)Output:[1, 2, 3, 4]
[1, 2, ‘a’, 4]

Updating Nested List:

更新嵌套列表:


l1=[1,2,3,[4,5]]
l1[3][0]=’a’
print (l1)Output:
[1, 2, 3, [‘a’, 5]]

Adding items to the list:

将项目添加到列表中:

1.append 2.extend

1.追加2.扩充

append

附加

It will append the argument as a single item to the end of the list. Length of list will be increased by 1.

它将参数作为单个项目附加到列表的末尾。 清单长度将增加1。


a1=[1,2,3]
a2=[4,5]
a1.append(a2)
print (a1) Output:
[1, 2, 3, [4,5]] b1=[4,5,6]
b1.append(7)
print (b1) Output:
[4, 5, 6, 7]

append method will modify the original list

append方法将修改原始列表

extend

延伸

It will iterate through the argument and add each element to the list.

它将遍历参数并将每个元素添加到列表中。

c1=[1,2,3]
c2=[4,5,6]
c1.extend(c2)
print (c1)Output:
[1, 2, 3, 4, 5, 6]

Removing Items from the list

从列表中删除项目

  • del

    德尔
  • pop()

    pop()
  • remove()

    去掉()
  • clear()

    明确()

    clear()

    明确()

    clear method will empty the list.It will return empty list.

    clear方法将清空列表。它将返回空列表。


a1=[1,2,3]
a1.clear()
print (a1)Output:
[]

del del keyword delete the list itself.

del del关键字删除列表本身。


a1=[1,2,3]
del a1
print (a1)Output:
NameError: name ‘a1’ is not defined.

remove() remove method will remove the element specified from the list.

remove() remove方法将从列表中删除指定的元素。


a1=[1,2,3]
a1.remove(1)
print (a1)Output:
[2,3]

If element mentioned is not in the list,it will raise ValueError.

如果提到的元素不在列表中,它将引发ValueError。


a1.remove(4)
print (a1)Output:
ValueError: list.remove(x): x not in list

pop() pop method will remove the item in the specified index from the list. If index not specified,it will remove the last item from the list.

pop() pop方法将从列表中删除指定索引中的项目。 如果未指定索引,它将从列表中删除最后一项。


a1=[1,2,3]
a1.pop(1)
print (a1)Output:
[1,3]a1.pop()
print (a1)Output:
[1]a1.pop(4)
print (a1)Output:
IndexError: pop index out of range

Joining two list:

合并两个列表:

a1=[1,2,3]
a2=[4,5,6]
a3=a1+a2
print (a3)Output:
[1,2,3,4,5,6]

count:

计数:

count will return the number of occurrences of particular element mentioned.

count将返回提到的特定元素的出现次数。


a1=[1,2,3,4,1,5,6,1]
print (a1.count(1))Output:
3

Sort

分类

sort method will sort the original list.

sort方法将对原始列表进行排序。

Sorting in ascending order using sort function:

使用排序功能按升序排序:


a1=[2,4,6,8,0,7,5,3,1]
a1.sort()
print (a1)Output:
[0, 1, 2, 3, 4, 5, 6, 7, 8]

Sorting in descending order using sort method.

使用sort方法按降序排序。


a2=[2,4,6,8,0,7,5,3,1]
a2.sort(reverse=True)
print (a2)Output:
[8, 7, 6, 5, 4, 3, 2, 1, 0]

reverse

逆转

Reverse method is used to reverse the list. It will reverse the original list .

反向方法用于反向列表。 它将反转原始列表。


l1=[1,3,5,2,4,6,8]
l1.reverse()
print (l1)Output:
[8, 6, 4, 2, 5, 3, 1]

List functions:

列表功能:

sorted()

sorted()

It will sort the list and return a new list. It won’t modify original list.Sorting in ascending order using sorted function:

它将对列表进行排序并返回一个新列表。 它不会修改原始列表。 使用排序函数按升序排序:


a1=[2,4,6,8,0,7,5,3,1]
a2=sorted(a1)
print (f”Original List:{a1}”)
print (f”Sorted List:{a2}”)Output:
Original List:[2, 4, 6, 8, 0, 7, 5, 3, 1]
Sorted List:[0, 1, 2, 3, 4, 5, 6, 7, 8]

Sorting in descending order using sorted function:

使用排序功能按降序排序:


a1=[2,4,6,8,0,7,5,3,1]
a2=sorted(a1,reverse=True)
print (f”Original List:{a1}”)
print (f”Sorted List:{a2}”)Output:
Original List:[2, 4, 6, 8, 0, 7, 5, 3, 1]
Sorted List:[8, 7, 6, 5, 4, 3, 2, 1, 0]

Reversed() Reversed function will reverse the elements in the list. It will return an iterator object. We can iterate through the iterator object or convert to list using list().

Reversed()反转函数将反转列表中的元素。 它将返回一个迭代器对象。 我们可以遍历迭代器对象或使用list()转换为list。

a1=[1,3,5,2,4,6]
a2=reversed(a1)
print (a2)
print (list(a2))Output:
<list_reverseiterator object at 0x02817808>
[6, 4, 2, 5, 3, 1]

min,max,sum,len

最小,最大,总和,len

min — returns the minimum number in the listmax- return the maximum number in the listsum — return the sum of the numbers in the listlen — return the number of elements in the list

min-返回列表中的最小值max-返回列表中的最大值sum-返回列表中数字的总和-返回列表中元素的数量

a1=[1,2,3,4,5,6,7,8,9,10]
print (min(a1))
print (max(a1))
print (sum(a1))
print (len(a1)) Output:
1
10
55
10s1=[‘apple’,’banana’,’orange’,’grapes’]
print (min(s1))
print (max(s1))
print (len(s1))
print (sum(s1)Output:
apple
orange
4
TypeError: unsupported operand type(s) for +: ‘int’ and ‘str’

Check if an item exists in the list.

检查列表中是否存在项目。

s1=[‘apple’,’banana’,’orange’,’grapes’]
if ‘apple’ in s1:
print (“YES”)
if ‘grapes’ not in s1:
print (“Grapes is not in the list “)Output:YES

copy,deepcopy copy function will return copy of the list.Modifications done in the original list will not be reflected in the copied list.But modifications done in the nested list will be reflected in the copied list. deep copy function will return deep copy of the list.Modifications done in the original list will not be reflected in the deep copied list.

复制,deepcopy复制功能将返回列表的副本。在原始列表中所做的修改不会反映在复制列表中。但是在嵌套列表中所做的修改将反映在复制列表中。 深度复制功能将返回列表的深度复制。在原始列表中所做的修改不会反映在深度复制列表中。


import copy
l1=[1,2,3,[4,5]]
l2=l1.copy()
print (“Copied : {}”.format(l2))
l3=copy.deepcopy(l1)
print (“Deep Copied: {}”.format(l3))
l1[0]=’a’
print (“After updating elements in the list”)
print (f”Original List:{l1}”)
print (f”Copied List:{l2}”)
print (f”Deep Copied List:{l3}”)
l1[3][1]=’b’
print (“After updating elements in the nested list”)
print (f”Original List:{l1}”)
print (f”Copied List:{l2}”)
print (f”Deep Copied List:{l3}”)Output:
Copied : [1, 2, 3, [4, 5]]
Deep Copied: [1, 2, 3, [4, 5]]
After updating elements in the list
Original List:[‘a’, 2, 3, [4, 5]]
Copied List:[1, 2, 3, [4, 5]]
Deep Copied List:[1, 2, 3, [4, 5]]
After updating elements in the nested list
Original List:[‘a’, 2, 3, [4, ‘b’]]
Copied List:[1, 2, 3, [4, ‘b’]]
Deep Copied List:[1, 2, 3, [4, 5]]

分配运算符: (Assignment Operator:)

Copy using assignment operator.

l1=[1,2,3]
l2=l1
print (l1)
print (l2)
l1[0]=’a’
l2[1]=’b’
print (“After modifying both list l1 and l2”)
print(l1)
print(l2)Output:
[1, 2, 3]
[1, 2, 3]
After modifying both list l1 and l2
[‘a’, ‘b’, 3]
[‘a’, ‘b’, 3]

In the above example both l1 and l2 will be referring same object in the memory.So changes made in one list will be reflected in the other list too.

在上面的示例中,l1和l2都将引用内存中的同一对象,因此在一个列表中所做的更改也将反映在另一个列表中。

Note:

注意:

list method which will update the original list:
append,extend,remove,clear,pop,insert,sort,reverselist function which will return new list and won’t modify the original list.
sorted() , reversed(),copy(),deepcopy()

翻译自: https://medium.com/swlh/python-list-79ef2e183d60

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值