Zero-based introductory learning Python (11)-list (3)

Some common operators for lists

Comparison operator

Logical operator

Concatenation operator

Repeat operator

When there are multiple elements, the default is to compare from the 0th element
Insert picture description here
Insert picture description here

String comparison is the size of the ASCII code value corresponding to each character
Insert picture description here
What is the ASSII code?
It is the abbreviation of American Standard Code for Information Interchange. The American Standard Code for Information Interchange was developed by the American National Standard Institute (ANSI). It is a standard single-byte character encoding scheme for text-based data.

we knowStrings can be spliced ​​with "+" and copied several times with "*", which can also be realized in the list
The expend() expansion list will be more standardized, instead of "+", because some operations of the plus sign are illegal, because the concatenation operator plus cannot implement the operation of adding new elements
Insert picture description here
Insert picture description here

Membership operator

in

not in

Insert picture description here
in and not in can only judge one level of membership , the same as break and continue affecting only one level of loop
Insert picture description here
**How ​​do we access the values ​​in the list? ? ? **Use two [] to enter the index value
Insert picture description here

Built-in functions for list types

How many friends does the list have? ? ? useto youFunction query
Insert picture description here

count(): The function of this method is to count the number of times each element appears in the list

The built-in method of the list should use the dot "." to indicate the range
Insert picture description here

index(): This method can limit the scope of the search, that is, return the position of its parameter in the list

  • Parameter 1: Element value
  • Parameter 2: Range start
  • Parameter 3: Range termination
    Insert picture description here
    list3.index(123,1,4): returns the position of the first occurrence of element 123 in list3 within the range of 1-3 index values

reverse(): The function of this method is to reverse the entire list in place

Insert picture description here

sort(): This method is to sort the list elements, the default is to sort from small to large,

Insert picture description here
What if you need to sort from largest to smallest?

  • Method 1: First call the sort() method to sort from small to large, and then use the reverse() method to reverse in place
  • Method 2: The sort() method actually has 3 parameters:
    [1] Parameter 1: The func parameter is used to set the sorting algorithm
    [2] Parameter 2: The key parameter is used to set the sort key, and merge sort is used by default
    [3] Parameter 3: The reverse parameter is False by default. If the reverse parameter is changed to True, it will be sorted from largest to smallest
    Insert picture description here

Supplement about fragment copy

Use slices to create a copy of the list.
Python variables are like a label, like a name, where they are posted, and where they are labeled
list6 = [4,5,3,6,8,1]
list7 = list6 [:]
list8 = list6
list7 is a copy of list6, and assigning list6 to list8, it seems that the effect is the same, but after sorting list6.sort(), list7 is still a copy of list6, but list8 changes with the order of list6 And change, so don’t be lazy,To copy a list, use fragmentation
That is to use = directly, there is an additional label
Insert picture description here
Insert picture description here

Task

0. Note that this question is a bit different from the one in the last class. After answering, please experiment on the computer or refer to the answer

>>> old = [1, 2, 3, 4, 5]
>>> new = old
>>> old = [6]
>>> print(new)

Will print [1, 2, 3, 4, 5], here is the assignment, new changes with the change of old, the last lesson was a piecewise copy
Insert picture description here
1. How can I change the "small turtle" in the list below to 'Little squid'?

list1 = [1, [1, 2, ['小甲鱼']], 3, 5, 8, 13, 18]

Use del to delete, then use append to add

>>> list1 = [1, [1, 2, ['小甲鱼']], 3, 5, 8, 13, 18]
>>> del list1[1][2]
>>> list1[1].append('小鱿鱼')

Insert picture description here
The small turtle method is simpler:

list1 = [1, [1, 2, ['小甲鱼']], 3, 5, 8, 13, 18]
list1[1][2][0] = '小鱿鱼'

2. To sort a list in order, what method is used?
list1.sort()

3. To sort a list in reverse order, what method is used?
list1.sort(reverse=True)
or;

>>> 列表名.sort()
>>> 列表名.reverse()

4. There are two built-in methods in the list that I haven't introduced to you, but you should be smart enough to explore the doorways you use: copy() and clear() The
copy() method is the same as using the slice copy. The
clear() method is used To empty the elements of the list, but note that the empty list still exists, but becomes an empty list

5. Have you heard of list comprehensions or list comprehensions?

Have not heard? ! It doesn't matter, let's learn it on the spot, look at the expression:

>>> [i*i for i in range(10)]
[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]

Print the squares of the numbers from 0 to 9 respectively, and then put them in the list

List comprehensions, also called list comprehensions, are inspired by the functional programming language Haskell. Ta is a very useful and flexible tool that can be used to dynamically create lists, the syntax is such as:

[Expressions about A for A in B]
E.g

>>> list1 = [x**2 for x in range(10)]
>>> list1
[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]

Equivalent to

list1 = []
for x in range(10):
    list1.append(x**2)

Please get the result of the list below in IDLE first, and restore the list deduction according to the example above.

>>> list1 = [(x, y) for x in range(10) for y in range(10) if x%2==0 if y%2!=0]

reduction:

>>> list1 = []
>>> for x in range(10):
	if x % 2 == 0:
		for y in range(10):
			if y % 2 != 0:
				list1.append((x,y))

Insert picture description here
6. Learn and use: Please use the list comprehension to supplement the part that was accidentally obliterated by the turtle
Insert picture description here

list1 = ['1.Just do it','2.一切皆有可能','3.让编程改变世界','4.Impossible is Nothing']
list2 = ["4.阿迪达斯","2.李宁","3.鱼c工作室","1.耐克"]
for x in list1:
    for y in list2:
        if x[0] == y[0]:
            print(y + ":" + x[2:])
list1 = ['1.Just do it','2.一切皆有可能','3.让编程改变世界','4.Impossible is Nothing']
list2 = ["4.阿迪达斯","2.李宁","3.鱼c工作室","1.耐克"]
list3 = [y + ":" + x[2:] for x in list1 for y in list2 if x[0] == y[0]]
for each in list3:
    print(each)

Insert picture description here
Insert picture description here

Guess you like

Origin blog.csdn.net/qq_44520665/article/details/113028232