Python Path-Basics (i): File processing, lists, tuples, dictionaries, set sets

Source: Internet
Author: User
Tags set set shallow copy

file processing

File processing mode:

[r] Open in read-only mode (read only) [RB] opens in binary read mode

[W] Open in write-only mode (empty file before writing) [WB] opens in binary write mode

[A] Open in Append mode (append to end of file) [AB] opens in binary append mode

[r+] Open in read-write mode [rb+] in binary read-write mode

[w+] Open in read-write mode [wb+] in binary read-write mode

[A +] open in read-write append mode [ab+] Open in binary read-write append mode

File operations

Open File

f = open (' File path ', ' mode ')

Manipulating files

F.readline ()  # # #仅读取一行数据f. ReadLines () # # #读取所有数据f. Write ()       # # # #写内容

Close File

F.close ()
string                                                                                                                                                                                          

First, the String method

Startswitch: is xxx opening

Endswitch: is XXX end

Strip: Returns a string that removes spaces on both sides

Split: Provides a delimiter to split a string into a list

Join: Specifies a delimiter that is used to concatenate elements in a sequence

Replace: Replaces an element in a string and returns the replaced result (all occurrences)

Second, the method instance operation

Join: Specifies a delimiter that is used to concatenate elements in a sequence

>>>seq = [' 1 ', ' 2 ', ' 3 ', ' 4 ', ' 5 ']>>>b = ' + '    #b为提供的分隔符, if the delimiter is not provided, use a space as a delimiter >>>print (b.join (seq)) 1+2+3+4+5

Replace: Replaces an element in a string and returns the replaced result (all occurrences)

>>>a = ' This was a test ' >>>print (A.replace (' is ', ' si ')) thsi si a test

Split: Provides a delimiter to split a string into a list

>>>a = ' 1+2+3+4+5 ' >>>print (a.split (' + ')) [' 1 ', ' 2 ', ' 3 ', ' 4 ', ' 5 ']

Strip: Returns a string that removes spaces on both sides

>>>a = '  hello,world!   ' >>>print (A.strip ()) hello,world!
sequence                                                                                                                                                                                        

First, the concept

A sequence is a set of sequential elements

There are two types of sequences: tuple (tuple), list

Second, List

Concept:

After the establishment can be deleted and modified

List method:

Append: Appends a new object to the end of the list

Insert: Inserts an object at the specified index location

Reverse: Reverse-store elements in the list

Index: Find a worthwhile index position in the list

Count: Count the number of occurrences of an element in a list

Remove: Remove one of the list's worthy first matches

Extend: Appends multiple values of another list at the end of a list

Sort: Sorting a list

List Method Instances

The corresponding values are listed according to the index:

>>> name_list = [' Zhao ', ' Qian ', ' Sun ']>>> name_list [' Zhao ', ' Qian ', ' Sun ']>>> name_list[0]< c0/> ' Zhao '

Append: Appends a new object to the end of the list

