On multivariate data type of of python

  • 2021-12-12 09:23:49
  • OfStack

Directory What is a list list1. Declare a list list
2. List what data types list supports
3. List the basic features of list
Summarize

What is the list list

List is the most basic data structure in python. Each element in the list is assigned a number--that is, the position of the current data, or index, the first index is 0, the second index is 1, and so on

* The list is a "basket" in which you can put anything. When there are more data 1, the structure is formed. List list is a form of data set

# List is an ordered, variable data structure/type

1. Declare a list list

Method 1: Create a null value list_data = list()

Method 2: Create a null value list_data = [ ]

Use between lists, separate them, and use square brackets at the beginning and end []

Grammatical rules of lists [China, 'SHENZHEN', 44, True, 1.0]

List adds other types of data (nested) [{"name": "like", "age": 4}, (1, 2), [1, 3, 4]]

Pay special attention to:

1. Each element (data) in the list follows the original rules of the data type. For example, the string type is quoted ""

2. There can be multiple layers of nesting, and each layer of nesting exists as an independent body

2. List what data types list supports

name = "like" string Str

age = 18 integer Int

height = 1.74 floating point Float

is_yes = True Boolean Bool

pets = ["Cow", "Tiger", "Rabbit"] List List

work_cities = ("Beijing", "Shanghai", "Guangzhou") Tuple Tuple

teacher = {"Name": "Zhang 3"} Dictionary Dict

3. List the basic features of list

Add: Support empty list creation, support list data update (add)

Delete: Support overall list deletion, list element deletion and slice deletion

Modification: Supports list data modification (based on subscript, index, index)

Query: Supports list data query (based on subscript, index, index), support slice query, support level 1 element length statistics (len)


personal_info= ['张3',"男"]
#列表基本操作
#增加数据
	personal_info.append(18)
	最后1位追加>>>['张3',"男",18]
	personal_info.insert(1,1.80)
	基于索引位置插入数据>>>['张3',1.80,"男",18]
#修改数据
	personal_info[1] = 1.90
	基于索引位置修改数据>>>['张3',1.90,"男",18]
	personal_info[2:4] = ["女",19]
	基于索引范围修改数据>>>['张3',1.90,"女",19]
#查询数据
	personal_info[1]
	基于索引位置修改数据>>> 1.90
	personal_info[2:4]
	基于索引范围修改数据>>>["女",19]
#删除数据
	personal_info.pop(1)
	基于索引删除数据>>>['张3',"女",19]
	personal_info.remove(19)
	基于值删除数据>>>['张3',"女"]
	del personal_info[0:2]
	基于索引位置删除>>>[]
	personal_info.clear()
	清空全部列表元素>>>[]
	del personal_info
	删除整个列表>>>[]
*特别注意
	1、修改数据和查询数据”1线间“
	2、圈定范围修改列表数据时,需要保证添加的数据为可迭代对象
	3、注意pop()默认为删除最后1位,可指定索引位置,并返回被删除的值
	4、remove()基于值/数据删除数据,删除的是第1个匹配的数据,并非所有
#列表合并 
	num_1 =[1,2,3]
	num_2 =[5,6,7,8]
	num_sum =num_1 + num_2
	>>>[1,2,3,5,6,7,8]
	num_1.extend(num_2)
	特别注意 extend()会改变当前变量内的值>>>[1,2,3,5,6,7,8]
#列表排序
	num_sum =num_2 + num_1 
	>>>[5,6,7,8,1,2,3,]
	num_sum.sort()
	正序>>>[1,2,3,5,6,7,8]	
	num_sum.sort(reverse = True)
	倒序>>>[8,7,6,5,3,2,1]
	sort()会直接改变当前变量内的值
#列表反转
	msg = ['好',"好","学","习"]
	msg.reverse()
	>>>["习","学","好","好"]
#元素数据统计
	msg.count("好")
	>>>2
#自身复制
	msg = msg * 2
	msg>>>['好',"好","学","习",'好',"好","学","习"]
#列表的长度
	len(msg)
	>>>8
#列表的复制
	num = [[1,2,3],[6,7]]
	id(num)
	>>> 4567867877
	id(num[0])
	>>> 4598080890 (同)
	#浅复制
	num_X = num.copy()
	id(num_X)
	>>> 4567867800
	id(num_X[0])
	>>> 4598080890 (同)
	#深复制
	import copy
	num_Y = copy.deepcopy(num)
	id(num_Y)
	>>> 4567867800
	id(num_Y[0])
	>>> 4598080999 (不同)
#包含关系判断
	msg = ["好","好",'学','习']
	"习" in msg
	>>>True
	仅支持当前层级判断
#切片延申
	num = [0,1,2,3,4,5,6,7,8,9]
	num[0:10:2]  [起,止,步长]
	>>>[0,2,4,6,8]
	类似于for循环中的range()函数
	num[::2]
	>>>[0,2,4,6,8]
	特别注意 起止不设定默认,为从头到尾的范围
#list在程序中的书写
    #单列表
    list = ["哈士奇","萨摩","柴犬","泰迪"]
    列表这个数据类型的性质决定了它通常内部存在数据比较长,书写的时候会超出屏幕边界,所以可以换行:
    list = ["哈士奇",
    		"萨摩",
    		"柴犬",
    		"泰迪"
    		]
    		注意对齐,好看清晰明了
    #嵌套列表
    list = [
    		['拍黄瓜','大拌菜','小葱拌豆腐'],
    		['白酒','啤酒','红酒'],
    		['橘子','香蕉','苹果'],
    		]		
    	1组数据里有高度、有宽度可以叫做:
    	1、矩阵
    	2、2维数组
#列表与for循环
	#初级难度
	dogs = ["哈士奇",
    		"萨摩",
    		"柴犬"
    		]
   	for dog in dogs:
   		print(dog)
   		第1次:哈士奇
   		第2次:萨摩
   		第3次:柴犬
	#中级难度
	menus = [
    		['拍黄瓜','大拌菜','小葱拌豆腐'],
    		['白酒','啤酒','红酒'],
    		['橘子','香蕉','苹果'],
    		]
    for menu in menus:
    	print(menu)
        第1次:['拍黄瓜','大拌菜','小葱拌豆腐']
        第2次:['白酒','啤酒','红酒']
        第3次:['橘子','香蕉','苹果']
	#高级难度
	menus = [
    		['拍黄瓜','大拌菜','小葱拌豆腐'],
    		['白酒','啤酒','红酒'],
    		['橘子','香蕉','苹果'],
    		]
	for menu in menus:
    	print(menu)
    	for food in menu:
    		print(food)
		第1次:['拍黄瓜','大拌菜','小葱拌豆腐']
			内循环第1次:'拍黄瓜'
			内循环第2次:'大拌菜'
			内循环第3次:'小葱拌豆腐'
        第2次:['白酒','啤酒','红酒']
        	内循环第1次:'白酒'
			内循环第2次:'啤酒'
			内循环第3次:'红酒'
        第3次:['橘子','香蕉','苹果']
        	内循环第1次:'橘子'
			内循环第2次:'香蕉'
			内循环第3次:'苹果'

Summarize

This article is here, I hope to give you help, but also hope that you can pay more attention to this site more content!


Related articles: