Tutorial list with tuples in Python

  Using Python in the list with tuples

  First, lists and tuples basis:

  Lists and tuples, can be placed is an ordered set of any data type.

  In many languages, the set of data types must match. However, for lists and tuples in Python, it does not require this:

  my_list = [66, 77, 'fe', 'cow'] # contain both a list of elements of type string and int

  print(my_list)

  # Output: [66, 77, 'fe', 'cow']

  my_tuple = ( 'fe_cow', 666) # int tuple contains an element of type string and simultaneously

  print(my_tuple)

  # Output ( 'fe_cow', 666)

  So what are the differences between them?

  List is dynamic: size of length is not fixed, may be increased freely, deletion or change elements.

  Tuple is static: the length of a fixed size, deletions or changes can not be increased.

  Here's an chestnuts:

  Create a separate lists and tuples. For a list easily add the last element in the list, but the same operations tuples will error:

  my_list = [1, 2, 3, 4]

  my_list [3] = 5 # python index is started from 0 my_list [3] indicating the ACL fourth element

  print(my_list)

  # Output: [1, 2, 3, 5]

  my_tuple = (1, 2, 3, 4)

  my_tuple[3] = 5

  print(my_tuple)

  # Error results:

  Traceback (most recent call last):

  File "", line 1, in

  TypeError: 'tuple' object does not support item assignment

  If you want to do on an existing tuple "change", it will only re-open a block of memory, create a new tuple.

  my_tuple= (1, 2, 3)

  new_tuple = my_tuple + (4, )

  print(new_tuple)

  # Output: (1, 2, 3, 4)

  Python lists and tuples support negative indexes:

  A negative index // list

  my_list = [1, 2, 3, 4]

  my_list[-1]

  4

  A negative index // tuple

  my_tuple = (1, 2, 3, 4)

  my_tuple[-2]

  3

  -1 is the last element, -2 is the penultimate element, and so on.

  Python lists and tuples support slicing:

  // list slicing

  my_list = [1, 2, 3, 4]

  my_list[1:3]

  [2, 3]

  // tuple slicing operations

  my_tuple = (1, 2 ,3 ,4)

  my_tuple[1:3]

  (2, 3)

  Index from 1 to 2, simple to understand: "before and after the opening and closing"

  Python may function by converting each list () and tuple ():

  // convert list of tuples

  list((1, 2, 3))

  [1, 2, 3]

  // convert list of tuples

  tuple([1, 2, 3])

  (1, 2, 3)

  Common built-in functions Python lists and tuples:

  // list of operations

  my_list = [3, 2, 3, 7, 8, 1]

  my_list.count(3)

  2

  my_list.index(7)

  3

  my_list.reverse()

  my_list

  [1, 8, 7, 3, 2, 3]

  my_list.sort()

  my_list

  [1, 2, 3, 3, 7, 8]

  // tuple operation

  my_tuple = (3, 2, 3, 7, 8, 1)

  my_tuple.count(3)

  2

  my_tuple.index(7)

  3

  list(reversed(my_tuple))

  [1, 8, 7, 3, 2, 3]

  sorted(my_tuple)

  [1, 2, 3, 3, 7, 8]

  count (item): indicates how many times the item appears / tuples hit list.

  index (item): Returns a list of the index represents the first occurrence of the item / tuple.

  list.reverse () and list.sort () denote the reverse situ list and sorting.

  Note that there is no built-tuples these two functions, need to be converted into a tuple list during operation of this function

  the reversed () and sorted () represents the same list / tuple version and sorting, but will return or reverse after a new sorted list / tuples.

  Second, lists and tuples storage differences:

  // use __sizeof __ () View storage

  my_list = [1,2 ,3 ,4]

  my_list.__sizeof__()

  72

  my_tuple = (1, 2, 3, 4)

  my_tuple.__sizeof__()

  56

  It can be seen, the same elements are placed, then stored in the tuple space for a list of less than 16 bytes. Why? The reason is as follows:

  List is dynamic, it needs to store a pointer, to point to the corresponding elements (int type, 8 bytes).

  Variable list, so the need for additional storage has allocated length size (8 bytes), so that it can be tracked in real time usage of the list of space, when there is insufficient space, the timely allocation of additional space.

  The following list describes the spatial allocation process: Zhengzhou crowd Price http://www.zzzykdfk.com/

  my_list = []

  my_list .__ sizeof __ () // empty list of storage space is 40 bytes

  40

  my_list.append(1)

  my_list.__sizeof__()

  After the addition of the element 1 // 72, listing the assigned space can store four elements (72--40) / 8 = 4

  # Record later added four elements

  my_list.append(2)

  my_list.__sizeof__()

  72 // Since before the allocated space, so adding elements 2, list spatially invariant

  my_list.append(3)

  my_list.__sizeof__()

  72 // added element 3, a list of space-invariant

  my_list.append(4)

  my_list.__sizeof__()

  72 // added element 4, the list of space-invariant

  my_list.append(5)

  my_list.__sizeof__()

  After adding 104 // element 5, a list of the lack of space, so they can be allocated to additional storage space of four elements

  In order to reduce the list of each of the add / delete operations overhead space allocation, space allocation when each Python will allocate some extra;

  Such mechanism ensures high efficiency of its operation: addition / deletion of the time complexity of 0 (1);

  For tuples, the fixed size of the head element, the element can not be changed, the storage space is fixed.

  Third, lists and tuples of performance:

  From the difference between the two storage described above, it can be concluded, the list of tuples is more lightweight than some of the tuple speed performance slightly better than the list.

  Python in the background, doing some resources to cache static data. Because there is garbage collection, if some variables are not used, Python will reclaim the memory occupied by them, returned to the operating system so that other variables or other applications.

  For some static variables, such as tuples, it is not being used and less space, Python will temporarily cache this memory. The next time we re-create the same size tuple, Python would not again requesting the operating system can directly look for memory, but can be directly assigned cache memory space before, so that we can greatly accelerate the speed of the program.

  Fourth, lists and tuples usage scenarios:

  If the amount of data and storage of the same, only the data needs to be rendered directly to the front, then it is surely more appropriate choice of tuples.

  // such as latitude and longitude return to the front

  def get_location():

  .....

  return (longitude, latitude)

  Or if the data storage amount is variable, then it is more appropriate with the list.

  // each additional information such as the return of teachers to the front

  teacher_list = [] // is stored inside

  result = DB (teacherID) // teachers to come up with new information from the database within a week, returns the QuerySet

  for item in result:

  teacher_list.append(item)

  V. Summary:

  Lists and tuples are ordered collection can store any type of data, the main difference is:

  List is dynamic, variable length may be increased freely, the change or deletion of elements.

  Storage space slightly larger than the list of tuples, the performance slightly inferior to the tuple;

  Tuple is static, the length of the fixed size of the elements can not add, delete or change operation.

  With respect to the list of tuples is more lightweight, slightly better performance;

  Six, list (), what is the difference with [] efficient?

  The difference is that:

  list () is a function call, Python function call creates a heap, and operates a range of parameters examined.

  [] Is a function of a built-in C, can be called directly, and therefore higher efficiency.


Guess you like

Origin blog.51cto.com/14335413/2456291