05-Python in the four data types

  Overview: The main analysis of four data types Python in this article: list, tuple, dict, set


A, list

  list is an ordered list, we can add or delete elements in the list at any time.

  list with the elements in square brackets [] enclosed, for example:

>>> coding = ['C', 'Java', 'PHP']
>>> coding
['C', 'Java', 'PHP']

len (): Get the number of elements

  We can use len () function to get the number of elements in the list:

>>> len(coding)
3

Access the elements by index

  list and C language arrays, indexes start at 0. We can access elements in the list by the index. When the index is out of range, Python will report IndexError error.

  We have two ways to get the last element:

    • only the (coding) - 1
    • only the (-1)

  So, the list may be marked by a backstepping 0, -1, -2 and so on.

append (): Additional elements

  You can append () append elements to the end of the list:

>>> coding.append('Python')
>>> coding
['C', 'Java', 'PHP', 'Python']

insert (): the element is inserted into the specified position

  Element can be inserted into the list of the specified location, the original order after the shift elements:

>>> coding.insert(1, 'C++')
>>> coding
['C', 'C++', 'Java', 'PHP', 'Python']

pop (): remove elements

  Can) function removes the element at the end of the list with a pop (:

>>> coding.pop()
'Python'
>>> coding
['C', 'C++', 'Java', 'PHP']

  Function by specifying the index, you can use pop () Removes the specified location

>>> coding.pop(1)
'C++'
>>> coding
['C', 'Java', 'PHP']

Replace an element: a direct assignment

  Element specifies the index can give a direct assignment to replace the element.

flexible list: no type defined

  elements of the list may be different data types, or even another list.

 

Two, tuple

  tuple is an ordered list, called tuples, and the list is very similar. tuple special is that: Once the initialization can not be modified . Naturally, tuple no append (), pop (), insert () method and the like.

Brings the same benefits: security

  Because immutable, it is more secure. If possible, try to use tuple instead of the list.

Define tuple giant pit

  Because immutable tuple, the tuple defining, the element must be determined.

  The definition of an empty tuple:

t = ()

  Only the definition of an element tuple (warning pit in front of God, noncombatant evacuation !!!):

= T (1) # defined in this way is not a tuple, but this number is 1 
T = (1,) # is only one element of such defined tuple

  Knock on the blackboard, draw the focus: When you define only one element tuple, be sure to add a comma.

The changes of the tuple of

  tuple is unchanged, that is the address pointed unchanged. As to whether the contents of the address in point changes, tuple expressed inability jurisdiction.

  For chestnut: tuple There are elements that list, tuple said the same, is to always point to this list, can not turn point to other list. As to whether this list of elements change, it is not tuple things considered friends.

 

Three, dict

   dict is the dictionary, similar to other languages ​​map. dict use key-value pair (key-value) memory, look fast. Note: The internal dict stored into the order and the key is independent.

  For example, a dict achieve payroll and inquiries:

>>> d = {'Albert': 11000, 'Chin': 5500, 'Chris': 12000}
>>> d['Albert']
10000

How fast is to excel

  Why dict look fast? Because dict not to find an index, but using a hashing algorithm (the Hash) is calculated according to the memory address key value directly to the corresponding memory fetch data. Similarly, when adding a new key-value pairs to the dict, also according to the key value of the calculated address. Since the value to be calculated according to the position of key, so the key must be immutable.

How to put data into dict

  Method a: Specifies the initialization;

  Method two: into by the key. For example, we give the students Albert salary increase:

>>> d['Albert'] = 15000
>>> d['Albert']
15000

  Since the key and value is one to one, so assign a new value to a key, the original value will be overwritten.

  If the key does not exist, dict error. To avoid such errors in two ways:

  • By determining whether key exists in:  'Thomas' in D 
  • Dict provided by the get () method. If the key does not exist, None, or their specified value returns:  d.get ( ' Thomas ' )  or  d.get ( ' Thomas ' , -1) 

How to delete a key

  To delete a key, you can use pop (key) methods.

list and dict what the United States?

  Compare and list, dict has the following characteristics:

  • Find and insert the fast, the key will not increase slowed;
  • It takes a lot of memory, memory and more waste.

  The opposite list:

  • Find and insert a time increases the element increases;
  • Small footprint, waste very little memory.

  So, dict is a way to use space in exchange for time.

 

Four, set

  collection is a set of key set, but not stored value. key can not be repeated, set no duplicate key.

How to create a set

  To create a set, you need to provide a list as a set of inputs:

>>> s = set([1, 2, 3])
>>> s
{1, 2, 3}

  While we pass list is ordered, but set in only these elements, there is no order.

  set will automatically filter out duplicate key:

>>> s = set([1, 2, 3, 3])
>>> s
{1, 2, 3}

How to add elements to set in

  You can  add (key)  method to add an element to the set. If you add the same element, it does not complain, but to no avail.

How do I delete elements in this set

  By  remove (key)  you can remove elements method:

>>> s.remove(2)
>>> s
{1, 3}

Filling in the gaps

  set as a collection of mathematical, you can do the intersection, union and other operations:

>>> s1 = set([1, 2, 3])
>>> s2 = set([2, 3, 4])
>>> s1 & s2
{2, 3}
>>> s1 | s2
{1, 2, 3, 4}

  and the same set of principles dict can not put a variable object. If you can not determine whether two objects are equal variable, naturally, we can not guarantee internal set "there will be no repetition of elements."

Guess you like

Origin www.cnblogs.com/murongmochen/p/11674778.html