As a qualified programmer, not only must master those complicated, dazzling, obscure algorithms and programming skills, for data construction, operation, transfer, storage and other operations are essential skills.

This series describes the features and operations used by Python to manipulate lists, tuples, dictionaries, array-numpy, DataFrame-pandas, and sets.

Today, I start the first article in the Python Data series (PART I) – List: Python’s coolies.

Lists have the following characteristics:

  • A set of ordered items, variable data type
  • A list is a data set surrounded by square brackets “[]”, with members separated by “, “.
  • The list can contain any data type, or it can contain another list, and it does not need to have the same type
  • The members of a list can be accessed by sequence number

To create a list, simply enclose the different data items separated by commas in square brackets. As follows:

> > > list1 = [1, 2, 3, 4, 5] > > > list1 [1, 2, 3, 4, 5] > > > list2 = [12, "KKK," [" 12 ", "bb"]] > > > list2 [12, 'KKK, [' 12', 'bb']] > > >Copy the code

The basic list operator

> > > list1 = [1, 2, 3, 4, 5] > > > list1 [1] # 1 take sequence for the element 2 > > > list1 [1] take the last element of a sequence of # 5 > > > len (list1) # list the length of 5 > > > ,5,6,7,8 list2 = [4] > > > list1 + list2 list # combination [1, 2, 3, 4, 5, 4, 5, 6, 7, 8] >>> list3=["hello world"] >>> list3*3 # Repeat list ['hello world', 'hello world', False >>> 7 in list2 True >>> for e in list1:# print all elements in list1... print(e) ... 1 2 3 4 5 >>> Max (list1) # 1 >>> min(list1) # 1 >>> list1. Index (2) # 1 >>> list1. 3 >>> list4=[1,2,3,5,6,1,23,5,6] >>> set(list4) # Go to the list set, {1, 2, 3, 5, 6, 23} >>> len(set(list4)) # Set number of elements 6 >>> list1. Append (15) # List increment 15 >>> list1 [1, 2, 3, 4, 5, 15] >>> list4.count(1) # Count the number of elements in the list 2 >>> list4.extend(list1) # Append two sequences at the end of the list >>> list4 [1, 2, 3, 5, 6, 1, 23, 5, 6, 1, 2, 3, 4, 5, 15] > > > # list4 + list1 the extend and list composite contrast [1, 2, 3, 5, 6, 1, 23, 5, 6, 1, 2, 3, 4, 5, 15, 1, 2, 3, 4, 5, 15] >>> list4.pop(1) # Remove and return the elements of the sequence. 15] >>> list4.remove(5) # Remove the first match of an element in the list >>> list4 [1, 3, 6, 1, 23, 5, 6, 1, 2, 3, 4, 5, 15] > > > list4. Reverse () # list take the > > > list4 [15, 5, 4, 3, 2, 1, 6, 5, 23, 1, 6, 3, 1] >>> list4. Sort () # List from small to large >>> list4[1, 1, 1, 2, 3, 3, 4, 5, 5, 6, 6, 15, 23] >>> list4[3:6] [2, 3, 3] >>> list4[3:] >>> list4[:3] >>> list4[:3] 1] >>> list4[:] # Select all elements in the list [1, 1, 1, 2, 3, 3, 4, 5, 5, 6, 6, 15, 23] >>> list4[3:10:2] # Select all elements in the list, the third parameter is the step, Left closed right away [2, 3, 5, 6] > > > list4] [: : - 1 # reverse output [23, 15, 6, 6, 5, 5, 4, 3, 3, 2, 1, 1, 1] > > > list4 [set] =,22,22 [22] # replace list specified element > > > list4 [1, 1, 1, 22, 22, 22, 4, 5, 5, 6, 6, 15, 23] > > > list5 = [12, "KKK," [" 12 ", "bb"]] # list elements within the various forms of > > > list5 [2] [' 12 ', 'bb'] > > > list5 [2] [1] 'bb' > > >Copy the code

The above basic operations on lists contain, in general, the most common types of operations used in daily learning. Hope the above arrangement can be helpful to your next study. For more information about Python operations, you can enter the official account and click “History Information” to see more exciting articles.

In the next article in this series, we’ll look at the Dictionary, a form of data that has more power than a list.

Review past

  1. Basics of Knowledge Mapping (I) – What is knowledge Mapping
  2. Knowledge Graph Foundation (2) – Knowledge representation system of knowledge graph
  3. Knowledge Graph Foundation (iii) – Schema construction
  4. Python: Say goodbye to Print? Excellent Debug artifact – Pysnooper
  5. Now, be about to batch operation of file, move knife!!