List, String, Dictionary common operations in python, python string

Source: Internet
Author: User

List, String, Dictionary common operations in python, python string

The list operation is as follows:

A = ["haha", "xixi", "baba"]
Increment: a. append [gg]
A. insert [1, gg] To the place marked as 1, add gg
Delete: a. remove (haha) deletes the first matched haha from left to right in the list.
Del a. [0] Delete the value corresponding to 0 as the subscript
A. NO content is written in the pop (0) brackets. The last one is deleted by default. If it is written, the corresponding content of the lower mark is deleted.
Change: a. [0] = "gg"
Query: a [0]
A. index ("haha") displays the first matched haha subscript from left to right.
A. count ("haha") shows the total number of haha instances in the list.
A. clear () clear List
Quickly traverse the contents of the list and print them together with the subscript.
Enumerate (a) extracts the content of each subscript and subscript in the list and puts it in an array. Therefore, you can use the for loop to traverse the list.
A = ["haha", "xixi", "baba"]
For index, data in enumerate (a): print (index, ":", data)
Result:
0: haha
1: xixi
2: baba
Important Notes:
A. copy () shallow copy, such as a = ["haha", "xixi", ["yan", "liu"], "baba"]
B = a. copy ()
Modify the content outside a. B will not change with it!
Modify the list content in a ["yan", "liu ~~
Cause: in fact, the list ["yan", "liu"] in list a exists independently in the memory. a only writes this memory pointer to it. ["yan ", "liu"] It is independent.
Simple purpose: create a common account, that is, the outer layer is independent and the inner layer list is shared.
Import copy
B = copy. deepcopy (a) deep, full copy, B completely independent. But it is rarely used. Because it will open up an independent memory space. If List a is large, memory consumption will be high.

 

String operation:

Name = "name is {name}, age is {age }"
Print (name. capitalize () # uppercase letters
Print (name. center (50, "-") # Add 25 "-" to the left and right sides.
Print (name. endswith ("an") # judge whether it ends with ""
Print (name. find ("a") # subscript of the first "a" found from left to right
Print (name. format (name = "yan", age = "24") # convert the content in the string {}

 

Dictionary operation:

How to obtain the value from a dictionary:
A = {"yan": 123, "liu": 456}
Print (a ["yan"]) # method 1. If the key does not exist, an error is returned.
Print (a. get ("yanada") # method 2. If the ket does not exist, None is returned.

A. keys () # Get key
A. values () # obtain value

* ** Serdefault usage:
A. setdefault ("yan", 789)
Print ()
{'Liu ': 456, 'any': 123}
A. setdefault ("wang", 789)
Print ()
{'Any': 123, 'Liu ': 456, 'wang': 789}
Find the key value in the dictionary. If it is found, the corresponding value is returned. If it is not found, the key value is added, assign a value to the value so that when the dictionary content is added, the value corresponding to the original key is dropped instead of the same key value.

* ** Update usage:
A = {"yan": 123, "liu": 456}
B = {"yan": 666, "haha": 888}
A. update (B)
Print ()
{'Any': 666, 'hahaha': 888, 'liu': 456}
Pass B as a parameter to the update function and merge it with a. If the key value is the same, take B as the standard. If a is to be updated

Items usage:
The dictionary is changed to a list. The list content --- key and value form a tuples. The key subscript is 0, and the value subscript is 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.