Study notes (October 23)--python Common methods

Source: Internet
Author: User

Two week class (October 23)
1. Common ways to learn Python lists
A list is made up of elements arranged in a sequence in a particular order. You can add anything from strings, numbers, dictionaries, and so on to the list, where there is no relationship between the elements. The list is also self-labeled, with the default starting from 0.
L = [' A ', ' B ', ' C ', ' abc ', 1, 2, 3]
Print type (l)
Print (dir (l))

Add an element to the end of the Append,list
L = [' A ', ' B ', ' C ', ' abc ', 1, 2, 3]
Print (L)
L.append (' Hello ')
Print (L)
Results:
[' A ', ' B ', ' C ', ' abc ', 1, 2, 3]
[' A ', ' B ', ' C ', ' abc ', 1, 2, 3, ' hello ']

Pop, delete a list element at the end
L = [' A ', ' B ', ' C ', ' abc ', 1, 2, 3]
L.pop ()
Print (L)
Results: [' A ', ' B ', ' C ', ' abc ', 1, 2]

Index, qualifying list subscript
L = [' A ', ' B ', ' C ', ' abc ', 1, 2, 3]
Print (L.index (' abc '))
Results: 3

Insert, inserting an element in the list
Format: Insert (Index,object)--insert object before index
L = [' A ', ' B ', ' C ', ' abc ', 1, 2, 3]
L.insert (2, ' 321 ')
Print (L)
Results: [' A ', ' B ', ' 321 ', ' C ', ' abc ', 1, 2, 3]

Remove, delete a list element
Format: L.remove (value)--remove first occurrence of value.
L = [' A ', ' B ', ' C ', ' abc ', 1, 2, 3]
L.remove (' abc ')
Print (L)
Results: [' A ', ' B ', ' C ', 1, 2, 3]

Reverse,list reverse Order
L = [' A ', ' B ', ' C ', ' abc ', 1, 2, 3]
L.reverse ()
Print (L)
Results: [3, 2, 1, ' abc ', ' C ', ' B ', ' a ']

Sort, sorting the list
L = [' A ', ' B ', ' C ', ' abc ', 1, 2, 3]
L.sort ()
Print (L)
Results: [1, 2, 3, ' A ', ' abc ', ' B ', ' C ']

List slices:
strings, lists, and tuples conform to the "sequence" feature in Python, and we can use slices (slice) to access any part of them, as long as the variables conform to this feature. We can think of the sequence as a queue, I might need the first three bits. After three bits, or four bits from the third position, or one by one, we use the slicing operator to achieve the above requirements.
The prototype of the slice operator in Python is:
[Start:stop:step]
That is: [Start index: End index: Step value]
Start index: Same as other languages, starting from 0. Sequence from left to right, the first worth index is 0, the last one is-1
End index: The slice operator is taken to the index and does not contain the value of the index.
Step value: The default is one after the other, if it is 2, it means to take one operation. The step value is positive when it is taken from left to right. If negative, a right-to-left fetch is indicated. The step value cannot be 0.
L = [' A ', ' B ', ' C ', ' abc ', 1, 2, 3]
Print (L[1:5:2])
Result: [' B ', ' ABC ']

2. Learn the collections and methods of Python
Meta-group
Tuple tuples are not to become list,tuple and list a little different

Definition form of 1.Tuple
t = (' A ', ' B ', ' C ', ' abc ', 1, 2, 3)
Print (dir (t))
Print (Type (t))
The difference between a tuple and a list is [] changed to (), no other changes, no tuple can be added and reduced, its own method is few, only count and index two methods, the other list method is not available.
2.Tuple single Element
A tuple is special when it comes to representing a single element, and we need to pay special attention, or else it's easy to make a mistake:
A = (1,)
b = (' abc ')
Print (Type (a))
Print (type (b))
Results:
<type ' tuple ' >
<type ' str ' >
As can be seen from the above example, a tuple must be separated when it represents a single element, otherwise the Python parser will not be considered a tuple.

Method of 3.Tuple
' Count ', counting the number of an element

t = (' A ', ' B ', ' C ', ' abc ', 1, 2, 3)
Print (T.count (' a '))
The result is: 1

' Index ' to return the subscript of an element
T.index (Value,[start, [Stop]), integer-return first index of value.
t = (' A ', ' B ', ' C ', ' abc ', 1, 2, 3)
Print (T.index (' abc '))
The result is: 3

3. Learn Python's dictionaries and methods
A dictionary is another mutable container model and can store any type of object.
Each key value of the dictionary (key=>value) pairs with a colon (:) split, each pair is separated by a comma (,), and the entire dictionary is included in curly braces {}, and the dictionary is assigned in three ways:
k = {' name ': ' Wan Yang ', ' age ': ' + ', 123: ' ABC '}
D = dict (A=1, b=2, c=3)
D = dict ([' Name ', ' Yang '), (' Age ', 20)])
Dictionaries have a number of common methods:
Get get
SetDefault settings
Keys
Values
Iteritems traversal
Update
Fromkeys
Zip merges two lists into a single dictionary
Pop Delete

To sort a dictionary:
Sort by value
MM = Dict (a=1,b=10,c=3,d=9)
Print sorted (Mm.iteritems (), key = Lambda d:d[1], reverse = True)


Study notes (October 23)--python Common methods

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.