Task2 Python basic exercise: data structure summary

Keywords: Python data structure

Task 2 Python basics review: data structure summary

List list

1. Create a list by derivation

x = [i for i in range(100) if (i % 2) != 0 and (i % 3) == 0]
#[3, 9, 15, 21, 27, 33, 39, 45, 51
#, 57, 63, 69, 75, 81, 87, 93, 99] <class 'list'>

2. Since the element of the list can be any object, what is saved in the list is the pointer of the object. Even if you save a simple [1,2,3], there are 3 pointers and 3 integer objects.

In the x = [a] * 4 operation, only four references to the list are created, so once a changes, the four a in X will also change.

x = [[0] * 3] * 4
print(x, type(x))
# [[0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0]] <class 'list'>

x[0][0] = 1
print(x, type(x))
# [[1, 0, 0], [1, 0, 0], [1, 0, 0], [1, 0, 0]] <class 'list'>

a = [0] * 3
x = [a] * 4
print(x, type(x))
# [[0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0]] <class 'list'>

x[0][0] = 1
print(x, type(x))
# [[1, 0, 0], [1, 0, 0], [1, 0, 0], [1, 0, 0]] <class 'list'>

3.list.extend(seq) append multiple values in another sequence at one time at the end of the list (expand the original list with the new list)

x = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday']
x.extend(['Thursday', 'Sunday'])
print(x)  
# ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Thursday', 'Sunday']

print(len(x))  # 7

4.list.insert(index, obj) insert obj in the index position.

x = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday']
x.insert(2, 'Sunday')
print(x)
# ['Monday', 'Tuesday', 'Sunday', 'Wednesday', 'Thursday', 'Friday']

print(len(x))  # 6

5. Copy all elements in the list (light copy): "

eek = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday']
print(week[:])  
# ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday']

6. Light copy vs. deep copy

list1 = [123, 456, 789, 213]
list2 = list1
list3 = list1[:]

print(list2)  # [123, 456, 789, 213]
print(list3)  # [123, 456, 789, 213]
list1.sort()
print(list2)  # [123, 213, 456, 789] 
print(list3)  # [123, 456, 789, 213]

list1 = [[123, 456], [789, 213]]
list2 = list1
list3 = list1[:]
print(list2)  # [[123, 456], [789, 213]]
print(list3)  # [[123, 456], [789, 213]]
list1[0][0] = 111
print(list2)  # [[111, 456], [789, 213]]
print(list3)  # [[111, 456], [789, 213]]

Append, extend and insert can add elements to the list. They have no return value and directly modify the original data object
7. Other methods
list.count(obj) counts the number of times an element appears in the list

l=[1,2,3]*3
num=l.count(1)
print(num)#3

list.index(x[, start[, end]]) finds the index position of the first match of a value from the list

list1 = [123, 456] * 5
print(list1.index(123))  # 0
print(list1.index(123, 1))  # 2
print(list1.index(123, 3, 7))  # 4

list.reverse() reverses the elements in the list

x = [123, 456, 789]
x.reverse()
print(x)  # [789, 456, 123]

Tuple tuple

You can create tuples with parentheses (), or without anything. For readability, it is recommended to use ().
When a tuple contains only one element, you need to add a comma after the element, otherwise the parentheses will be used as operators.

x = (22)
print(type(x))  # <class 'int'>
x = (22,)
print(type(x))  # <class 'tuple'>

print(8 * (8))  # 64
print(8 * (8,))  # (8, 8, 8, 8, 8, 8, 8, 8)

1. Create tuples

x = (1, 10.31, 'python'), ('data', 11)
print(x)
# ((1, 10.31, 'python'), ('data', 11))

print(x[0])
# (1, 10.31, 'python')
print(x[0][0], x[0][1], x[0][2])
# 1 10.31 python

print(x[0][0:2])
# (1, 10.31)

2. Update and delete a tuple

week = ('Monday', 'Tuesday', 'Thursday', 'Friday')
week = week[:2] + ('Wednesday',) + week[2:]
print(week)  # ('Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday')

Note: tuples are immutable, so we cannot directly assign values to the elements of tuples. However, as long as the elements in tuples are mutable, we can directly change their elements. Note that this is different from assigning their elements.

t1 = (1, 2, 3, [4, 5, 6])
print(t1)  # (1, 2, 3, [4, 5, 6])

t1[3][0] = 9
print(t1)  # (1, 2, 3, [9, 5, 6])

