Python list tuple dict

Source: Internet
Author: User
Tags shallow copy python list

Very unexpectedly, my blog actually someone visited, there is a man to evaluate, praise, inexplicable gratitude 650) this.width=650; "src=" Http://img.baidu.com/hi/jx2/j_0063.gif "alt=" j_0063. GIF "/>

A list

name = ["AAA", "BBB", "CCC", "ddd", "Eee", "fff", "GGG", "HHH", "III"]

Print (name)

[' AAA ', ' BBB ', ' CCC ', ' ddd ', ' eee ', ' fff ', ' ggg ', ' hhh ', ' III ']

Insert Name.insert (4, "AAA") # 4 to insert before the first few elements

Results [' AAA ', ' BBB ', ' CCC ', ' ddd ', ' aaa ', ' eee ', ' fff ', ' ggg ', ' hhh ', ' III ']

Modify Name[0] = "Aaa.1"

Results [' aaa.1 ', ' BBB ', ' CCC ', ' ddd ', ' eee ', ' fff ', ' ggg ', ' hhh ', ' III ']

Name.append appending elements to the end of the list

Name.pop (i) delete the element used at the specified position, where I is the index subscript

Name.remove () Deletes the first occurrence of a value

Name.count () Number of elements appearing in the statistics list

Name.index () lists the index of the element print (Name.index ("Eee")) returned 4

Name.extend (LIST6) append LIST6 to name

Name.copy copy () is a shallow copy that copies only the parent object and does not copy the child objects in the parent object; The deepcopy is a deep copy, which can be considered a complete copy of the past.

Del Name[1] #删除指定下标的元素

Name.sort () #排序
Name.reverse () #倒序

Del Name[1:3] #删除指定下标范围的元素

Print (Name[2:7:2]) prints 2 to 7 step 2

Replace all occurrences of 9 with 99999999

List = [1,2,3,4,5,6,7,8,9,10,10,9,8,7,6,5,4,3,2,1,2,4,7,9,3,4,6,7,9]for i in Range (List.count (9)): List[list.index (9 )] = "99999999"

Two Dict

Dict1 = {' Zhangsan ': 30000, ' Lisi ': 321000, ' Wanger ': 123654, ' Wangwu ': 123878}

Note: The value of key must be unique

' Zhangsan ' in Dict1 to determine if key is in the dictionary

Dict1.get () method, returns None if the key value does not exist or the return value specified by itself

Dict1.keys (): Returns a dictionary of all keys in a list

Dict1.values (): Returns all values in the dictionary as a list

Dict1.items (): Returns an array of traversed (key, value) tuples in a list

Dict.update (DICT2): Update the dictionary dict2 key/value pairs to Dict

Dict.clear (): Delete all elements in the dictionary

Dict.copy (): Returns a shallow copy of a dictionary

Dict.setdefault (Key, Default=none): Similar to get (), but if the key does not already exist in the dictionary, the key will be added and the value will be set to default

Compared with list, Dict has the following features:

    1. The speed of finding and inserting is very fast and will not increase with the increase of key;

    2. It takes a lot of memory, and it wastes a lot of memory.

And the list is the opposite:

    1. The time to find and insert increases as the element increases;

    2. Small footprint and little wasted memory.

Ternary group

When creating an empty tuple, you can create a bracket directly, and when you create a tuple that has only one element, you must add a comma (,) to the polygon:

Tuple1 = (' Wudashen ', ' Langdacheng ', ' Wangchengcheng ')

Tuple2 = (1,2,3,4,5,6,7,8,9,0)

Tuples can use subscript indexes to access values in tuples

>>> print "tuple1[0]:", tuple1[0]

Tuple1[0]: Wudashen

Calculate the length of a tuple: Len (tuple1):

Returns the maximum value in the tuple: Max (tuple2), Min (tuple2)

Comparison of two tuple elements: CMP (TUPLE1,TUPLE2)

Calculate the length of a tuple: Len (tuple1):

Convert list to Narimoto Group: lt = Tuple (List1)

To convert a tuple to a list (LS)




Python list tuple dict

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.