The addition and deletion of the list of Python and the usage of the Python dictionary, Xiaobai can also learn the way of python

table of Contents

Foreword

1. List

1. The format of the list:

2. Print an element

 3. Print multiple elements at the same time

4. Add and delete elements to the list

Add element

Delete element

Second, the dictionary

1. The format of the dictionary

2. Add / delete elements to the dictionary

3. Similarities between lists and dictionaries

Four, nested with each other

Five, the difference between lists and dictionaries


 

Foreword

Starting from the long python road, as a programmer, I have to write the front end, do the back end, write the sql, and understand the deployment. Of course, the recent hot python must also be engaged, so I have to toss it up. Come, learn java with me on the left, learn python from Xiaobai together on the right, learn together, grow together 

 

1. List

1. The format of the list:

A list needs to use brackets [ ]to frame the various data in it, and each data in it is called an "element". Each element must be separated by a comma.

For example: names = ['Zhang San', 'Li Si', 'Wang Wu']

names = ['张三','李四','王五']
for i in names
	print(i+',你好')

operation result:

 

2. Print an element

Of course, it can also be printed one by one, the subscript starts from 0

names = ['张三','李四','王五']
print(names[1]+',你好')

 

3. Print multiple elements at the same time

To print multiple elements at the same time, we use a colon to slice, as the name implies, to take out a certain segment of the list and process it. This way of slicing allows us to take multiple elements from the list.

list2 = [0,1,2,3,4,5,6,7,8,9]
print(list2[:])
print(list2[2:])
print(list2[:2])
print(list2[1:3])
print(list2[2:4])

For the above output, you may be more difficult to remember. It is a bit similar to java's substr function. You only need to remember the following formula: the colon is left and right, and the head is taken; the colon is left and right is not taken.

 

4. Add and delete elements to the list

 

Add element

You need to use the append () function to add elements to the list. Append means append and add. Use append () to add elements to the list, only one element at a time

The elements in the list can be strings, numbers, etc., or the list itself (that is, nesting is supported inside the list), as long as the number of parameters after the append function meets one (a single list will also be regarded as an element). The appended elements will be added at the end of the list.

names = ['张三','李四','王五']
names.append('赵六')
print(names)

 

 

Delete element

Using the del () function, the del statement is very convenient. It can delete one element or multiple elements at a time (the principle is similar to slicing, left is taken and right is not taken)

names = ['张三','李四','王五','赵六']
print(names)
del(names[3])
print(names)
del(names[0:2])
print(names)

 

 


 

Second, the dictionary

The above list is similar to java's list function, python has a map function similar to java

 

1. The format of the dictionary

Xiaoming, Xiaohong, and Xiaogang have taken 95, 90, and 90 points respectively, and if we use a list to load the data, we need to create a new list to put the scores on, and it is very troublesome to ensure that the order of names is the same. Therefore, there is a one-to-one correspondence between two kinds of data such as this name and value (such as score, height, weight, etc.). It is more convenient to use the second data type-"dictionary" to store.

The elements of the dictionary are composed of key-value pairs, connected by English colons. For example '张三':18, we '张三'call it key and 18value. In this way, the combination of the unique key and the corresponding value is called [key-value pair], the key in the dictionary is unique, and the value can be repeated

Format: ages = {'Zhang San': 18, 'Li Si': 19, 'Wang Wu': 20}

 

Lists get values ​​by index, such as list [0], while dictionaries get values ​​by key

such as

ages = {'张三':18,'李四':19,'王五':20}
print(ages['张三'])

 

2. Add / delete elements to the dictionary

The code to delete the key-value pairs in the dictionary is a del statement del 字典名[键], and the new key-value pairs use assignment statements 字典名[键] = 值.

ages = {'张三':18,'李四':19,'王五':20}
print(ages['张三'])

#删除
del ages['张三']
print(ages)

#增加
ages['赵六'] = '21'
print(ages)

 

3. Similarities between lists and dictionaries

In the list and dictionary, if you want to modify the element, you can use the assignment statement to complete

names = ['张三','李四','王五']
names[1] = '赵六'
print(names)

ages = {'张三':18}
ages['张三'] = 20
print(ages)

 

Four, nested with each other

# 最外层是大括号,所以是字典嵌套列表,先找到字典的键对应的列表,再判断列表中要取出元素的偏移量
students = {
    '第一组':['小明','小红','小刚','小美'],
    '第二组':['小强','小兰','小伟','小芳']
    }
print(students['第一组'][3])
#取出'第一组'对应列表偏移量为3的元素,即'小美'

# 最外层是中括号,所以是列表嵌套字典,先判断字典是列表的第几个元素,再找出要取出的值相对应的键
scores = [
    {'小明':95,'小红':90,'小刚':100,'小美':85},
    {'小强':99,'小兰':89,'小伟':93,'小芳':88}
    ]
print(scores[1]['小强'])
#先定位到列表偏移量为1的元素,即第二个字典,再取出字典里键为'小强'对应的值,即99。

 

Five, the difference between lists and dictionaries

The elements in the list have their own clear "position", so even if the elements that seem to be the same, as long as the position of the list is different, they are two different lists.

Compared with the dictionary, it seems to be much more easy-going, and the order of transfer does not affect. Because the data in the list is ordered, and the data in the dictionary is randomly arranged

# 如果==左右两边相等,值为True,不相等则为False。
print(1 == 1)  
# 1等于1,所以值为True

print(1 == 2)
# 1不等于2,所以为False

names1 = ['张三','李四','王五']
names2 = ['王五','张三','李四']
print(names1 == names2)

ages1 = {'张三':18,'李四':19,'王五':20}
ages2 = {'王五':20,'张三':18,'李四':19}
print(ages1 == ages2)

 

Published 88 original articles · Liked 151 · Visits 450,000+

Guess you like

Origin blog.csdn.net/qq_27471405/article/details/103997212