Summary of common operations for lists, strings, and dictionaries in Python

Source: Internet
Author: User
The following small series for everyone to bring a brief discussion of Python in the list, string, Dictionary of common operations. Small series feel very good, now share to everyone, also for everyone to make a reference. Let's take a look at it with a little knitting.

The list operation is this:

A = ["haha", "Xixi", "Baba"]

Add: A.append[gg]

A.INSERT[1,GG] in place labeled 1, added GG

Delete: a.remove (haha) Remove from left to right in list, first match to Haha

Del A.[0] Delete the value corresponding to the subscript 0
A.pop (0) No content is written in parentheses, the default is to delete the last one, write, delete the corresponding subscript content

Change: A. [0] = "GG"

Check: a[0]

A.index ("haha") displays the haha subscript from left to right, first match to
A.count ("haha") shows the number of haha in the list
A.clear () clear List A

Quickly iterate through the contents of the list and go to the subscript to print together.

Enumerate (a) is to take the list of each subscript, subscript content, put in an array, so you can use for loop to traverse.

A = ["haha", "Xixi", "Baba"]
For Index,data in Enumerate (a):p rint (Index, ":", data)

Results:

0:haha
1:xixi
2:baba

Key notes:

A.copy () shallow copy, e.g. a = ["haha", "Xixi", ["Yan", "Liu"], "Baba"]
b = A.copy ()

Modify the contents of a outside, B will not change with!

Modify the list contents of a ["Yan", "Liu"],b will change ~ ~

Reason: In fact a list inside the list ["Yan", "Liu"] is in memory alone, a just write this memory pointer to this, ["Yan", "Liu"] it is a separate.

Simple use: Create a common account, that is, the outer layer is independent and the inner list is shared.

Import Copy
b = Copy.deepcopy (a) deep, fully copy,b completely independent. but use less. Because it will open up a separate memory space. If a list is large, it consumes memory.

String manipulation:

Name = "Name is {name}, age is {"} "
Print (Name.capitalize ()) #首字母大写
Print (Name.center, "-") #左右加25个 "-"
Print (Name.endswith ("an")) #判断是不是以 end of "an"
Print (Name.find ("a")) #从左往右开始找到的第一个 subscript for "a"
Print (Name.format (name= "Yan", age= ")) #将字符串 the contents of {}

Dictionary operation:

How the dictionary gets the value:

A = {"Yan": 123, "Liu": 456}
Print (a["Yan"]) #方法1, if key does not exist, an error will be
Print (A.get ("Yanada")) #方法2, if Ket does not exist, returns none

A.keys () #获取key
A.values () #获取value

Serdefault usage:

A.setdefault ("Yan", 789)
Print (a)
{' Liu ': 456, ' Yan ': 123}
A.setdefault ("Wang", 789)
Print (a)
{' Yan ': 123, ' Liu ': 456, ' Wang ': 789}

First go to the dictionary to find this key value, found, return its corresponding value did not find the proof is not, then add a key value, and assign value to the new dictionary content can be used to avoid the key value of the same, the new is not successful, Instead, the value corresponding to the original key should be dropped.

Update usage:

A = {"Yan": 123, "Liu": 456}
b = {"Yan": 666, "haha": 888}
A.update (b)
Print (a)
{' Yan ': 666, ' haha ': 888, ' Liu ': 456}

b as a parameter, passed to the update function, and a merge, if the key value is the same, the B will prevail, a to be updated

Items Usage:

Changes the dictionary to a list, where the contents of the list---a tuple of key and value, and key is labeled 0,value subscript 1

Related Article

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.