This is the first day of my participation in the Challenge

Author: Cola

Source: Coke’s path to data analysis

Please contact authorization for reprinting (wechat ID: data_COLA)

Writing in the front

This section is about data structures in Python basics, for those who haven’t seen the previous section:

Pandas learning Notes (2) : Variables

Data structures are covered in four sections, starting with the first structure, lists.

A list is a data structure enclosed in square brackets []. Elements and elements are separated by commas. The elements in the list can be added, deleted, checked, modified and a series of operations.

List name = [Element 1, element 2, Element 3]

We learn lists from five dimensions: create, add, delete, search and modify.

1, build

The data in the list is not restricted by type, can be numeric or character, and can be mixed. Create a new list, i_list. The data in the list is numeric.

In [1] : # to create a new list i_list =,5,8 [2] i_list Out [1] :,5,8 [2]Copy the code

You can use the type function to view the data type of a list. You can see that the i_list list is of type list

In [2]:type(i_list)
Out[2]:list
Copy the code

Create a character list s_list that stores characters

In [3]:s_list = ["a","b","c"]
       s_list
Out[3]:["a","b","c"]
Copy the code

Create a new mash-up list, h_list, that has both numeric and character values

In [4]:h_list = [1,"a",2,"b"]
       h_list
Out[4]:[1,"a",2,"b"]

Copy the code

2,

Increment means to add elements to a list, or to add a list to a list.

2.1 Add elements to the list

The append method is used to insert a new element at the end, such as appending the value 10 to the end of the i_list list.

In [5] : # appended i_list. Append (10) i_list Out [5] :,5,8,10 [2]Copy the code

Insert (1,”s”); insert(position, value); insert(position, value);

In [6] : # specified insert s_list. Insert (1, "s") s_list Out [6] : [" a ", "s", "b", "c"]Copy the code

2.2 Adding a list to a List

The “+” operator can be used to merge lists such as i_list and s_list, which are purely joined together.

In [7] : i_s = i_list + s_list i_s Out [7], [2,5,8,10, "a", "s", "b", "c"]Copy the code

You can also use the extend method, A.xtend (b), which combines lists of B into lists of A. The effect is the same, except that i_list is now a different type of list rather than a purely numeric one.

In [8] : i_list. The extend (s_list) i_list Out [8] : [2,5,8,10, "a", "s", "b", "c"]Copy the code

3, delete

An element in a list can be deleted by value or by index. The remove method specifies the value of the element to be removed. Only one value can be deleted at a time. For example, the “s” character in the s_list can be deleted.

In [9]:s_list.remove("s")
       s_list
Out[9]:["a","b","c","d"]
Copy the code

The pop method deletes by index, specifying the location of the character to be deleted, such as the value 10 in the i_list, which is known to be the last digit in the list

In [10] : i_list. Pop i_list Out [10] : (1),5,8 [2]Copy the code

The del function, permanently removed, has the same effect, except that one is a Python function, and one is an object-oriented method.

In [11]:del i_list[-1]
        i_list
Out[11]:[2,5,8]
Copy the code

4,

Lookup means to find out if an element is in the list and access it by index.

4.1 Finding whether a value is in the list

You can use the IN operator to find if a value is in a list and return the logical value True/False. This is the same way you would find a string written in a previous datatype, such as if 5 is in the i_list.

In [12]:5 in i_list
Out[12]:True
Copy the code

4.2 Finding the value of the specified position in the list

Finds the value at the specified position in the list, that is, accesses the element by index. The list is ordered, each element has its own specific index, and you can use indexes and slicing to find a value somewhere in the list. If a is 0, b is 1, and C is 2, then a is 0, b is 1, c is 2.

In [13] : s_list Out [13] : [" a ", "b", "c", "d"] In [14] : s_list [2] # for 3rd value Out [14] : 'c'Copy the code

Take the value of the third and subsequent position, slice,: means all values, 2: means starting from the third place, including all values after the third place.

Out[15]:['c','d'] In [15]:s_list[2:]Copy the code

5, change

To change the value of an element in the list, simply find the location and assign the value, as simple as that. For the first value in the s_list list, change to uppercase A.

In [16]:s_list[0] = "A"
        s_list
Out[16]:["A","b","c","d"]
Copy the code

Optionally, operations on lists include sorting, copying, counting, and so on, as you can see.

6, sorting,

Sort method, default ascending, descending requires reverse = True parameter.

In [17] : # ascending s_list sort () s_list Out [17] : [" A ", "b", "c", "d"] In [18] : # descending s_list. Sort s_list (reverse = True) Out[18]:["d","c","b","A"]Copy the code

7, copy,

The copy method copies a list. If you copy s_list, you get a copy of it and can modify it.

In [19]:s_list.copy()
Out[19]:["d","c","b","A"]
Copy the code

8. Find the list length

Use len to find the length of a list, such as s_list, and you get 4 with 4 elements.

In [20]:len(s_list)
Out[20]:4
Copy the code

9, counting

Count is used to count the number of occurrences of elements in a list. For example, count is used to count the number of occurrences of element C in t_list. The result is 1, indicating the occurrence of one element.

In [21]:s_lisr.count("c")
Out[21]:1
Copy the code

conclusion

New list:

  • [1, 2, 3, 4]
  • [“a”,”b”,”c”,”d”]

Add elements: append(end)/insert(specified position)

Add list: extend

Remove elements: remove(by value)/pop(by position)

Find elements:

  • in
  • Index + slice

Modify elements: find and assign values

Sorting: sort

Copy, copy

Length: len

Count: the count