Python Study Notes Part 4 (Chapter 4) Combination Data Types-First Part

Course content: various functions (lists, primitives, dictionaries, collections) for processing combined data types

1. List

(1) List generation: ls = ['1',1,{1:'Jack',2:'Rose'},('Jack','Rose')], the list is a very tolerant combination data type , The elements can be numbers, strings, Boolean data, lists, primitives, dictionaries, lists, etc., and the list is an ordered element;

(2) The nature of the list: visit ls[5] to access its sixth element, ls[start label: end label (not available): step]; ls[-1:-6:-1]

(3) List operation: add, delete, check and modify, ① add an element: ls.append('ww'), ls.append(['we','here']) add multiple elements: ls.extend([' we','here']), insert an element at a certain position ls.insert(?); ② delete an element: ls.remove('we'), ls.pop() delete the last element; ③ find one Element: ls[22]; ④Modify an element ls[2] ='apple'; Sorting: permanent sorting function -ls.sort(), temporary sorting function -lss = sorted(ls), sorting from small to large by default, If you need to sort from big to small, you can use the reverse ='True' keyword; copy: ls = [1,2,3], ls1 = ls is equivalent to giving ls an alias ls1, they also point to [1,2 ,3], the real copy can be: lss = ls.copy() or lss = ls[:]

Two, tuple

(1) Generation of Yuanzu: ts = ('asas','passing', 1)

(2) The nature of tuples: tuples are unchangeable data

(3) Tuple operation: once the tuple is determined, it cannot be changed; secondly, the packing operation, the function return value has multiple data, automatically packed into a tuple; finally, the unpacking operation, a tuple is unpacked and changed Into several independent data;

Three, dictionary

(1) The generation of the dictionary: {1:'Zhang San', 2: "Li Si", 3: "Wang Wu"}

(2) The nature of the dictionary: the dictionary is unordered, and the value can be obtained by accessing the key value s[key]

(3) Dictionary operation: add, delete, check and modify, and use the get function. For example, output: "Cattle, Sheep, Sheep, Niu, Horse, Horse, Niu, Sheep, Niu, Horse, Niu Niu", count the number of each character and then form a dictionary.

Four, collection

(1) The generation of the set: g = {'Zhang San','Wang Wu','Li Si'}

(2) The nature of the set: the set has no order, and there can be no repeated elements in it

(3) Collection operation: add, delete, check and modify;

Problem: Conversion of sets and lists·

Difficulties, easy to forget places

*** (packaging and unpacking operations of tuples)

(1) Use the get() function of the number of the same words in the dictionary operand

The picture comes from the Internet

(2) Get all the keys of the dictionary and all the values ​​of the dictionary

s = {202001:'Zhang San', 202002:'Li Si', 202003:'Wang Wu'}

skeys = list(s.keys())

svalues = list(s.svalues())

#s = {202001:'张三',202002:'李四',202003:'王五'}
s = {202001:'张三',202002:'李四',202003:'王五'}
print(list(s.keys()))
print(list(s.values()))

运行结果:
[202001, 202002, 202003]
['张三', '李四', '王五']

(3) Traverse all elements of the dictionary s.items()

e = list(s.items())
print(e)
for a,b in s.items():
    print(a,b)

运行结果:
[(202001, '张三'), (202002, '李四'), (202003, '王五')]
202001 张三
202002 李四
202003 王五

(4) The dictionary deletes a key-value pair del s[12306] deletes the specified key-value pair or s.pop(key) deletes the last pair of key-values ​​in the dictionary

del s[key] means that a key-value pair is deleted

s.pop(key) means that a key-value pair is deleted and this data is returned

print(s.pop(202003))
运行结果:
‘王五’

The expressions of dictionary deletion and list deletion are not the same, it is very easy to confuse, you should use more attention

ls.remove("junk food")

ls.pop() deletes the last data in the list and returns this data, and the list can specify which data to delete, ls.pop(2) means delete the third data in the list

 

(4) Set intersection and complement operation

 

The list is converted to a collection, and the collection is converted to a list

summary:

(1) [] and () are used when accessing combined elements, such as list ls[2], tuple t[2], dictionary z[2010001], set s[2]XXX, this expression is wrong, because the set The elements in are unordered, it is meaningless to access the first few elements of the collection, and access to the key-value pairs in the dictionary can only access the value based on the key.

To use a function, parentheses () are generally used. Inside the parentheses are the parameters. ls.sort() The list is permanently flipped, w = sorted(ls) means that the list is temporarily flipped, and the original list remains unchanged. The default sorting rule is from small to large. If you want to sort from big to small, you need to use the keyword reverse ='True'

(2) Elements in lists, tuples, dictionaries, and sets are all separated by commas

(3) The keys in the dictionary must be immutable, such as tuples, strings, numbers, etc., not lists, dictionaries, sets, etc.

(4) Application of dictionary get function

(5) The key point is the operation of strings and dictionary operations

Guess you like

Origin blog.csdn.net/dqefd2e4f1/article/details/112977064