Python Study Notes-list

Source: Internet
Author: User

Create

 
>>> List = ["A", "B", "C", "D", "E"] >>> list ['A', 'B ', 'C', 'D', 'E']

Obtains an element or substring.

 
>>> List = ["A", "B", "C", "D ", "E"] # obtain the first element >>> list [0] 'A' # obtain the last element >>> list [-1] 'E' # If the index value exceeds beyond the list length,> list [10] traceback (most recent call last): file "<stdin>", line 1, in <module> indexerror: list Index out of range # Use slice to simulate the Javascript slice function and obtain the substring >>> list [] ['B', 'C ', 'D'] # The default start index of the slice is 0 >>> list [: 3] ['A', 'B ', 'C'] # The default start index of the slice is-1 >>> list [3:] ['D ', 'E'] # The length of the list is automatically corrected when the range is exceeded> list [] ['D', 'E']

Replace

>>> List = ["A", "B", "C", "D ", "E"] # Replace a single element >>> list [3] = "D" >>> list ['A', 'B', 'C', 'D ', 'E'] # Replace n elements >>> list [: 3] = ["A", "B", "C"] >>> list ['A ', 'B', 'C', 'D', E] # extension by replacement, a bit like JavaScript splice >>> list [-1:] = ["E", "F"] >>> list ['A', 'B', 'C', 'D', 'E ', 'F'] # shrink by replacement >>> list [-2:] = "E" >>> list ['A', 'B', 'C ', 'D', 'E']

Append

>>> List = ['B']

# Push an element. Note that this method does not return a value.
>>> List. append ('E ')
>>> List
>>> ['B', 'E']

# Add an element to a specified position
>>> List. insert (0, 'A ')
>>> List
['A', 'B', 'E']

# Add multiple intangible objects to a specified position
>>> List [2: 2] = ['C', 'D']
>>> List
['A', 'B', 'C', 'D', 'E']

Merge

>>> List1 = ['A', 'B', 'C'] >>> list2 = ['D ', 'E'] # Use the + operator to generate a brand new list> list3 = list1 + list2> list3 ['A', 'B', 'C ', 'D', 'E'] >>> list1 ['A', 'B', 'C'] # Use extended functions on the original list, note that this method does not return values >>> list1.extend (list2) >>> list1 ['A', 'B', 'C', 'D', 'E']

Detection

 
>>> List = ["A", "B", "C", "", "B"] # determine whether it is a member of the target list> "B" in listtrue> "e" in listfalse # You can use the index method to obtain location >>> list. index ("A") 0 # But an error will be reported when it is not found, shit, what is the broken design >>> list. index ("e") traceback (most recent call last): file "<stdin>", line 1, in

  
   
Valueerror: List. Index (x): x not in list

  

Delete

>>> List = ['A', 'B', 'C', 'C ', 'C'] # Pop functions similar to JavaScript >>> list. pop () 'B' >>> list ['A', 'B', 'C ', 'C'] # JavaScript shift function can be implemented by passing parameters >>> list. pop (0) 'A' >>> list ['A', 'A', 'B', 'C ', 'C'] # specifies the removed element. Only one element is removed at a time. >>> list. remove ('B') >>> list ['A', 'A', 'B', 'B', 'C ', 'C'] # if it does not exist, an error will be reported, which is very unfriendly >>> list. remove (10) traceback (most recent call last): file "<stdin>", line 1, in

  
   
Valueerror: list. remove (x): x not in list # Clear all 'B' elements >>> while 'B' in list: list. remove ('B')... >>> list ['A', 'A', 'C ', 'C'] # Remove a group of elements> Del list [] # Same as list [] = []> list ['A ', 'C'] # Clear all> Del list [:] # Same as list [:] = []> list []

  

Statistics

>>> List = ['A', 'A', 'B', 'C'] >>> Len (list) 4 # Return the number of times a appears in the list >>> list. count ('A') 2

List Parsing

 
>>> S = [x ** 2 for X in range (10)] >>> v = [2 ** I for I in range (13)] >>> M = [X for X in S if x % 2 = 0] >>>>> print s; print V; print M [0, 1, 4, 9, 16, 25, 36, 49, 64, 81] [1, 2, 4, 8, 16, 32, 64,128,256,512,102 4, 2048,409 6] [0, 4, 16, 36, 64]
 
>>> Noprimes = [J for I in range (2, 8) for J in range (I * 2, 50, I)] >>> primes = [X for X in range (2, 50) If x not in noprimes]> Print primes [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47]
>>> Words = 'the quick brown fox jumps over the lazy dog '. split () >>> print words [the ', 'quick', 'Brown ', 'fox', 'jumps', 'over', 'the ', 'Lazy ', 'Dog'] >>>>> stuff = [[W. upper (), W. lower (), Len (w)] for W in words] >>> for I in stuff :... print I... [The ', 'the', 3] ['quick ', 'quick', 5] ['brown ', 'Brown', 5] ['fox ', 'fox', 3] ['jumps', 'jumps', 5] ['over', 'over', 4] ['the ', 'the ', 3] ['Lazy ', 'Lazy', 4] ['Dog', 'Dog', 3] >>>>> stuff = map (lambda W: [W. upper (), W. lower (), Len (w)], words) >>> for I in stuff :... print I... [The ', 'the', 3] ['quick ', 'quick', 5] ['brown ', 'Brown', 5] ['fox ', 'fox', 3] ['jumps', 'jumps', 5] ['over', 'over', 4] ['the ', 'the ', 3] ['Lazy ', 'Lazy', 4] ['Dog', 'Dog', 3]

Flat a two-dimensional list

>> list = [[1, 3, 5], [2, 4] >>> [flatten for inner in list for flatten in inner] [1, 3, 5, 2, 4] 

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.