1. List Features:
a. enclose in square brackets
b. Comma separates each element
c. The elements in the list can be numbers, strings, lists, booleans... all of them can be put in
d. A list is a collection, anything can be placed inside it
e. You can get the value through the index, you can always look inside
li = [1, 2, 3, ['yu', 'gh', 'jk', ['py', 16, 'op'], 23], True, 'cyx']
v = li [3] [ 3] [0] [1]
print (v)
f. You can take values through slices, and the result of slices is also a list
li = [1, 2, 3, ['yu', 'gh', 'jk', ['py'], 23], True]
print (li [3])
print (li [2: 4])
g. Support for loop, support while loop
li = [1, 2, 3, ['yu', 'gh', 'jk', ['py'], 23], True, 'cyx']
for item in li:
print(item)
h. Once the string is created and cannot be modified, the list elements can be modified, because the list is stored in the memory in the form of a linked list. The linked list indicates that the elements are not continuous in the memory, but the position of the previous element will also store the next element. Location information, which can be modified by indexing or slicing
The string can be valued by index, but the value cannot be modified (because the string cannot be changed after it is created)
The list can either take a value by index, or modify the value by index
li = [1, 2, 3, ['yu', 'gh', 'jk', ['py'], 23], True, 'cyx']
li [1] = [11, 22, 33, 44 ]
print (li)
li = [1, 2, 3, ['yu', 'gh', 'jk', ['py'], 23], True, 'cyx']
li [1: 3] = [20, 30]
print (at the)
i. Delete, which can be deleted by index or slice
li = [1, 2, 3, ['yu', 'gh', 'jk', ['py'], 23], True, 'cyx']
del li [1]
print (li)
li = [1, 2, 3, ['yu', 'gh', 'jk', ['py'], 23], True, 'cyx']
del li [0: 4]
print (li)
j. Support in operation
li = [1, 2, 3, ['yu', 'gh', 'jk', ['py'], 23], True, 'cyx']
v = 20 in li
print(v)
k. Convert the string to a list, and the list uses a for loop internally
s = 'qwertyuioopasdfghjkl'
v = list (s)
print (v)
l. Because numbers cannot be used for for loops, and lists are converted using for loops, numbers cannot be converted to lists with lists
m. Convert list to string
m1. Write a for loop: there are both numbers and strings in the list
s = ''
li =[11, 22, 33, '123', 'alex']
for i in li:
s = s + str(i)
print(s)
s = ''
li = [1, 2, 3, ['yu', 'gh', 'jk', ['py', 16, 'op'], 23], True, 'cyx']
for i in li:
s = s + str(i)
print(s)
m2. Using str conversion can only add '' to the whole list, and cannot remove the comma inside the list
li = [1, 2, 3, ['yu', 'gh', 'jk', ['py', 16, 'op'], 23], True, 'cyx']
v = str (li)
print (v)
m3.''.join(): can be used when only strings
li =['123', 'alex']
s = ''.join(li)
print(s)
o. In python, none represents a null value, and iterable represents an iterable object (string, chat table)
p.append: the original value is appended at the end
li = [11, 22, 33, 44]
li.append (5)
print (li)
q.clear: clear
li = [11, 22, 33, 44]
li.clear ()
print (li)
r.copy: shallow copy
li = [11, 22, 33, 44]
v = li.copy ()
print (v)
s.count: count the number of times the element appears
li = [11, 22, 33, 44, 22]
v = li.count (22)
print (v)
t.extend: extends the original list, there is a for loop inside, so the parameter needs to be an iterable object
append: Append the parameter as a whole
li = [11, 22, 33, 44, 22]
li.append ([5656, 'cyx'])
print (li)
li = [11, 22, 33, 44, 22]
li.extend ([5656, ' cyx '])
print (li)
li = [11, 22, 33, 44, 22]
for i in [5656,' cyx ']:
li.append (i)
print (li)
li = [11, 22, 33, 44, 22]
li.extend('Set it up')
print(li)
u.index: Get the index position of the current value according to the value. By default, the left side is preferred. If you find the first one, you will not find it. You can set the start and end positions.
li = [11, 22, 33, 44, 22]
v = li.index (22)
print (v)
v.insert : inserts an element at the specified index
li = [11, 22, 33, 44, 22]
li.insert (2, 'cyx')
print (li)
w. pop deletes the last value by default, and gets the deleted value, you can add an index to specify deletion
li = [11, 22, 33, 44, 22]
v = li.pop ()
print (li)
print (v)
li = [11, 22, 33, 44, 22]
v = li.pop (3)
print (li)
print (v)
x.remove removes the specified value in the list, left first
li = [11, 22, 33, 44, 22]
li.remove (22)
print (li)
Note: pop remove del[0] del[0:3] clear means delete
y.reverse flips and reverses the current list
li = [11, 22, 33, 44, 22]
li.reverse ()
print (li)
z.sort sort, the default sort from small to large
Lists are ordered and elements can be modified
li = [11, 22, 33, 44, 22]
li.sort ()
print (li)
li = [11, 22, 33, 44, 22]
li.sort (reverse = True)
print (li)
2. Tuple: The secondary processing of the list, the writing format is plus (), the elements cannot be modified, and cannot be added or deleted.
Generally, when writing a tuple, add , in the last digit, and no error will be reported. In order to distinguish the tuple from the parameters
a. The index value, you can always look inside
tu = (11, 22, 33, 44, 22)
v = tu [0]
print (v)
tu = (11, 22, 33, [(33,44)], 44, 22,)
v = tu [3] [0] [0]
print (v)
b. Slice value, the result is still a tuple
tu = (11, 22, 33, 44, 22)
v = tu [0]
print (v)
c.for loop, so tuples are also iterable objects
tu = (11, 22, 33, 44, 22)
for item in tu:
print(item)
d. Strings, lists, and tuples are all iterable objects, and both strings and lists can be converted into tuples
li = [11, 22, 33, 44, 22]
v = tuple(li)
print(v)
s = 'sgjdkhie'
v = tuple(s)
print(v)
tuple to list
tu = (11, 22, 33, 44, 22)
v = list (tu)
print (v)
tuple to string
1) When the elements are all strings
tu = ('hui', 'qwe')
v = ''.join(tu)
print(v)
2) When the element is a combination of strings and numbers, a for loop needs to be written
s = ''
tu = (123, 'qwe')
for i in tu:
s = s + str(i)
print(s)
e. Tuples are also ordered
f. The first-level elements of the tuple cannot be modified, deleted, or added
tu = (11, 22, 33, [(33,44)], 44, 22,)
tu[3][0] = 567
print(tu)
33 in [(33,44)] is not modifiable because it belongs to the first-level element of (33,44)
g.count: Get the number of times the specified element appears in the tuple
h.index gets a value index position
3. Dictionary dict:
a. Basic structure
comma-separated, one element is called a key-value pair
The value in the key-value pair can be any value and can be nested
Numbers, strings, tuples (cannot be modified), booleans (may be repeated with 0, 1) can be used as key values, lists (dynamic) cannot be used as dictionary keys
When the dictionary is saved, it is stored according to the hash table. The dictionary generates a table in the memory, and the key is converted into a bunch of numbers in the memory. There is a corresponding relationship between the numbers.
Create a dictionary:
info = {'k1': 'v1', 'k2': 'v2'}
b. The dictionary is unordered, and the order of elements will change every time you print
c. Index method to find the specified value
info = {'k1': 18, 'k2': True, 'k3': [11, [], (), 22, 33,{'kk1': 'vv1', 'kk2': 'vv2', 'kk3': (11, 22)}], 'k4': (11, 22, 33, 44)}
v = info['k1']
print(v)
info = {'k1': 18, 'k2': True, 'k3': [11, [], (), 22, 33,{'kk1': 'vv1', 'kk2': 'vv2', 'kk3': (11, 22)}], 'k4': (11, 22, 33, 44)}
v = info['k3'][5]['kk3'][0]
print(v)
d. Support del deletion
info = {'k1': 18, 'k2': True, 'k3': [11, [], (), 22, 33,{'kk1': 'vv1', 'kk2': 'vv2', 'kk3': (11, 22)}], 'k4': (11, 22, 33, 44)}
del info['k1']
print(info)
info = {'k1': 18, 'k2': True, 'k3': [11, [], (), 22, 33,{'kk1': 'vv1', 'kk2': 'vv2', 'kk3': (11, 22)}], 'k4': (11, 22, 33, 44)}
del info['k3'][5]['kk1']
print(info)
e. While loop is not supported, for loop is supported. By default, only key is output in for loop, or only value can be defined, or key-value pair can be defined to obtain
info = {'k1': 18, 'k2': True, 'k3': [11, [], (), 22, 33,{'kk1': 'vv1', 'kk2': 'vv2', 'kk3': (11, 22)}], 'k4': (11, 22, 33, 44)}
for item in info:
print(item)
info = {'k1': 18, 'k2': True, 'k3': [11, [], (), 22, 33,{'kk1': 'vv1', 'kk2': 'vv2', 'kk3': (11, 22)}], 'k4': (11, 22, 33, 44)}
for item in info.keys():
print(item)
info = {'k1': 18, 'k2': True, 'k3': [11, [], (), 22, 33,{'kk1': 'vv1', 'kk2': 'vv2', 'kk3': (11, 22)}], 'k4': (11, 22, 33, 44)}
for item in info.values():
print(item)
info = {'k1': 18, 'k2': True, 'k3': [11, [], (), 22, 33,{'kk1': 'vv1', 'kk2': 'vv2', 'kk3': (11, 22)}], 'k4': (11, 22, 33, 44)}
for k, v in info.items():
print(k, v)
When the ek value is repeated, only one can be retained; when the Boolean value is repeated with 0 or 1, it cannot be displayed; when there is no 0 or 1, the Boolean value can be displayed normally
info ={ 1: 'asdf', "k1": 'asdf','k1': "123",(11,22): 123,}
print(info)
info ={ 1: 'asdf', "k1": 'asdf', True: "123",(11,22): 123,}
print(info)
info ={ 7: 'asdf', "k1": 'asdf', True: "123",(11,22): 123,}
print(info)
f.clear/copy
g.fromkeys creates a dictionary based on the sequence and specifies a uniform value
dic ={ 'k1': 'v1', "k2": 'v2'}
v = dict.fromkeys(['qw', 123, 'uio'], 123)
print(v)
dic ={ 'k1': 'v1', "k2": 'v2'}
v = dict.fromkeys('dffhkji', 123)
print(v)
h. Get the key by indexing, if the key does not exist, an error will be reported
Get the key by get method. If the key does not exist, it will output a null value and no error will be reported. You can also specify the second parameter, which means that if the key does not exist, the second parameter will be returned by default. If the key value exists, the first parameter will be ignored directly. two parameters
dic = {'k1': 'v1', "k2": 'v2'}
v = dic.get('k333')
print(v)
dic = {'k1': 'v1', "k2": 'v2'}
v = dic.get('k333', 1111)
print(v)
When i.pop is deleted, the deleted value can be obtained. The parameter can be added with a default value. When the key does not exist, the default value will be returned.
dic = {'k1': 'v1', "k2": 'v2'}
v = dic.pop('k1')
print(dic, v)
dic = {'k1': 'v1', "k2": 'v2'}
v = dic.pop('k1111', 'lop')
print(dic, v)
j.popitem without parameters, delete key-value pairs randomly, and get the value
dic = {'k1': 'v1', "k2": 'v2'}
v = dic.popitem()
print(dic, v)
dic = {'k1': 'v1', "k2": 'v2'}
k, v = dic.popitem()
print(dic, k, v)
The function of k.setdefault is to set the value. If the key already exists, it will not be set, but the value corresponding to the current key will be obtained; if the key does not exist, it will be set and the value corresponding to the current key will be obtained.
dic = {'k1': 'v1', "k2": 'v2'}
v = dic.setdefault('k1', 123)
print(dic, v)
dic = {'k1': 'v1', "k2": 'v2'}
v = dic.setdefault('k1111', 123)
print(dic, v)
l.update covers the existing ones, and updates the ones that do not exist, using two calling methods
dic = {'k1': 'v1', "k2": 'v2'}
dic.update({'k1': 123, 'k3': 'uiop'})
print(dic)
dic = {'k1': 'v1', "k2": 'v2'}
dic.update(k1=123, k2=456, k3='klj')
print(dic)
m. Support in operation, the default judgment key
dic = {
"k1": 'v1'
}
v = "k1" in dic
print(v)
v = "v1" in dic.values()
print(v)
n.enumerate usage: get index and value at the same time
list1 = [ "this", "is", "one", "test"]
for index, item in enumerate(list1):
print index, item
>>>
0 this
1 is
2 a
3 test
list1 = [ "this", "is", "one", "test"]
for index, item in enumerate(list1, 1):
print index, item
>>>
1 this
2 is
3 a
4 test
4. Summary of data types and variables
a. Divide by variable and immutable
After reassignment, use id (variable name) to view, the id address changes, indicating that it is immutable, modify its value, the id address does not change, indicating that it is variable
mutable: list dictionary
Immutable: String Number Tuple
b. According to the order of access
Access in index order: tuple string list
Access by key value mapping: dictionary
Direct Access: Digital
c. Store the number of elements
container type: list tuple dictionary
Atomic Type: Number String
Dictionary query speed is faster than list, but dictionary occupies high memory
Must carry:
1. Number
int(..)
2. String
replace/find/join/strip/startswith/split/upper/lower/format
tempalte = "i am {name}, age : {age}"
# v = tempalte.format (name='alex',age=19)
v = tempalte.format(**{"name": 'alex','age': 19})
print(v)
3. List
append, extend, insert
index, slice , loop
four, tuple
ignore
index, slice, loop first-level elements cannot be modified
five, dictionary
get/update/keys/values/items
for, index
six, boolean value
0 1
bool(...)
None "" () [] {} 0 ==> False
dic = {
'plant':
{'herb':
['morning glory', 'cineraria', 'gourd', 'aster', 'winter wheat', 'beet'],
'woody':
[ 'Arbor', 'Shrub', 'Semi-shrub', 'Rusong', 'Chorus'],
'Aquatic plants':
['Lotus', 'Clanderum', 'Acorus', 'Acorus calamus', 'Water Onion', 'Zailihua', 'Barracuda grass']},
'Animal':
{'Amphibian':
['Mountain turtle', 'Mountain turtle', 'Stone frog', 'Salamander', 'Toad' , 'turtle', 'crocodile', 'lizard', 'snake'],
'avian':
['pheasant', 'raw chicken', 'long crocodile', 'changguo chicken', 'fighting cock', 'long tail chicken', 'silk bone'],
'Mammal':
['tiger', 'wolf', 'rat', 'deer', 'marten', 'monkey', 'barracuda', 'pixi', 'sloth', 'zebra', 'dog']}}