python3-Notes-b-001-Data structure-list list[]

Source: Internet
Author: User
Tags shallow copy

#List[Ordered variable sequence]
DefLists ():
# ---List---
#List is available byAppend ()/pop ()Method,Use as a stack
#List is available byDeque ()Packaging,Use as a two-way queue
#Create
Lists = [A,"B","C"]#List
Lists =List ()#Empty list
Lists =List (1,2,3))# (To convert a tuple to a list(Note:Converting a dictionary to a list can lose data)
Lists = [IFor IInchRange10)]#Other: [(10,i) for I in range (10)]

#Statistics
Length =Len (lists)#List length
Count = Lists.count ("C")#Count the number of elements

#Get
STR0 = lists[0]#Take out the data in the list
Lists = lists[0:3]
Lists = lists[0:3:2]
Ary2 = Lists.copy ()#Shallow Copy List

#Find
Lists = [A,"B",C,' B ',A,C,' d ',' B ',A,' B ']#List
index = Lists.index ("B")#Query the location of the element,There is no throwing exception(Attention) = 1
index = Lists.index ("B",2)#Start position, default is0 =>3
index = Lists.index ("B",4,8)#End position, default is list length=>7
Elem =Min (lists)
Elem =max (lists)

# traverse
for i in lists:
Span style= "COLOR: #8888c6" >print ( "for:%s"% i)

# modify
Lists[0] = "0" # Modify the first Data
Lists.reverse () # reverse
Lists.sort () # ( natural order ) sort

#Add to
Lists.append ("D")#Add data(Add to End)
Lists.insert (1,"A1")#Inserting data
Lists.extend (["D",E, "F"]) # Add Data ( element appended to end )
Lists = [1, 2] + [3, 4] # Merge list
Lists = [1, 2] * 3 # add element to itself 3 times

#Delete
Del lists[1]#Delete based on index(Attention)
Lists.pop ()#Delete last Element
Lists.remove (1)#Delete based on element,Throw a line if you can't find it
Lists.clear ()#Clear List

#Judge
Boolean = "a" in lists # Whether the element exists in the list
boolean = " a "not in lists # If the element does not exist in the list

# sort
lists = sorted (lists) # dictionary ( unordered variable map type , key-value pair collection , and key unique )

python3-note-b-001-data structure-list list[]

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.