A string
1.1 split a string:
s = "123*456*789*"
split_all = s.split("*") # ['123', '456', '789', '']
split_1 = s.split("*",1) # ['123', '456*789*']
1.2 character replacement
s = "123*456*789*"
s = s.replace("*","") # 123456789
1.3 substring count
This sub-string can be a single character, it can be a string
# str.count(sub, start= 0,end=len(string))
str = "123*456*789"
num = str.count("123") # 1
num = str.count("*") # 2
1.4 Regularization
Regularization need to use this package re
import re
s = "123*456*789"
s_split = re.split("\*", s) # ['123', '456', '789'] \*是正则语句
Two, list
2.1 Index
Multidimensional list element index, unlike np.array can write array [i, j] directly, but to write list [i] [j]
Delete the specified items 2.2
list = [1,2,1,1,3,4]
list.remove(1) # [2, 1, 1, 3, 4]
Adding 2.3 list
adding the list corresponds to append
a = [1,2]
a = a + [1] # [1,2,1] 相当于 a.append(1)
2.4, list of copy:
Do not use list.copy () This is a shallow copy, for multi-layer list is invalid. Use a copy of this package
import copy
list = [[1],[2]]
list2 = copy.deepcopy(list)
2.5 list sorting
the list.sort () only for the object list. For other iteration object, use the sorted () function. sorted Function Reference https://www.runoob.com/python/python-func-sorted.html
# list.sort(cmp=None, key=None, reverse=False) 默认从小到大排序
list = [4,6,2,7,8,1]
list.sort() # [1, 2, 4, 6, 7, 8]
# 方法二,可对所有可迭代的数排序
# sorted(iterable, cmp=None, key=None, reverse=False)
list2 = sorted(list)
Three, dictionary
3.1 ordinary dictionary:
dic = {}
dic["a"] = 1 # {'a':1}
3.2 collections package
This package contains a number of special dictionaries, reference: https://www.jianshu.com/p/f2a429aa5963
Which has a value with a default dictionary defaultdict. On an ordinary dictionary, a call there have been no key will complain, but collection.defaultdict, this key will assign a default value.
import collections
dic = collections.defaultdict(lambda:1) # 默认初始值是 1
dic = collections.defaultdict(lambda:[]) # 默认初始值为一个空的list
Dictionary 3.3 Other properties
Reference: https://www.runoob.com/python/python-dictionary.html
No. | Functions and Description |
---|---|
1 | dict.clear () All elements in the deleted dictionary |
2 | dict.copy () Returns a shallow copy of the dictionary |
3 | dict.fromkeys (seq [, val]) creates a new dictionary, the sequence elements do seq dictionary key, val a dictionary initial value corresponding to all keys |
4 | dict.get (key, default = None) Returns the specified key, if the return value is not in the dictionary default value |
5 | dict.has_key (key) if the key is in the dictionary dict returns true, otherwise returns false |
6 | dict.items () to return may traverse the list (key, value) tuples array |
7 | dict.keys () to return a list of all the keys dictionary |
8 | dict.setdefault (key, default = None) and get () is similar, but if the key does not exist in the dictionary, and will add value to default keys |
9 | dict.update (dict2) to dict2 dictionary of key / value pairs in the update to the dict |
10 | dict.values () all values in the list returned dictionary |
11 | pop (key [, default]) value of a given key to delete the dictionary corresponding to the key, the return value is deleted. key value must be given. Otherwise, return default values. |
12 | popitem () to go back and delete the last pair of keys and values in the dictionary. |
Fourth, digital
it (whether) | Absolute value |
num1 // num2 | Divisible |
num1 % num2 | Take the remainder |
num1 ** num2 | Index num1 ^ num2 |