Meta-group
#为何要有元组, storing multiple values, tuples immutable, more are used to make queries t= (1,[1,3], ' SSS ', (UP)) ' (() ') # Print (Type (t)) # # # # # # Tuples can be used as dictionaries for key# d={: ' Egon '}# print (D,type (d), d[(+/-)]) # slice # goods= (' iphone ', ' Lenovo ', ' sanxing ', ' Suoyi ') # Print (Goods[1:3]) # length #in: #字符串: Substring # list: element # tuple: element # Dictionary: key# goods= (' iphone ', ' Lenovo ', ' sanxing ', ' Suoyi ') # print (' iphone ' In goods ' see is inside the element in not inside # d={' a ': 1, ' B ': 2, ' C ': 3}# print (' B ' in D) look at the key is not inside D #掌握 # goods= (' iphone ', ' Lenovo ', ' sanxing ', ' Suoyi ') # Print (Goods.index (' iphone ')) # Print (Goods.count (' iphone ')) #补充: The tuple itself is immutable, but the inner element can be a mutable type t= (1,[' A ', ' B '], ' SSS ', ()) #t =tuple ((1,[1,3], ' SSS ', ()) # t[1][0]= ' A ' # print (t) # t[1]= ' Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa '
Dictionary
Dictionary Common Methods
Dictionary representation: info_dic={' name ': ' Haiyna ', ' age ':, ' sex ': ' Female '}# common operations: #1. Save/Take info_dic={' name ': ' Egon ', ' age ': ', ' sex ': ' Male '} print (info_dic[' name11111111 ') #找不到则报错了 print (' Info_dic.get (' name ', None) ') Print (Info_dic.get (' name222222 ', None)) #get方法找不到不报错, you can set the default value #pop:key the presence of the pop-up value, does not exist to return the default value, if there is no default error # Print (Info_dic.pop (' nam123123123123123123e ', None) # Print (info_dic) # Print (Info_dic.popitem ()) # Print (info_dic) # info_dic[' Level ']=10# print (info_dic) # # Delete Info_ dic={' name ': ' Egon ', ' age ':, ' sex ': ' Male '}# info_dic.pop () # Info_dic.popitem () # del info_dic[' name ']## key s, value s, key value to info _dic={' name ': ' Egon ', ' age ':, ' sex ': ' Male '}# print (Info_dic.keys ()) # Print (Info_dic.values ()) # Print (info_ Dic.items ()) # for k in info_dic:# # Print (k,info_dic[k]) # print (k) # print (' ========> ') # for K in Info_dic.keys ( ): # print (k) # for Val in Info_dic.values (): # print (val) # for K,v in Info_dic.items (): #k, v= (' name ', ' Egon ') # p Rint (k,v) # length # info_dic={' name ': ' Egon ', ' age ':, ' sex ': ' Male '}# print (Len (Info_dic) # # loop # # contains in# info_dic={' name ': ' Egon ', ' age ': +, ' sex ': ' Male '}# print (' name ' in Info_dic) # print (' name ' in Info_dic.keys ()) # print (' Egon ' in Info_dic.values ()) # Print ("Name ', ' Egon ') in Info_dic.items ()) #掌握info_dic ={' name ': ' Egon ', ' age ':, ' sex ': ' Male '}# info_dic.update ({' A ': 1, ' name ': ' Egon '}) # print (info_dic) # info_dic[' Hobbies ']=[]# info_dic[' Hobbies '].append (' study ') # info_dic[' Hobbies '].append (' read ') # print (info_dic) #setdefault: Key does not exist set default value , and put back the value of the default value #key is not set by default, and return the already existing value # Info_dic.setdefault (' Hobbies ', [Max]) # print (info_dic) # Info_dic.setdefault (' Hobbies ', [1,2,3,4,5]) # print (info_dic) # info_dic={' name ': ' Haiyan ', ' age ': +, ' sex ': ' Male '}# {' name ': ' Egon ', ' age ': 18 , ' sex ': ' Male ', ' hobbies ': [' study ']}# info_dic.setdefault (' hobbies ', []). Append (' study ') # {' Name ': ' Egon ', ' age ': 18, ' Sex ': ' Male ', ' hobbies ': [' study ', ' read ']}# info_dic.setdefault (' hobbies ', []). Append (' read ') # {' Name ': ' Egon ', ' age ' : ' Sex ': ' Male ', ' hobbies ': [' study ', ' read ', ' Sleep ']}# info_dic.setdefault (' hobbies ', []). AppEnd (' sleep ') # L=info_dic.setdefault (' hobbies ', []) # Print (L,id (l)) # Print (ID (info_dic[' hobbies ')) # print (info_dic) # Learn # d=info_dic.copy () # print (d) # info_dic.clear () # Print (info_dic) # D=info_dic.fromkeys ((' Name ', ' age ', ' sex '), None) # Print (d) # D1=dict.fromkeys ((' Name ', ' age ', ' sex '), None) # D2=dict.fromkeys (' name ', ' age ', ' sex '), (' Egon ', ' Male ') # print (D1) # print (D2) # info=dict (name= ' Haiyan ', age=18,sex= ' male ') # print (info) # # Info=di
Set Set
The function of a set
Knowledge Point review: mutable types are non-hash types, immutable types are hash types
function: de-weight, relational operation
Definition: Can contain multiple elements, separated by commas, the elements of the collection follow three principles:
1. Each element must be an immutable type (hash, which can be used as a dictionary key)
2. No duplicate elements
4. Unordered
Note that the purpose of the collection is to store different values together, and to do relational operations between different sets, without having to tangle the individual values in the collection
Second, common methods
In and not in
| and the Set (print (Pythons.union (LINUXS)))
& Intersection (Print (pythons.intersection (LINUXS)))
-Subtraction (Print (Pythons.difference (LINUXS)))
^ Symmetric difference set (print (Pythons.symmetric_difference (LINUXS)))
==
>,>=, <,<= parent set (Issuperset), subset (Issuberset)
+ View Code?
123456789101112 |
>,>
=
,<,<
=
set1
=
{
1
,
2
,
3
,
4
,
5
}
set2
=
{
1
,
2
,
3
,
4
}
print
(set1 >
= set2)
print
(set1.issuperset(set2)) set1
=
{
1
,
2
,
3
,
4
,
5
}
set2
=
{
1
,
2
,
3
}
print
(set1<
=
set2)
print
(set1.issubset(set2))
|
Third, practice
I. Relational operations
With the following two collections, Pythons is the student name collection for the Python course, and Linuxs is a collection of student names enrolled in the Linux course.
pythons={' Alex ', ' Egon ', ' Yuanhao ', ' Wupeiqi ', ' Gangdan ', ' Biubiu '}
linuxs={' Wupeiqi ', ' Oldboy ', ' Gangdan '}
1. Find out who signed up for Python and enroll in the Linux class student name collection
2. Find a collection of all student names enrolled
3. Find out the name of the student who only enrolled in the Python course
4. Find a collection of student names without the same two courses
+ View Code?
123 |
pythons
=
{
‘haiyan‘
,
‘fank‘
,
‘yaling‘
,
‘lalal‘
,
‘haidong‘
,
‘biubiu‘
}
linuxs
=
{
‘six‘
,
‘wu‘
,
‘dabao‘
}
# 1. 求出即报名python又报名linux课程的学员名字集合
|
+ View Code?
12 |
pythons = { ' Haiyan ' ' yaling ' ' Lalal ' ' Haidong ' , ' Biubiu ' } Linuxs = { ' Six ' ' Dabao ' |
+ View Code?
12 |
print (pythons & linuxs) # 2. 求出所有报名的学生名字集合 |
+ View Code?
12 |
pythons = { ' Haiyan ' ' yaling ' ' Lalal ' ' Haidong ' , ' Biubiu ' } Linuxs = { ' Six ' ' Dabao ' |
+ View Code?
12 |
print (pythons | linuxs) # 3. 求出只报名python课程的学员名字 |
+ View Code?
12 |
pythons = { ' Haiyan ' ' yaling ' ' Lalal ' ' Haidong ' , ' Biubiu ' } Linuxs = { ' Six ' ' Dabao ' |
+ View Code?
12 |
print (pythons - linuxs) # 4. 求出没有同时这两门课程的学员名字集合 |
+ View Code?
12 |
pythons = { ' Haiyan ' ' yaling ' ' Lalal ' ' Haidong ' , ' Biubiu ' } Linuxs = { ' Six ' ' Dabao ' |
+ View Code?
1 |
print (pythons ^ linuxs) |
Iv. methods
+ View Code?
+ View Code?
1 |
linuxs = { ‘six‘ , ‘wu‘ , ‘dabao‘ } |
+ View Code?
12345 |
linuxs.add( ‘xiaoxiao‘ ) #说明set类型的集合是可变类型 linuxs.add([ 1 , 2 , 3 ]) #报错,只能添加不可变类型 print (linuxs) # 2. |
+ View Code?
1 |
linuxs = { ‘six‘ , ‘wu‘ , ‘dabao‘ } |
+ View Code
Python learning "2nd": Python data type (2)