Python list, use of dictionaries

Source: Internet
Author: User
Tags python list

First, if you want to use the Python module, you can use the Import module name directly, if you want to use a third-party module, please download the installation (PIP installation) yourself.

Second, list:

Define a list of empty lists: List = []

<1>Add an element to the list aa:list.append (' AA ') = = "Print (list) ==>[' a ']

<2>Delete Just the element: List.remove (' AA '); A similar method is clear (), except that clear () is clear () all elements in the list.

<3>, method Copy, also known as light Copy, copy is the original object corresponding to the memory space, and the module copy method deepcopy is deep copy, copy is a real object, through the following example can be explained:

List = [1,2,[3,4]]

List1 = List.copy ()

List[2].append (5) ==> final output list and list1, found the elements are the same.

If you import copy this module and then use List1 = copy.deepcopy (list) for copy, you will find that no matter how the elements in the list change, the elements in the List1 will not change, which is the difference.

<4>, another method is count, it can calculate how many of an element in the list, and with index can draw the same elements are in which subscript, see the following example:

list = [' AA ', ' BB ', ' a ', ' AA ']

For I in range (List.count (' AA ')): #list. Count (' AA '), which will draw a list of how many elements AA

index = list.index (' AA ') #list. Index (' AA '), which results in the first subscript of an element AA.

Print (' Subscript: ' Index)

#通过循环可以得出所有的元素aa所在的下标.

<5>, method insert:

The purpose of this method is to add an element to the front of an element, for example, if you want to add a new element to the front of the third element, just write it: List.insert (2, ' new ')

<6>, method sort:

Sorting of lists that contain both strings and numbers is not supported in Python3, and is supported in Python2.

Three, Dictionary:

The data in the dictionary is the key value pair: Key:value

<1>,keys:

This method can be used to determine whether a key is in a dictionary, or to iterate through a key through a loop.

<2>,values:

Take out a dictionary, and all values coexist in a list.

<3>, get:

This method uses more, mainly is according to the user input key, to take out its corresponding value, the simplest example is the user information stored in the dictionary, and then through the user input key, take out the user's corresponding information, please see the following example:

Dict = {

' Name ': ' da ',

' Age ': 25,

' Sex ': Nan

}

If the user wants to remove the age value based on key, it can be:

Print (Dict.get (' age ')) = 25

<4>setdefault:

This method I use less, that is, when the key exists to change value, if it does not exist, add it into; when I write a script, if I want to change a key's worth of flowers, I will change it with dic[key] = value.

<5>, and others want clear, etc., are similar to the use of the list, here do not elaborate.

Python list, use of dictionaries

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.