A summary of the similarities and differences between sequences and dictionaries in Python.

Source: Internet
Author: User

A summary of the similarities and differences between sequences and dictionaries in Python.

Commonalities:
1. They are all core types of python and are part of the python language itself.

Core Type and non-core type
Most core types can generate their objects through specific syntax. For example, "dave" is the expression for creating string-type objects;
Non-core types must be created by built-in functions. For example, the file type must be created by calling the built-in function open.
Class can also be understood as a custom non-core type.

2. The boundary check does not allow an index to be exceeded.

>>> a = 'dave'>>> a[3]'e'>>> a[4]
Traceback (most recent call last): File "<stdin>", line 1, in <module>IndexError: string index out of range

3. []

>>> a = 'dave'>>> print a[0]d>>> a = ['d','a','v','e']>>> print a[0]d>>> a = ('d','a','v','e')>>> print a[0]d

4. Most of them support iterative protocols


Differences:

1. orderliness
String, list, And tuples are sequences. tuples can be considered as immutable lists.
The dictionary is the only ing type in python.
The sequence is ordered, and the dictionary has no order.


2. Variability
List, Dictionary variability.
The string, tuples, and numbers are non-mutable, that is, the content of the corresponding bucket is unchangeable after copying, unless the object is destroyed.
For example:

>>> s = 'dave'>>> id(s)
140105068006304
>>> s = 'python' + s[1:]>>> s
'pythonave'
>>> id(s)
140105067997536

S in the first line points to memory space 140105068006304, storing the string 'Dave'
The second line of s is changed to 'pythonave ', but it just points to a memory address that stores the 'pythonave' string. in the past, 140105068006304 of the memory space may still be 'Dave ', or it may be cleared and destroyed by the system.

3. Sequential operations
Supported sequences (strings, lists, and tuples) and ing (dictionary)
Note: The sequence operation generation type remains unchanged.

>>> seq = 'dave'>>> print seq[1:]
ave
>>> seq = ['d','a','v','e']>>> print seq[1:]
['a', 'v', 'e']
>>> seq = ('d','a','v','e')>>> print seq[1:]
('a', 'v', 'e')

4. List parsing expression

Sequence (string, list, And tuples) and ing (dictionary) are supported in expressions)
Different from sequential operations, a list parsing expression can only generate one list.

>>> str = [ a * 2 for a in 'dave' ]>>> print str
['dd', 'aa', 'vv', 'ee']
>>> L1 = [1,2,3]>>> L2 = [4,5,6]>>> L3 = [(a+b) for (a,b) in zip(L1,L2)]>>> print L3
[5, 7, 9]
>>> tup = (1,2,3)>>> tups = [ a * 2 for a in tup]>>> print tups
[2, 4, 6]
>>> dic = {'a':'dave','b':'emily'}>>> dics = [ i *2 for i in dic.items()]>>> print dics
[('a', 'dave', 'a', 'dave'), ('b', 'emily', 'b', 'emily')]

The items (), keys (), and values () methods in the dictionary are all returned lists. Here, for I in a is written for I ina. keys (), which has the same effect.

>>> dic = {'a':'dave','b':'emily'}>>> dicts = [ i * 2 for i in dic]>>> print dicts
['aa', 'bb']

5. nesting

In addition to strings, lists, tuples, and dictionaries can be nested in multiple layers.
Nesting does not conflict with variability.
In tup = (4, 5, a), the input B is a variable pointer or a memory address, which is a number.

>>> a = [1,2,3]>>> tup = (4,5,a)>>> print tup[2]
[1, 2, 3]
>>> a[0] = 'x'>>> print tup[2]
['x', 2, 3]

A points to a list, and the list is changeable. Therefore, after B [0] = 'X' operation, you can see the change in the index of the tuples.

>>> a = '123'>>> tup = (4,5,a)>>> print tup[2]
123
>>> a = 'xxx'>>> print tup[2]
123

A points to a string that is non-mutable. Therefore, after a = 'xxx' operation, the pointer a changes itself and points to another memory space, the memory address in the tup tuples does not change to the space indicated by the memory address, or the string '123'


In the above two paragraphs, tup is always immutable, and changes are also the changes in the memory pointed to by its elements as pointers.

Articles you may be interested in:
  • Sequence Method in python
  • Python obtains the smallest elements in a sequence.
  • Sequence of basic python tutorials
  • Example of how Python merges and sorts two ordered lists
  • Comparison of Two Methods of traversing Dictionary (dict) in python
  • Python converts a string into a dictionary dict
  • Dictionary details in python

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.