2021-09-09--python Operator List Derivation with Less Product

Posted by KMC1499 on Thu, 09 Sep 2021 18:34:27 +0200

A little python knowledge a day

Does it not smell when you add up more???

Section XIV

Operators and public methods

Operator, supports container types

# Multiple variables are assigned to the same value, one to one,
str1, str2 = "abc", "def"  # Character string
list1, list2 = [1, 2], [3, 4]  # list
t1, t2 = (11, 21), (31, 41)  # Tuple tuple
set1, set2 = {10, 11}, {20, 21}  # aggregate
dict2 = {"name": "qao", "age": 99}  # Dictionaries

a, +: Merge, support string, list, tuple

print(str1 + str2)  # Results: abcdef
print(t1 + t2)  # Result: Error, collection and dictionary do not support merging

b, *: copy, support string, list, tuple

print(t1 * 3)  # Results: (11, 21, 11, 21, 11, 21)
print(dict2 * 4)  # Error: Collections and dictionaries do not support replication

c, in: whether the element exists, supports strings, lists, tuples, dictionaries, and returns Booleans of True and False

print("a" in str1)  # Result: True
print("qao" in dict2)  # Result: False

d, not in: whether the element does not exist, supports strings, lists, tuples, dictionaries, and returns Booleans of True and False

print("a" not in str1)  # Result: False
print("qao" not in dict2)  # Result: True

Dictionary in and not in usage

print("age" in dict2)  # Result: True
print(dict2["name"] in dict2.values())  # Result: True

Public method

str3 = "abcd"  # Character string
list3 = [1, 2, 4, 6, 1]  # list
t3 = (12, 22, 32, 42)  # Tuple tuple
set3 = {500, 400, 300, 200, 100}  # aggregate
dict3 = {"xm": "guy", "age": 60, "id": 123456}  # Dictionaries

a, len(): Calculate the number of elements in the container

print(len(t3))
print(len(dict3))
# Result:
4
3

b, del or del(): delete, delete according to subscript, delete according to k without subscript

del set3  # Delete by Collection Name
print(set3)
# Result:
List does not exist, error will occur

del list3[2]  # Delete by Subscript
print(list3)
# Result:
[1, 2, 6, 1]
del(dict3["xm"])
print(dict3)
# Result:
{'age': 60, 'id': 123456}

c, max(): Returns the maximum value of an element in a container
d, min(): Returns the minimum value of an element in a container

print(max(str3))
print(min(str3))  # English size is sorted alphabetically
# Result:
d
a
print(max(set3))
print(min(set3))
# Result:
500
100
print(max(dict3))
print(min(dict3))
# Result:
id
age

e, range(start,end,step): Generate a number from start to end in steps for recycling

Generate numbers between 1 and 10

for i in range(1, 11):  # Step size is not written after unpacking, default is 1
    print(i)
# Result:
1
2
3
4
5
6
7
8
9
10

for i in range(1, 11, 2):  # Before and after the package, the step is not written, default is 1, step is incremental
    print(i)
# Result:
1
3
5
7
9

for i in range(10):  # Represents start without writing, defaults to zero, step 1
    print(i)
# Result:
0
1
2
3
4
5
6
7
8
9

f,enumerate(Traversable object): Function to use a traversable data object(Such as a list, tuple, or string)Combines a sequence of indexes, listing both data and data subscripts, commonly used for for In a loop, changes the starting value of a sequence subscript in a sequence
for i in enumerate(list3):  # The data type returned is a tuple in which the first number is the subscript for the number in the sequence and the second number is the number in the sequence
     print(i)
# Result:
(0, 1)
(1, 2)
(2, 6)
(3, 1)

for i in enumerate(list3, start=2):  # Start sets the start value of the subscript
    print(i)
# Result:
(2, 1)
(3, 2)
(4, 6)
(5, 1)

Container Type Conversion

list4 = [11, 22, 33, 44, 55]                     # List list list
t4 = (10, 20, 30, 40, 50)                        # Tuple tuple
s4 = {13, 23, 33, 43, 53}                        # set collection
dict4 = {"name": "gao", "age": 34, "id": 65535}  # dict dictionary

A, tuple(): Convert a sequence to a tuple

print(tuple(list4))
print(tuple(t4))
print(tuple(s4))
print(tuple(dict4))  # val value is lost when converting dictionary to other sequence types
# Result:
(11, 22, 33, 44, 55)
(10, 20, 30, 40, 50)
(33, 43, 13, 53, 23)
('name', 'age', 'id')

b, list(): Convert a sequence to a list

print(list(list4))
print(list(t4))
print(list(s4))
print(list(dict3))  # age type is integer, no longer converted into type, can be entered as string type, and then converted to integer type
# Result:
[11, 22, 33, 44, 55]
[10, 20, 30, 40, 50]
[33, 43, 13, 53, 23]
['age', 'id']

c, set(): Convert a sequence to a set

Be careful
1. Collection can quickly complete list de-weighting
2. Subscripts are not supported by collections

print(set(list4))
print(set(t4))
print(set(s4))
print(set(dict4))
# Result:
{33, 11, 44, 22, 55}
{40, 10, 50, 20, 30}
{33, 53, 23, 43, 13}
{'id', 'age', 'name'}

d, dict() does not convert other types to dictionary functions because dictionaries contain key-value pairs and other types lack keys or values

print(dict(list4))
# Result:
Unable to run, exit code 1

List Derivation

Derivative function: simplifying sequence writing
Derivation of lists: Create a regular list with an expression or control a regular list. List derivation is also called list generation

# Create a list of 1 to 10 using while, for, and list derivation
# while Loop
i = 1
list5 = []  # Create an empty list
while i <= 10:  # Generate numbers between 1 and 10
    list5.append(i)  # Loop adds i (generated value to empty list), append() adds function
    i += 1
print(list5)
# Result:
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

# for loop
list6 = []  # Create an empty list
for j in range(1, 11, 1):  # range generates numbers from 1 to 10 with a step of 1
    list6.append(j)
print(list6)
# Result:
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

# List derivation, generally used with for loop
list7 = [d for d in range(1, 11)]
print(list7)
# Result:
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

List derivation with if

Create a list of even numbers in a range of 1 to 10
1. Use range step 2 to control
2. Use if statement to control implementation: for loop followed by if plus condition

list8 = [a for a in range(1, 11) if a % 2 == 0]
print(list8)
# Result:
[2, 4, 6, 8, 10]

Multiple for loops implement list derivation
Effect achieved [(1, 0), (1, 1), (1, 2), (2, 0), (2, 1), (2, 2)]
for loop implementation
Tuple data 1:1 2->range(1,3)Tuple data 2,0 1 2->range(3)

list9 = []
for i1 in range(1, 3):
    for i2 in range(3):
        list9.append((i1, i2))  # Add Tuples to List
print(list9)
# Result:
[(1, 0), (1, 1), (1, 2), (2, 0), (2, 1), (2, 2)]

Multiple for loops implement list derivation in the form of for loops (outside) for loops (inside)

list10 = [(t, b)for t in range(1, 3) for b in range(3)]
print(list10)
# Result:
[(1, 0), (1, 1), (1, 2), (2, 0), (2, 1), (2, 2)]

Topics: Python Pycharm crawler