Dictionary learning record 002 / conventional method string / file operation

 

【dictionary】

# 1 dictionary definition: the key and value were written in the form of data types

   # Pros: looks very intuitive, fast value

   # Features: Dictionaries are unordered, unlike list

info = {

    "guoyanna":"a",

    "jingna":"b",

    "zhangying":"c",

}

 

2 dictionary CRUD

A method of increasing 1

info [ "name"] = "   sichen" If the key does not exist increases, if the key is present, then changed to modify the value of the

A method of increasing 2

info.setdefault ( "car", "baoma     ") If the key exists without modification, directly increase

print(info)

delete

info.pop("guoyanna")

print(info)

 

del info["jingna"]

print(info)

To delete a random element

info.popitem ()

print(info)

 

The value

print (info [ "name"] ) # if an error would take absent

print (info.get ( "bucunzai") ) # If the value does not exist to take returns None , the recommended value mode; may be taken when a value less than a given value, written as follows

print(info.get("buzhidao","nan"))

 

Gets a dictionary of all the key

print(info.keys())

Gets a dictionary of all value

 

# Dictionary Empty

info.clear()

 

# Add the dictionary: second dictionary of key and value added to the first dictionary info inside

info.update (second dictionary's name)

 

# Determine whether the dictionary has a key: returns true or false

Print ( "Key name " in info)

 

# Judge a key if there is

print("zhangying"in info)

 

Direct circulation dictionary, then, is to take the dictionary key

for i in dictionary name :

    print(i)

 

While taking a dictionary key and value values:

for key, value in the dictionary name .items ():

    print("%s => %s”%(key,value))

 

Circular list - all elements inside output

l = [ 'liuzhao', 'liudonghai', 'zhaowenming', 'xiaoming', 'users']

for i in l:

    print(i)

String cycle - an output element which

a = "hello"

for i in a :

    print(i)

 

Commonly used method [string]

1 to remove the string inside the left and right sides of spaces:

  Print (String. Strip )

2 to remove the spaces around the string

  Left blank: string L. Strip

  The right space . Strings. Rstrip

3 replacement string values inside

  print (s.replace ( " old values ", " the value you want to change "))

4 to remove the intermediate space string

  print(s.replace("  ",""))

5 to find the number of times a character appears

  print(s.count("c"))

6 for a string inside the element index

  print (s.index ( "a") ) # find a non-existent element will complain

  print (s.find ( "a") ) # can not find returns -1 , not being given

7 boolean value: false to true

8 verify the correctness of returns true / false

  print ( variable .startswith ( "a")) # authentication string is not in a start

  print ( variable .endswith ( ". jpg")) # verification is not in the picture in .jpg format at the end

9 content output string to upper case

   Variable .upper ()

10 lowercase string contents are output:

  Variable .lower ()

11 yuan group

   11-001: Content tuples which are not to be modified

   11-002: If there is only one element tuple, then after the element must be added a comma

  11-003: represents tuple: t = (1,2,3)

   11-004: About two methods tuples: View the number of elements appear: t.count ( "element name"); t.index (subscript view elements)

Element 12 inside the string comma-separated by the following method, the string can be divided, a list of output - to what string element split, the split symbols which method is what, if not prevail string format

    Variable .split ( ",")

13 string into a list of elements inside, to facilitate post writable Dictionary - segmented elements output symbol string is determined by the preceding join that symbol

   L = [ "guo", "ya", "nan"]

   Print (“,”join(L))

   

Read mode: r

  You can only read, can not write, open a nonexistent file will complain

Write mode: w

  Only write, it will overwrite the contents of a file before; if the file does not exist create your own

Append mode: a

  In the original contents of the file based on the new content can be increased written, written in the last; if they will create the file does not exist

 

Read-write mode: r +

Read-write mode: w +

Additional read mode: a +

 

 

- efficient processing of files

1 using the cycle of operation for a file, each line may be obtained directly outputs the content file

- Modify the contents of the file

The first method: first read the contents, then replaced, remove the original contents of the file, the new content to write into

The second method: progressive process;

                  A first open a file, read a line written to modify a file inside an empty b from a file, delete a file, change the name of the file b is a file name

The benefits of using an open file with conduct

No need to manually close the file, it will automatically shut off when the need

Guess you like

Origin www.cnblogs.com/guodengdeng/p/10961236.html