Python: Lists, tuples, and dictionaries

Source: Internet
Author: User

1.1. Features:

An ordered set of arbitrary objects reads variable lengths, heterogeneous, and arbitrarily nested categorical object reference arrays that are variable sequences: When assigning an object to a data structure element or variable name, Python always stores a reference to the object, not a copy of the object.
1.2. Create a list
Name_list = [' Zhang ', ' Wang ', ' Li ']
or name_list = List ([' Zhang ', ' Wang ', ' Li '])
1.3. Basic operation
    Index: name_list[1]   in the list, the first element has an index value of 0.
Append: Name_list.append (' Zhao '), append can be repeated, but not recommended.
Delete: Name_list.pop ()---Delete the last one;
        Name_list.pop (2)----deleted by index location
          Name_list.remove (' Zhang ')---delete the specified element.
Count: Name_list.count (' Zhao ')
Insertion: Name_list.insert (2, ' Zhangsan ')
    Invert: Name_list.reverse ()
    Sort: name_list.sort ()-Sort by ASC code.
         name_list.remove (' Zhang ')
Slice: Extracts a piece of data from the list. Guxio in spite of the tail. NAME_LIST[0:3]---Cut the first 3 elements, Name_list[0:4:2] every one, the last one represents the interval length.
      name_listp[-2:]---Cut the last two elements
Dir (name_list) View list available syntax, ignore ' _add_ ' and other double underline syntax
  Determine if the element is in the list: ' Zhang ' in name_list
In some applications, the pop and append of the list are used together for fast LIFO. (LIFO)
2, meta-group
  2.1 tuples are read-only lists,
=(1122334455)
 或  ages  = tuple (( 11 22 33 44 55 ))
  Note: Elements of a tuple cannot be modified, but elements of a tuple element can be modified. Eg: an element of the tuple is a dictionary, and the Elements of the dictionary can be modified.
Dir (list)
>>> ' count ', ' index '
  2.2 List conversion to tuple type  View list types. Tuple (name_list)
  tuple goto list  (list)
3. Execute script incoming parameters

Python has a large number of modules, which makes developing Python programs very concise. The class library includes three:

    • Python-supplied modules
    • Industry-Open Source modules
    • Modules developed by programmers themselves

Python internally provides a SYS module where SYS.ARGV is used to capture parameters passed in when executing a python script

123456 #!/usr/bin/env python# -*- coding: utf-8 -*-import sys printsys.argv 

4. The dictionary dictionary is the only type of mapping in the Python language. The hash value (key, key) and the object (value, values) of the mapped type object are a one-to-many relationship, which is often considered a mutable hash table.

The Dictionary object is mutable, which is a container type that can store any number of Python objects, including other container types.

Characteristics:

Read by key instead of offset

Unordered collection of arbitrary objects

Variable length, heterogeneous, arbitrary nesting

belongs to a mutable mapping type

The difference between a dictionary type and a sequence type:

1. Access to and access to data differs in different ways.
2. The sequence type only uses the key of the numeric type (indexed in numerical order from the beginning of the sequence);
3. The mapping type can be used as a key for other object types (such as numbers, strings, ganso, generally with strings as keys), and the key of the sequence type is different, the mapping type of the key is straight 4. Associate or indirectly with the stored data value.
5. The data in the mapping type is unordered. This is not the same as the sequence type, and the sequence type is numerically ordered.
6. The mapping type is directly "mapped" to a value with a key.

 To create a dictionary:
123 person  =  < Code class= "Python plain" >{ "Mr.wu" ' age ' 18 or person  =  dict "Mr.wu" 18

1, the key and the value with the colon ":" separate;
2, the term and the item with a comma "," separate;
3. The keys in the dictionary must be unique, and the values may not be unique.

Note: If the value in the dictionary is a number, it is best to use a string number form, such as: ' Age ': ' 040′ without ' age ': 040

Common operations:

D = {' Egg ': 3, ' Ham ': 1, ' spam ': 2}

    • Index d[' egg ']
    • New d[' brunch ' = ' Bacon ' dictionary key is unordered collection, new when you can not specify the location of the key.
    • Delete del d[' egg ']
      D.pop (' egg ') specifies the key to delete
    • Cycle
    • Length Len (D)
    • Display key, value, key value
      >>>list (D.values ()) >>>list (D.keys ()) >>>list (D.items ())
    • Get method: D.get (Key.default)
      When the key does not exist, the dictionary traversal looks for a missing-key error, but when the key does not exist, a system default of None or a user-defined default value can be obtained by getting it.
      Eg:>>>d.get (' spam ') 2
      Print (D.get (' toast ')) None
      D.get (' Toast ', 88) 88
    • Update

    • Cycle
      D = {' A ': 1, ' B ': 2, ' C ': 3}
      For I in D:
      Print (I, ' \ t ', D[i]) # ' \ t ' indicates the key and value are separated by tab.
    • Catching and repairing of abnormal key value
PS: Loops, range,continue and break

Python: Lists, tuples, and 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.