python--Data structure

Source: Internet
Author: User

List method

Append (Element)

LI1 = ["hello""fftu"];li1.append ("  verb");p rint (LI1);    #['Hello'fftu'  verb']

LI1 = ["Hello","FFTU", About];li2= ["adv","pron","adj"];li1.append (li2);p rint (LI1); #['Hello','FFTU', About, ['adv','pron','adj']]

You can see that no matter what data type is added, the append () method populates it as the last element of the list.

Extend (Element)

LI1 = ["Hello","FFTU", About];li2= ["adv","pron","adj"];# li1.extend (li2); Li1+=Li2;print (LI1); ['Hello','FFTU', About,'adv','pron','adj']

The middle two sentences are equivalent.

Insert (I, Element)

I is the index that the element will occupy

LI1 = ["Hello","FFTU", About];li2= ["adv","pron","adj"];li1.insert (1, Li2);p rint (LI1); #['Hello', ['adv','pron','adj'],'FFTU', About]

Remove (Element)

LI1 = ["Hello","FFTU", About];li2= ["adv","pron","adj"];li1.remove ("Hello");p rint (LI1); #['FFTU', About]

Pop (i)

 li1 = [ hello   ", "  FFTU  , 99  ];li2  = [ " adv  " ,  pron  " ,  adj  "   1  );p rint (LI1); #[  " hello  , 

If you do not pass the parameter I, the last element is deleted by default.

This function has a return value, and the returned value is the deleted element

Clear ()

Remove all the elements from the list and become empty lists

Index (Element)

Li1 = ["Hello""fftu"= ["  adv""pron""adj"];p rint ( Li1.index ());     #2

COUNT (Element)

LI1 = ["Hello","FFTU", About,"FFTU"];li2= ["adv","pron","adj"];p rint (Li1.count ("FFTU")); #2

Use a list as a stack

Stack as a specific data structure with the attribute "last in, first out"

Append () presses the element onto the top of the stack

Pop () is removed from the top of the stack (delete element)

Use the list as a queue

Seems to have introduced a queue, not

List-derived

Typically, an application applies some actions to each element of a sequence, using the resulting results as an element to generate a new list, or by creating a subsequence based on a determined criterion.

Example: List element * Create a new list

LI1 = [15= [(3 for in Li1];p rint (result);      #[3]

Modify the above code

LI1 = [153 for in Li1];p rint (result);      #[[13], [5], [ten]]

Format a list element with a function to generate a new list

LI1 = [1,5,Ten];d ef addwords (Element):return "the number before the modification is:"+str (element); result= [[Addwords (Element),3* element] forElementinchLi1];p rint (result); #[['the number before the modification is: 1',3], ['the number before the modification is: 5', the], ['the number before the change is: Ten', -]]

If as filter

I think the filter should be written in the outer layer of the derivation.

involves two lists

 li1 = [1 , 5 , 10  ];li2  = [2 , 6 , 9  ];result  = [(Li1element * li2element) for  li1element for  li2element  Li2];p rint (result); #[ 2 , 6 , 9 , 10 , < span>, 45 , 20 , 60 , 90 ] 

List conversions

In fact, is the rearrangement of the matrix, used to look again

Dictionary

Dict1 = {"name":"Jiao"," Age": -};d EF insertwords (value):if(Isinstance (Value,int)):        return(" now is"+str (value)); if(isinstance (value, str)):return("Hello"+value); result= {str (key): Insertwords (Dict1[key]) forKeyinchDict1};p rint (result); #{'name':'Hello Jiao',' Age':' now is'}

Traversal tips

When traversing in a dictionary, the keywords and corresponding values can be read at the same time using the items () method.

Dict1 = {"name""jiao" "Age"  ;  for inch Dict1.items ():     if int ) ):        + str (value));     Else :         + value); #namejiao #age25

When traversing in a sequence, the index position and the corresponding value can be obtained simultaneously using the enumerate () function

LI1 = ["helo""fftu"];  for inch Enumerate (LI1):     " : " + str (value)")    ; 0  12:

~end

python--Data Structure

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.