>>> name_list [' Zhao ', ' Qian ', ' Sun ']>>> name_list.append (' li ')   >>> name_list[' Zhao ', ' Qian ', ' sun ', ' Li '

Insert: Inserts an object at the specified index location

>>> Name_list.insert (2, ' a ')  >>> name_list[' Zhao ', ' Qian ', ' A ', ' sun ', ' Li ']

Remove: Remove one of the list's worthy first matches

>>>name_list [' Qian ', ' A ', ' sun ', ' Li ']>>>name_list.remove (' Zhao ') >>> name_list[' Qian ' , ' A ', ' sun ', ' Li '

Count: Count the number of occurrences of an element in a list

>>>name_list[' Zhao ', ' Qian ', ' sun ', ' Li ', ' Zhao ']>>>name_list.count (' Zhao ')   2

Index: Find a worthwhile index position in the list

>>> name_list[' Zhao ', ' Qian ', ' sun ', ' Li ', ' Zhao ']>>> name_list.index (' Qian ')    1

Extend: Appends multiple values of another list at the end of a list

>>>a=[1,2,3]>>>b=[4,5,6]>>>a.extend (b) >>>print (a) [1,2,3,4,5,6]

Intercept when you know the index location

>>> name_list[' Zhao ', ' Qian ', ' sun ', ' Li ', ' Zhou ', ' Wu ', ' Zheng ', ' Wang ']>>>name_list [0:5]         # Intercept 0-5 lines [' Zhao ', ' Qian ', ' sun ', ' Li ', ' Zhou ', ' Wu ']

Intercept without knowing the index position

>>> name_list[' Zhao ', ' Qian ', ' sun ', ' Li ', ' Zhou ', ' Wu ', ' Zheng ', ' Wang ']>>>name_list[name_ List.index (Zhao): Name_list.index (Zhao) +4]  #从zhao开始截取四个包括zhao [' Zhao ', ' Qian ', ' sun ', ' Li ']

Three, meta-group

Concept:

Once established, the elements of the tuple cannot be modified and can only be read

Tuple method

CMP (TUPLE1,TUPLE2): Comparing elements of two tuples

Tuple (a): Convert List A to Narimoto group

To create a tuple:

>>>tuple = (' Zhao ', ' Qian ', ' Sun ')

To delete an entire tuple:

>>> del tuple
Dictionaries                                                                                                                                                                                            

First, the concept

Quickly locate related values, Key,vale. Key is the value of the keys value

Second, the characteristics

Disorder of

Three, the method of the dictionary:

Clear: Clears all items in the dictionary

Items: Returns all items in the dictionary as a list

Copy: Returns a new dictionary of the same key-value pair (shallow copy)

Pop: Specify a build, and then remove this key-value pair from the list

Fromkeys: Creates a new dictionary with the given key, value is None

Update: Updates a dictionary item to another dictionary (the latter replaces the former) method instance

Iv. Examples of dictionary methods

Clear: Make sure all the items in the dictionary

Code: People = {' Zhangsan ': +, ' Lisi ': 18}print (people) print (people.clear ()) Result: {' Lisi ': ' Zhangsan ': 25}none

Copy: Returns a new dictionary of the same key-value pair (shallow copy)

Code: People = {' Zhangsan ': +, ' Lisi ': 18}print (people) print (people.copy ()) Result: {' Lisi ': ' Zhangsan ': 25}{' Lisi ': 18, ' Zhangsan ': 25}

Fromkeys: Creates a new dictionary with the given key, value is None

Code: Dice = {}print (Dict.fromkeys ([' Zhangsan ', ' Lisi ')) Result: {' Lisi ': none, ' Zhangsan ': none}

Items: Returns all items in the dictionary as a list

Code: People = {' name ': ' Zhangsan ', ' age ': '}print ' (People.items ()) Result: [(' Age ', ' ' + '), (' Name ', ' Zhangsan ')]

Pop: Specify a build, and then remove this key-value pair from the list

Code: People = {' name ': ' Zhangsan ', ' age ': '}print ' (people) print (People.pop (' name ')) print (people) Result: {' age ': ' 25 ', ' Name ': ' Zhangsan '}zhangsan{' age ': ' 25 '}

Update: Updates one dictionary item to another dictionary (the latter replaces the former)

Code: People  = {' name ': ' Zhangsan ', ' sarlay ': 6000}people2 = {' name ': ' Zhangsan ', ' Sarlay ': 12000}print (people) print ( People2) Print (People.update (people2)) print (people) Result: {' sarlay ': 6000, ' name ': ' Zhangsan '} {' Sarlay ': 12000, ' name ': ' Zhangsan '}none{' sarlay ': 12000, ' name ': ' Zhangsan '}

Find Value by key

Code: People  = {' name ': ' Zhangsan '}print (people[' name ') ') Result: Zhangsan

Returns all keys in the dictionary

Code dic = {' Tom ': One, ' Sam ': $, ' Lily ': 100}print (Dic.keys ()) results [' Lily ', ' Sam ', ' Tom ']

Returns all the value in the dictionary

Code dic = {' Tom ': One, ' Sam ': $, ' Lily ': 100}print (Dic.values ()) Results [100, 57, 11]
Set Set                                                                                                                                                                                            

First, the concept:

Fast access, can not be repeated, unordered

Two, set Set method:

Add: Adds an element to the collection

Discard: Removes an element from the collection and does not error if the element does not exist

Remove: Removes an element from the collection and an error if the element does not exist

Pop: Randomly deletes an element and returns the element

Union

Difference: Take the difference of two sets

  

  

 

 

  

Python Path-Basics (i): File processing, lists, tuples, dictionaries, set sets

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.