List operations for 04-python

Source: Internet
Author: User

The list in Python is used the most, the commonly used methods are:

Append (value) extend (L)   Add all elements of the specified list insert (index, value)    inserts remove (value) Pop (index)        does not specify delete last, clear ()         empties count (value)    statistics how many sort ()          sorts reverse ()       to order copy ()          latent copy

The other:

1, List-derived

List push-to-type, simple list creation from sequence
Applies an action to each element of a sequence, using its result as a new list element
need to use [], tuple with ()
Each list is pushed to either create an expression after for, and then have 0-multiple for or if clauses that return the result
is a list based on the expressions generated from the for and if contexts, and if you want to push out a tuple, you must use the slogan
#x3get a new list of VEC= [2,4,6]new_vec= [3* x forXinchVec]print (New_vec) # get a mapdict= [(x, x**2) forXinchVec]print (dict) # Rent a call to elements in a sequence fruits= ['Banana','Apple','Oragin']new_fruits= [Fruit.upper () forFruitinchFruits]print (new_fruits) # using Ifif_vec= [3* x forXinchVecifX >3]print (If_vec) # 2 List of mixed vec2= [4,6, -1]lista= [x * y forXinchVec forYinchVec2]listb= [Vec[i] * Vec2[i] forIinchRange (VEC)]print (lista) print (LISTB)

Dictionary derivation

Ten: x**2 for in range (}print) (new_dict)

2, conversion of nested lists

For example, I want to convert a list of 3 * 4 to a list of 4 * 3

Matrix = [    [1,2,3,4],    [5,6,7,8],    [9,Ten, One, A]]new_matrix= [[Row[i] forRowinchMatrix forIinchRange4)]print (New_matrix) # above is equivalent to Matrix_1= [] forIinchRange4): Matrix_1.append ([row[i] forRowinchMatrix]) Print (matrix_1) # get further overwrite matrix_2= [] forIinchRange4): Ineer= []     forRowinchmatrix:ineer.append (Row[i]) print (Row[i], end=', ') Print ('---', Row) print (matrix_2) forRowinchmatrix:print (Row)

3, traversing with enumerate
# dictionary derivation new_dict= {x +Ten: x**2+Ten  forXinchRangeTen)}print (new_dict) # The traversal of the dictionary forKvinchnew_dict.items (): print (k, v) # Returns the index, DICT returns key forI, VinchEnumerate (new_dict): print (I, v) # traverse 2 more sequences, use ZIP (), go to the shortest sequence questions= ['name','Quest','Favorite']answers= ['Lancelot',' the','Blue','ABC'] forQ, ainchZip (questions, answers): Print (Q, a) # Reverse Traverse Collection forQinchreversed (Questions): print (q)

Enumerate returns an Iterator object that contains a yield that returns the running state of the function

We can also use yield to implement a simple iterator



List operations for 04-python

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.