String:
ordered, immutable data types, support indexing, slicing, steps (each letter or character string are called elements)
index (subscript): precise positioning of each element to index through the
index from the left rightward starting at row 0 from the beginning not exceed the maximum index, exceeds the given row from right to left starting from -1
(GU head regardless of the end) slice
step print (name [:: 2] to take the entire string, the step of 2
string method:
upper 全部大写 lower全部小写 capitalize 首字母大写 title每个单词的首字母大写 swapcase 大小写转换
starswith 以..开头 endswith 以..结尾
center 居中 (填充)
find 查找(通过元素查找索引,找不到返回-1) index 查找(通过元素查找索引,查找不到报错)
join 拼接 (将列表转化为字符串) print("_".join(["1","2","4"]))
count 统计 strip脱去两端的空格换行以及制表符
split 分割 (以空格和换行以及制表符进行分割,也可以以指定的方式进行分割)
replace 替换 format 字符串格式化
判断系列(is)
isdecimal 判断是否是十进制
isalnum 判断是否是字母中文 数字
isalpha 判断是否是字母中文
isdigit 判断是否是全部是阿拉伯数字
List (keyword: list): It is surrounded by [], every element separated by commas, can store various types of data
of variable data type support ordered indexing, slicing, and step CRUD
list by: append Extend iNSERT
iNSERT method to insert an object into the list, and append method is used at the end of the list of new objects added,
Extend put an iteration object to open each appended to the list out, the three of them. two have in common is that there is no return value.
Delete the list: the Remove the Clear POP del
. 1, POP pop out or specified index of the last element, and returns a value, a variable can be used to accept what was deleted when the specified index does not exist error.
2, remove the specified element is an element not delete the index, remove non-existent element will complain.
3, clear Clear List
4, del list the entire list to remove slices remove
del lst [1: 3] of the specified elements in the slice range for deletion
list change: Index, slice, step
index modifier li1 = [1, 2, 3, { 'a': 1 , 'b': 2}] li1 [0] = 'a' # 1 changes
the slice modifications (range or interval) with no steps or steps slices 1, the do not care the number of elements is equal to the modified sections.
Note: step 1 is not to pay attention to whether the value of the number of elements in the slice is equal to the change. If the step is not 1, pay attention to the number of elements must be the same.
Check list:
for loop
nested list: [......] as an element
Other list:
1, COUNT
query list appear the number of times an element
c = lst.count ( "Bai") # number of queries Bai appear
2, descending
lst.sort (Reverse = True)
lis.sort (FUNC = None , key = None, reverse = False (or True))
differs from that sort sorted is modified in situ, only a list, and returns a new sorted sequence, can be used in any one iteration object
3, reverse
situ flip
lst.reverse ()
. 4, len
length of the list
5、index
返回从左到右列表中某个元素出现的第一个索引值
List derivation formula
[variables (variables after processing) for the variable in the list] cycle mode
List sort () to add key parameters and operation method reverse
sort () is a python list sorting method, sort and reverse key method is often used in the other two optional parameters. To use keyword arguments, that is, to specify the name.
key in the sort () function in the role, we need to provide the function can be used as sort of use. For example, we have a list, the list element length to be sorted, then you will need to calculate the length len () function
X = [ 'Hello', 'ABC', 'iplaypython.com']
>>> X .sort (key = len)
>>> X
[ 'ABC', 'Hello', 'iplaypython.com']
of course, as sort key function, just len, as long as the functions corresponding to the writing according to your requirements can. If you need to sort integer numbers, we need to use int:
>>> a = ['3','188','50','1225']
>>> a.sort(key=int)
>>> a
['3', '50', '188', '1225']
sort()还有另外一个关键字参数,就是reverse,它是一个布尔值True/False,作用是用来决定是否要对列表进行反向排序
>>> a.sort(key=int,reverse=True)
>>> a
['1225', '188', '50', '3']
ST = L [1,23,4,5,7,8,9]
Print (lst.index (. 4)) # lookup indexed by the elements
lst.sort () # default sort ascending
lst.sort (reverse = True) # descending
print (lst)
lst = [1,23,4,5,7,8,9]
lst.sort()
lst.reverse() # 人工降序
print(lst)
lst = [1,23,4,5,7,8,9]
lst.reverse()
print(lst) # 将源数据进行反转
lst = [1,23,4,5,7,8,9]
lst1 = lst[::-1]
print(lst)
print(lst1) # 不修改源数据进行反转
lst = [1,2,3,4]
lst = lst + [1,2,3]
print(lst) # [1,2,3,4,[1,2,3],] [1,2,3,4,1,2,3]
lst = [1,2,3] * 5
print(lst)
print(id(lst[0]),id(lst[3]))
Tuple (keyword: tuple):
orderly, immutable data types (only check)
range range:
range (1,10) [Starting position: ending position] care regardless tail iterables
range (1, 10,) [Starting position: end position: step]
(10 represents end position, starting position defaults to 0)
was born to solve the range of numbers can not be recycled
Dictionary (keyword: dict): all operations by the key are
random variable data type
key: must be immutable data type values are arbitrary
字典的增:通过键值对添加(强加)
字典名['键]="值"
有则不加,无则不加
字典名.setdefault(键 , 值 )
字典的删:pop clear del 字典中没有remove
字典名.pop() 通过字典的键进行删除
字典名clear() 清空整个列表
del.字典名[''键”] 通过键进行删除
字典的改
字典名['键]="值"
update
d={'x':5}
c={'12':65}
c.update(d)
print(c)
字典的查
dic.get("键") 查询不到返回None,查找不到的时候,返回自己制定的内容
dic.setdefault(" 键 ")查询不到返回None
print(dic.keys( )) 获取一个键高仿列表
print(dic.values())获取一个值高仿列表
高仿列表支持迭代,不支持索引
for i in dic :
print(i,dic[i]) 打印键值
lst.append(i,dic[i])
for i in dic.items():
print(lst(dic.items()))
字典的其他:
dic = {"key":1,"key1":2,"key2":4,"key3":1}
print(dic.popitem()) # 随机删除 python3.6版删除最后一个键值对
# popitem返回的是被删除的键值对
print(dic)
Collection (keyword: set) is a set of values without a dictionary (natural de-emphasis)
a variable number of types of disorderly
line of code to re-
print (list (set (to the weight of the contents))
collection by: add (direct addition) update (iteration add)
to delete the collection: remove (specified element deletion) clear (empty) pop (random puncturing, usually minimal)
change the set: Add to delete the
check collection: the for loop
Other operations set
s1 = { "Liu can", "Zhao Si", "⽪ ⻓ ⼭"}
S2 = { "Branch ⻓ Liu", "rural ⻓ Feng", "⽪ ⻓ ⼭"}
Intersection
There are two sets of elements
print(s1 & s2) # {'⽪⻓⼭'}
print(s1.intersection(s2)) # {'⽪⻓⼭'}
Union
print (s1 | s2) # { ' Liu Section ⻓', 'von rural ⻓', 'Zhao Si', '⽪ ⻓ ⼭' 'can Liu'}
Print (s1.union (S2)) {# 'Liu Section ⻓ ',' von rural ⻓ ',' Zhao Si ',' ⽪ ⻓ ⼭ '' can Liu '}
Difference set
print (s1 - s2) # { ' Zhao Si', 'can Liu'} obtained in the presence of a first frame of a single
print (s1.difference (s2)) # { ' Zhao Si', 'can Liu'}
Anti intersection
print (s1 ^ s2) # present in two separate data set { 'von Township ⻓' 'can Liu', 'subjects ⻓ Liu', 'Zhao Si'}
Print (s1.symmetric_difference (S2)) {# ' Feng Township ⻓ '' can Liu ',' subjects ⻓ Liu ',' Zhao Si '}
S1 = { "Liu can", "Zhao Si"}
S2 = { "Liu can", "Zhao Si", "⽪ ⻓ ⼭ "}
⼦集
print (s1 <s2) # set1 set2 is set submenus do? True
Print (s1.issubset (s2))
Superset
print (s1> s2) # set1 set2 is a superset of it? False
Print (s1.issuperset (s2))
Various types of data conversion
str -- int
int -- str
str -- bool
bool -- str
int -- bool
bool -- int
list -- tuple 元组
lst = [1,23,5,4]
print(tuple(lst))
tuple -- list
tu = (1,23,5,4)
print(list(tu))
list -- set
lst = [1,23,12,31,23]
print(set(lst))
set -- list
tuple -- set
tu = (1,2,3,4,5)
print(set(tu))
set -- tuple
Currently conversion dictionary, their implementation
Example: a character string "k: 1 | k1: 2 | k2: 3 | k3: 4" processed into a dictionary { 'k': 1, ' k1': 2, 'k3': 4 }
DIC = {}
A = a.split ( '|')
Print (A)
for I in A:
Print (I)
I = i.split ( ':')
dic.setdefault (I [0], int (I [. 1]))
Print (DIC)
And a set of dictionaries can not modify the original size (length dictionary) while traversing (cycle), can be modified value
S = {1,2,3,4,5,6}
for I in S:
s.pop ()
Print (s)