Python base Dict, collection, file

Source: Internet
Author: User
Tags readable

A dictionary is aKey:valueThe data type
dict1{
' Stud1 ': ' Sun Lizhao ',
' Stud2 ': ' Slz ',
' Stud3 ': ' Sunlizhao ',
}
Dict is unordered, and key is the only one born to go heavy.
Increase:
Dict1[' stud4 '] = ' Cang jing empty '
Modify:
dict1[' stud4 '] = ' enrique '
Delete:
Specify key deletion
Dict1.Pop(' stud3 ');
   deldict1[' STUD2 ']
Random deletion:
Dict1.Popitem ()
Find:
Standard usage:
   stud1 in Dict1
The value is found by key and does not exist to return none
Dict1.Get(' Stud1 ')
Nesting:
Av_catalog = {
"Europe and America": {
"www.youporn.com": ["a lot of free, the world's largest", "General quality"],
"www.pornhub.com": ["a lot of free, also very big", "quality than Yourporn High"],
"letmedothistoyou.com": ["mostly selfie, high-quality pictures many", "resources are not many, update slow"],
"x-art.com": ["high quality, really high", "all charges, Dick than please bypass"]
    },
"Japan and Korea": {
"Tokyo-hot": ["How the quality is not clear, the individual has not liked the Japanese and Korean fan", "Heard is charged"]
    },
"Continent": {
"1024": ["All free, really good, good Life Peace", "server in foreign countries, slow"]
    }
}

av_catalog["mainland" ["1024"][1] + = "can crawl down with crawlers"
print (av_catalog["Continent" ["1024x768"])
#ouput
[' All free, really good, good person life is safe ', ' server is abroad, slow, can crawl down with crawlers ']


Other poses:
The value of the entire dict
Dict1.values ()
The whole dict key.
Dict1.keys ()


Set the required element must be immutable: it cannot be a list, a dictionary, but he is mutable and cannot be used as a dictionary key .
Data type: integer floating-point string list tuple (tuple) Dictionary Boolean collection
A collection is an unordered, non-repeating combination of data, and its main functions are as follows:
Go to the weight, turn a list into a set, and then automatically go heavy.
  Relationship test, test the intersection of two sets of data, difference set, and the relationship between the set
s = set ([3,5,9,10,3,9])     create a collection of values
t = set ("Hello") Create a collection of unique characters  

Print (A.intersection(b))intersection, take the same content out for shorthand (a&b)
Print (A.Union(b)) #and set, a large set of a and B together in a shorthand (a|b)
Print (A.difference(b)) #Difference LevelA there are some, but b there is no shorthand ( A-B)
Print (A.symmetric_difference(b))Inverse intersection, take out the content that is not duplicated (a^b)

# Print (A.Issuperset(b))#bEach of the elements in ain aa>=b

T.Add(' X ') #AddOne item
S.Update([10,37,42]) # in SAdd multiple items
File operations
1 Open the file, get the file handle and assign a value to a variable
2 manipulating files with a handle
3 Closing files

The mode of opening the file is:

R, read-only mode (default).

W, write-only mode. "unreadable; not exist; create; delete content;"

A, append mode. "Readable; not exist" create; "only append content;"

r+, can read and write files. "readable; writable; can be added"

w+, write and read

A +, with a

"U" means that the \ r \ n \ r \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ r or r+ mode can be converted automatically when reading

"B" means processing binary files (e.g. FTP send upload ISO image file, Linux can be ignored, Windows process binary file should be labeled)

RB WB AB

  • w +   + Th style= "Text-align:center" > " TD style= "Text-align:center" > TD style= "Text-align:center" > TD style= "Text-align:center" >
    mode r r+ w+ a a+
    read +   +   +
    write + + + +
    create     + + + +
    overlay     + +    
    pointer at start + + + +    
    Pointer at the end + +
Import OS
f = open (' feb8-1.txt ', ' r+ ') opens the file
F. Write(' hello ') writes content
F.read() method is to extract all the data, parameters limit the number of extracts, Chinese and English occupy the same position
Print (FFF. ReadLine()) prints The single row of the current cursor
print (FFF. ReadLines ()) prints everything after the cursor ( multiline )
F. close () close Open file

f.< Strong>tell () file cursor position (medium: 3, UK: 1)
F. seek (3) artificially cursor is positioned to 3
F. flush () contents from memory written to disk
F.truncate([size]) intercepts the file , the truncated byte is specified by size, and the current file location is assumed by default.
Os. Rename (' current filename ', ' new filename ')   OS module renaming 
Os. Remove ( deleted filename ) OS module Delete file
Os. mkdir Create a new directory under current directory test
o S.  chdir                 ( "/home/newdir"       Current  directory changed to  '/home/newdir '  
os. GETCWD () work directory
The file prints row by line, and line sixth adds a string
# num = 0
# for I in Fff.readlines ():
# num + = 1
# If num = = 6:
# i = '. Join ([I.strip (), '----'])
# Print (I.strip ())
# Fff.close ()


Python base Dict, collection, file

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.