Getting Started with Python Week day04 (the set and character encoding)

1, a collection of

 1.1, the definition of

{} In the plurality of elements separated by a comma, a plurality of elements satisfies the following three conditions:
    <1 must be set within the element type is immutable.
    <Unordered collection of elements 2 within.
    <3 within the set of elements is not repeated.

 

1.2, type conversion

res = set ( 'hellolllll') # Type string revolutions set 
print (set ([1,1,1,1,1,1]) ) # set to automatically re- 
print (set ([1,1,1,1 , 1,1, [11,222]]) # error, the variable type is present in the collection 
print (set ({ 'k1' : 1, 'k2': 2})) # key set stored

1.3, built-in method

= {friends1 "ZERO", "Kevin", "Jason", "Egon"} 
friends2 = { "of Jy", "Ricky", "Jason", "Egon"} 
on the intersection: friend common to both 
print (friends1 & friends2) 
Print (friends1.intersection (friends2)) 
taken and set / collection: both all friends 
Print (friends2 | friends1) 
Print (friends1.union (friends2)) 
to take the set difference: take friends1 unique friend 
print (friends1 -friends2) 
Print (friends1.difference (friends2)) 
symmetric difference: the sum of two friends are unique to the user (that is, remove common friend) 
Print (friends1 ^ friends2) 
Print (friends1.symmetric_difference (friends2)) 

and his son set: relationship included 
s1 = {1,2,3,4} 
s2 = {l, 2,3} 
Print (s1> s2) is s1 s2 # Analyzing superset 
print (s1.issuperset (s2)) # s1 is determined s2 superset 
print (s1 <s2) Analyzing # s1 s2 set of sub 
print (s1.issubset (s2)) # determines a subset of s2 s1
# Inclusion relationship does not exist, are the following Comparative False 

S1 = {l, 2,3} 
s2 = {l, 2,3} 
Print (S1 == s2) # S1 and s2 each other Sons
Deduplication 
1, only to the weight for immutable type 
Print (SET ([1,1,1,1,2])) 

2, the original order can not guarantee 
l = [1, 'a' , 'b', ' Z ', 1,1,1,2] 
L = List (SET (L)) 
Print (L)  
Other operational length, the operational member, the cycle as before 

other built-in method
s = {1,2,3} 
built-in methods need to know. 1: discard 
s.discard (. 4) to remove elements # Nothing absence do 
Print (S) 
s.remove (. 4) # remove elements does not exist error 


need to know built method 2: Update 
s.update ({l, 3,5}) 
Print (S) 

need to have built-in methods. 3: POP 
RES = s.pop () 
Print (RES) 

need to have built-in methods. 4: the Add 
S. the Add (. 4) 
Print (S) 



remaining methods are all understood 
res = s.isdisjoint common portion ({3,4,5,6}) # set two completely independent, there is no return True 
Print (RES) 

Learn 
s.difference_update ({3,4, 5}) = S # s.difference ({3,4, 5}) 
Print (S)

2, character encoding

ASCII table:
1, only supports the English string
2, 8-bit binary number corresponding to an English string

GBK Table:
1, supports English characters, Chinese characters
2, 8-bit (8bit = 1Bytes) binary number corresponding to an English string
16-bit (16bit = 2Bytes) corresponds to a binary number string Chinese

Unicode (unified memory use Unicode):
1, compatible character nations
      and nations character has a corresponding relationship between the
2, 16-bit (16bit = 2Bytes) binary number corresponds to a Chinese string
      of individual rare will use 4Bytes, 8Bytes

 

Old character encodings can be converted to unicode, but can not unicode Huzhuan

 

utf-8:
  English -> 1Bytes
  characters -> 3Bytes

Conclusion:
1, the memory retention using unicode, we can change that to disk in a format
English + Chinese characters - "unicode-" gbk
English + Japanese - "unicode-" shift-jis
Palais character "-unicode-" utf-8

2, garbled text file access problems
exist in turmoil: the solution is, the encoding format should be set to support the paper format string
to take the chaos: the solution is, what files are encoded formats such as hard disk memory, it should be What encoding format is read into memory


3, python interpreter reads the default encoding file
python3 default: UTF. 8-
python2 Default: ASCII

Specify the file header to modify the default encoding:
the first line py file write:
#coding: GBK

4, to ensure that the two phases are not garbled core rule before running python program:
Specifies the file header
encoding format files originally stored in the hard disk used: # coding


. 5,
to python3 default type of direct deposit into str unicode format, in any case not be garbled
ensure python2 str type of distortion is not
x = u 'on'


6 for
python2 string interpreter has two types: STR, Unicode
# STR type
x = 'on' # string value specified by the header encoding format value stored in the variable memory space
# Unicode type
x = u ' the '# forcibly saved as unicode

  

 

  

Guess you like

Origin www.cnblogs.com/BoyGc/p/12470765.html