Python List and Dict methods

Source: Internet
Author: User
Tags python list

# # #list类的方法


# # # #append Last add an element in the list
A = [1,2,3,4,5,6, "DSSDSD"]
A.append (5)
Print (a)

# # #clear Empty the elements in the list
Example:
A = [1,2,3,4,5,6, "DSSDSD"]
A.clear ()
Print (a)

# # #copy Copy List
Example:
A = [1,2,3,4,5,6, "DSSDSD"]
V1 = a.copy ()
Print (v1)

# # Number of specified elements in #count list
Example:
A = [1,2,3,4,5,6, "DSSDSD", 2]
V1 = A.count (2)
Print (v1)

# # #extend Add elements to the list last
Example:
A = [1,2,3,4,5,6, "DSSDSD", 2]
V1 = A.extend ("4")
Print (a)

# # #index The index position of the elements within the list
Example:
A = [1,2,3,4,5,6, "DSSDSD", 2]
V1 = A.index (3)
Print (v1)

# # #index Insert index insert element into table
Example:
A = [1,2,3,4,5,6, "DSSDSD", 2]
A.insert (2, "index")
Print (a)

# # #pop Remove elements from a specified list using an index
Example:
A = [1,2,3,4,5,6, "DSSDSD", 2]
A.pop (0)
Print (a)

# # #remove Delete the specified element in the list
Example:
A = [1,2,3,4,5,6, "DSSDSD", 2]
A.remove (1)
Print (a)

# # #reverse Reverse List
Example:
A = [1,2,3,4,5,6, "DSSDSD", 2]
A.reverse ()
Print (a)

# # #sort list from small to large sort note You can only do small to large sorting of numeric lists.
Example:
A = [13,12,23,43,55,6,4545,2]
A.sort ()
Print (a)


######### #dict字典方法

# # #fromkeys to create a dictionary from a sequence and specify a uniform value
Example:
v = Dict.fromkeys (["ASD", 234,3333,2323],555)
Print (v)

# # #get Print out the value of key in the dictionary
Example:
info = {"K1": 3, "K2": 4, "K3": 5, "K4": 6, "K5": 7}
v = info.get ("K1") # # #get ("ASD", 1111) when a key ASD does not exist in the dictionary, the output of the following 1111
Print (v)

# # #pop Delete a key-value pair in the dictionary
Example:
info = {"K1": 3, "K2": 4, "K3": 5, "K4": 6, "K5": 7}
v = info.pop ("K1")
Print (INFO,V)

# # # #popitem Randomly delete a key-value pair in a dictionary
Example:
info = {"K1": 3, "K2": 4, "K3": 5, "K4": 6, "K5": 7}
v = Info.popitem ()
Print (INFO,V)

# # #setdefault Set a value that, when it exists in the dictionary, gets the value of the set key that is added to the dictionary when the setting value does not exist
Example:
info = {"K1": 3, "K2": 4, "K3": 5, "K4": 6, "K5": 7}
v = Info.popitem ()
Print (INFO,V)

# # #update Update the values in the dictionary and add to the dictionary when the key-value pair does not exist
Example:
info = {"K1": 3, "K2": 4, "K3": 5, "K4": 6, "K5": 7}
v = info.update ({"K1": +, "K7": 4000})
Print (info)

# # #keys Get the value of all keys in the dictionary
Example:
info = {"K1": 3, "K2": 4, "K3": 5, "K4": 6, "K5": 7}
v = Info.keys ()
Print (v)

# # #values Get the values of all values in the dictionary
Example:
info = {"K1": 3, "K2": 4, "K3": 5, "K4": 6, "K5": 7}
v = info.values ()
Print (v)

Python List and Dict methods

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.