3. Built in method
The tuple size and content cannot be changed, so there are only count and index methods.

t = (1, 10.31, 'python')
print(t.count('python'))  # 1
print(t.index(10.31))  # 1

4. Decompression
If you only want a few elements of a tuple, use the wildcard "*", which is called wildcard in English. It represents one or more elements in computer language. The following example is to throw multiple elements to the rest variable.

t = 1, 2, 3, 4, 5
a, b, *rest, c = t
print(a, b, c)  # 1 2 5
print(rest)  # [3, 4]

str string

1. Escape character description
\Backslash symbol
’Single quotation mark
Double quotes
\n line feed
\t horizontal tab (TAB)
\r enter

2. Three quotation marks allow a string to span multiple lines. The string can contain line breaks, tabs and other special characters.

para_str = """This is an instance of a multiline string
 Multi line strings can use tabs
TAB ( \t ). 
You can also use line breaks [ \n ]. 
"""
print(para_str)
# This is an instance of a multiline string
# Multi line strings can use tabs
# TAB (    ). 
# You can also use line breaks[
#  ]. 

para_str = '''This is an instance of a multiline string
 Multi line strings can use tabs
TAB ( \t ). 
You can also use line breaks [ \n ]. 
'''
print(para_str)
# This is an instance of a multiline string
# Multi line strings can use tabs
# TAB ( 	 ). 
# You can also use line breaks[ 
#  ]. 

3. Common built-in methods of string

  • capitalize() converts the first character of a string to uppercase
  • lower() converts all uppercase characters in the string to lowercase
  • upper() converts lowercase letters in a string to uppercase
  • swapcase() converts uppercase to lowercase and lowercase to uppercase in the string
  • count(str, beg= 0,end=len(string)) returns str
    If beg or end is specified, the number of str occurrences within the specified range will be returned.
str2 = "DAXIExiaoxie"
print(str2.count('xi'))  # 2
  • Endswitch (suffix, beg = 0, end = len (string)) checks whether the string is suffix with the specified substring
    End, if yes, return True, otherwise return False. If beg and end specify values, check within the specified range.
  • Startswitch (substr, beg = 0, end = len (string)) checks whether the string starts with the specified substr. If yes, it returns True, otherwise it returns False. If beg and end specify values, it checks within the specified range.
str2 = "DAXIExiaoxie"
print(str2.endswith('ie'))  # True
print(str2.endswith('xi'))  # False
print(str2.startswith('Da'))  # False
print(str2.startswith('DA'))  # True
  • find(str, beg=0, end=len(string)) checks whether str is included in the string. If the specified range is beg and end, check whether it is included in the specified range. If it is included, return the starting index value, otherwise return - 1.
  • rfind(str, beg=0,end=len(string)) is similar to the find() function, but it starts from the right.
str2 = "DAXIExiaoxie"
print(str2.find('xi'))  # 5
print(str2.find('ix'))  # -1
print(str2.rfind('xi'))  # 9
  • isnumeric() returns True if the string contains only numeric characters, otherwise False.
str3 = '12345'
print(str3.isnumeric())  # True
str3 += 'a'
print(str3.isnumeric())  # False
  • partition(sub) finds the substring sub and divides the string into a triple (pre_sub,sub,fol_sub). If the string does not contain sub, it returns ('original string ',' ',' ').
  • rpartition(sub) is similar to the partition() method, but it starts from the right.
str5 = ' I Love LsgoGroup '
print(str5.strip().partition('o'))  # ('I L', 'o', 've LsgoGroup')
print(str5.strip().partition('m'))  # ('I Love LsgoGroup', '', '')
print(str5.strip().rpartition('o'))  # ('I Love LsgoGr', 'o', 'up')
  • split(str="", num) has no parameters. The default is to slice strings with spaces as separators. If the num parameter is set, only num substrings will be separated and a list of spliced substrings will be returned.
str5 = ' I Love LsgoGroup '
print(str5.strip().split())  # ['I', 'Love', 'LsgoGroup']
print(str5.strip().split('o'))  # ['I L', 've Lsg', 'Gr', 'up']
u = "www.baidu.com.cn"
# Use default separator
print(u.split())  # ['www.baidu.com.cn']

# Use '.' as separator
print((u.split('.')))  # ['www', 'baidu', 'com', 'cn']

# Split 0 times
print((u.split(".", 0)))  # ['www.baidu.com.cn']

