Python basics of developing, List collection of basic operating inventory

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/meiguanxi7878/article/details/102726828

Python list List is the most basic data structures, is the most frequently used type of data in Python, List elements in the list need not have the same type, very convenient to use. Now come to experience the basic operation of the List list.

Basic operation set list

List of basic operation (=, copy, +, *, len, in)

1) = Assignment

list01 = [10, 20, 30, 40, 50, 60, 70, 80, 90]
list02 = list01 # =赋值,把list01赋给list02
print(list02)

list03 = list01.copy() # 使用copy把list01的值给list03
print(list03)

Note: = and copy are essentially different!

list assignment

2) + List collection merge two

在学习过程中有什么不懂得可以加我的
python学习交流扣扣qun,784758214
群里有不错的学习视频教程、开发工具与电子书籍。
与你分享python企业当下人才需求及怎么从零基础学习好python,和学习什么内容
list01 = [1, 2, 3]
list02 = [4, 5, 6]
print(list01 + list02)

The combined list

3) Let the set of repeated many times List *

list01 = ["I", "love", "Python"]
print(list01 * 3)

Repeat the set list

4) len returns the number of list elements in a set

list01 = [10, 20, 30, 40, 50, 60, 70, 80, 90]
print("list01的元素个数为:", len(list01))

list the basic operation

5) in member operator, determines whether a certain element in the List

list01 = [10, 20, 30, 40, 50, 66]
print("40是否在list01中:", 40 in list01)
print("70是否在list01中:", 70 in list01)

list members of the judge

List collection to add elements (append, insert, enxtend)

Defined list01, list02, and add elements

list01 = [10, 20, 30, 40, 50, 60]
list02 = []

1) using the append method to add elements to List as a whole, by default added to the end of the list List

list01.append("70")
print(list01)
list02.append("BBB")
print(list02)

list elements are added

2) the use of added insert may be inserted to the specified location

list01.insert(1, 15)
print(list01)
list02.insert(1, "CCC")
print(list02)

Operation list

3) Use enxtend added, a plurality of additional one-time value in the other sequence (at the end of the list with a new list of the original extended list)

在学习过程中有什么不懂得可以加我的
python学习交流扣扣qun,784758214
群里有不错的学习视频教程、开发工具与电子书籍。
与你分享python企业当下人才需求及怎么从零基础学习好python,和学习什么内容
list02.extend(["DDD", "EEE", "FFF"])
print(list02)
list01.extend(list02) # extend是将List02中的每一个值依次添加至List01列表的末尾
print(list01) 

Adding list

List remove elements (remove, pop, del, clear)

1) remove delete a specific element value, a default deletion is the first element of the same elements is not given to the element

list01 = [10, 20, 30, 40, 50, 60, 30]
list01.remove(30)
print(list01)

list remove elements

2) pop pop elements, when there is no index number () Removes the last element in the default

list01 = [10, 20, 30, 40, 50, 60, 30]
list01.pop(1) # 删除第二个元素
print(list01)
list01.pop()
print(list01) # 默认情况下删除最后一个元素

Delete list

**3)del 删除列表中指定位置的元素

list01 = [10, 20, 30, 40, 50, 60, 30]
# del list01[1] # 删除第二个元素
# print(list01)
del list01[1:5] # 删除第二个到第五个
print(list01)

list操作

4)clear 清空列表中的所有元素

list01 = [10, 20, 30, 44, 50, 66, 30]
list01.clear() # 删除list列表中所有的元素
print(list01)


对Python感兴趣或者是正在学习的小伙伴,可以加入我们的Python学习扣qun:784758214,看看前辈们是如何学习的!从基础的python脚本到web开发、爬虫、django、数据挖掘等,零基础到项目实战的资料都有整理。送给每一位python的小伙伴!每天都有大牛定时讲解Python技术,分享一些学习的方法和需要注意的小细节,点击加入我们的 python学习者聚集地

list清空

以上就是List列表最最常用到的基础操作,当然方法还有很多,关注IT教头王进,一起学Python。

Guess you like

Origin blog.csdn.net/meiguanxi7878/article/details/102726828