# Split once
print((u.split(".", 1)))  # ['www', 'baidu.com.cn']

# Split twice
print(u.split(".", 2))  # ['www', 'baidu', 'com.cn']

# Split twice and take the item with sequence 1
print((u.split(".", 2)[1]))  # baidu

# Split twice, and save the divided three parts to three variables
u1, u2, u3 = u.split(".", 2)
print(u1)  # www
print(u2)  # baidu
print(u3)  # com.cn

Remove line breaks

c = '''say
hello
baby'''

print(c)
# say
# hello
# baby
print(c.split('\n'))  # ['say', 'hello', 'baby']
  • Splitlines ([keepers]) are separated by lines ('\ r', '\ r\n', \ n ') and return a list containing lines as elements. If the parameter keepers is False, it does not contain line breaks. If it is True, it retains line breaks
str6 = 'I \n Love \n LsgoGroup'
print(str6.splitlines())  # ['I ', ' Love ', ' LsgoGroup']
print(str6.splitlines(True))  # ['I \n', ' Love \n', ' LsgoGroup']
  • Maketrans (inb, outtab) creates a character mapping conversion table. The first parameter is a string, which represents the characters to be converted, and the second parameter is also a string, which represents the target of conversion.
  • translate(table, deletechars = "") convert the characters of the string according to the table given by the parameter table, and put the characters to be filtered into the deletechars parameter.
str7 = 'this is string example....wow!!!'
intab = 'aeiou'
outtab = '12345'
trantab = str7.maketrans(intab, outtab)
print(trantab)  # {97: 49, 111: 52, 117: 53, 101: 50, 105: 51}
print(str7.translate(trantab))  # th3s 3s str3ng 2x1mpl2....w4w!!!
  • Python string formatting symbol
Symboldescribe
%cFormatted characters and their ASCII codes
%sFormat the string and process the object with the str() method
%rFormat the string and process the object with the rper() method
%dFormat integer
%oFormat unsigned octal number
%xFormat unsigned hexadecimal number
%XFormat unsigned hexadecimal number (upper case)
%fFormat floating-point numbers to specify the precision after the decimal point
%eFormatting floating point numbers with scientific counting
%EThe function is the same as%e, format floating-point numbers with scientific counting method
%gUse% f or% e depending on the size of the value
%GThe function is the same as% g. use% f or% E according to the size of the value
  • Formatting operator helper
Symbolfunction
m.nm is the minimum total width of the display and n is the number of digits after the decimal point (if available)
-Use as left alignment
+Displays a plus sign (+) before a positive number
#Zero ('0 ') is displayed in front of octal numbers and' 0x 'or' 0x 'is displayed in front of hexadecimal numbers (depending on whether' x 'or' x 'is used)
0The displayed number is preceded by '0' instead of the default space\

dict dictionary

Variable type and immutable type
Sequences are indexed by consecutive integers. The difference is that dictionaries are indexed by "Keywords". Keywords can be any immutable type, usually strings or numeric values.
Dictionary is the only mapping type in Python. String, tuple and list belong to sequence type.
So how to quickly determine whether a data type X is a variable type? Two methods:

Troublesome method: use the id(X) function to perform some operation on X and compare the IDS before and after the operation. If they are different, X is not variable. If they are the same, X is variable.
Convenient method: use hash(X) to prove that X can be hashed, that is, it is immutable, and vice versa.

aggregate

Similar to dict, set in Python is also a set of keys, but does not store value. Since keys cannot be repeated, there are no duplicate keys in set.

Set1 > = set2 determines whether the set contains other sets. If so, it returns True; otherwise, it returns False.

x = {"f", "e", "d", "c", "b", "a"}
y = {"a", "b", "c"}
z = x.issuperset(y)
print(z)  # True
print(x >= y)  # True

sequence

seasons = ['Spring', 'Summer', 'Fall', 'Winter']
a = list(enumerate(seasons))
print(a)  
# [(0, 'Spring'), (1, 'Summer'), (2, 'Fall'), (3, 'Winter')]

b = list(enumerate(seasons, 1))
print(b)  
# [(1, 'Spring'), (2, 'Summer'), (3, 'Fall'), (4, 'Winter')]

for i, element in a:
    print('{0},{1}'.format(i, element))
# 0,Spring
# 1,Summer
# 2,Fall
# 3,Winter

Posted by tyler on Mon, 08 Nov 2021 16:26:26 -0800