An introduction to Python that even monkeys can understand (Part 2)

By the way, continuing from the last time, this article is for Python beginners. This time, as shown in the table of contents below, we will deal with the list of Python.

table of contents

  1. Create a list
  2. Extract data in list
  3. Edit / add / delete list
  4. List operating principle

1. Create a list.

Last time I learned about variables and types. Types included float for real numbers, int for integers, str for text, and boolean for True or False, but this time we'll introduce a new type list </ b>. By the way, regarding the variables mentioned last time, basically, only one data can be stored in one variable. Last time, I calculated the amount of money when importing a product, but if only one data can be assigned to one variable, when I want to put the amount of money data of four items, I would write as follows.

price1 = 2
price2 = 2.5
price3 = 13
price4 = 1.15

However, basically you want to do an analysis when you have a lot of data. Since there are four, it only takes four lines, but if there are 100 items, you will have to describe up to price 100. And instead of writing 100 lines of variables, you can use a list </ b>. Using a list, you can use square brackets ([]) and a colon (,) to write: This is the new data type list </ b>.

[2, 2.5, 13, 1.15]

And this data can also be assigned into variables. The method is simply to connect the variables with equals.

price = [2, 2.5, 13, 1.15]

You have now assigned the list to a variable called price. This list can contain any type of data, whether it's a float, an integer, a boolean, or a string. It is also possible to put different types of data in one list as shown below. For example, you can store not only "how much" information but also "which item is how much" in one list as shown below.

price = ["ItemA", 2, "ItemB", 2.5, "ItemC", 13, "ItemD", 1.15]

And that's not all. It is also possible to put the list in the list again.

price = [["ItemA", 2], ["ItemB", 2.5], ["ItemC", 13], ["ItemD", 1.15]]

In the above example, the structure is such that one list contains four sublists. By doing the above, you can create a list for each item, with the first list being the ItemA list and the next being the ItemB list, making it easier to manage your data.

Of course, you can also put variables in the list. For example, if you put the variables used to calculate the import cost last time in one list, it will be as follows.

price = 2
exchange = 110
tax = 0.13
totalprice = price*exchange*(1+tax)
price_list = [price, exchange, tax, totalprice]
print(price_list)
#If you run the above code,[2, 110, 0.13, 248.59999999999997]Is returned.

price_list2 = ["price", price, "exchange", exchange, "tax", tax, "totalprice", totalprice]
print(price_list2)
#Different types of data can be stored in the list
#If you run the above code,['price', 2, 'exchange', 110, 'tax', 0.13, 'totalprice', 248.59999999999997]Is returned

You can also calculate in the list.

list = [4/2, "d"*4, 5+1]
print(list)
#If you run the above code,[2.0, 'dddd', 6]return it.
#You can also calculate in the list
#"d"Is a character string, so if you multiply it by four"dddd"Becomes

2. Extraction of data in list (subset)

Now you know how to create a list. However, in order to perform analysis, it is necessary not only to create a list, but also to extract data to be used for analysis from it. Extracting this data is called a subset </ b>. Use the index </ b> to extract the data in the list. For example, take a look at the price above.

price = ["ItemA", 2, "ItemB", 2.5, "ItemC", 13, "ItemD", 1.15]

The first data in this list is assigned an index of 0 and the second data is assigned an index of 1. Here, the price data for Item C is located at the 6th position in this list, so that data is assigned an index of 5. And if you want to refer to the data of each index, use each parenthesis "[]" List name [index] </ b> It is described as. See the example below.

price = ["ItemA", 2, "ItemB", 2.5, "ItemC", 13, "ItemD", 1.15]
price[5]
#When I run this code, the data with index 5(13)return it
#Data with index 5 is 6th from the left

Of course, if you write price [6], the 7th "Item D" will be returned. You can also use a negative number as an index. If you use a negative number, the referenced data will be the nth data from the end of the list. For example, the price of ItemD is at the very end of the list, so you could write:

price = ["ItemA", 2, "ItemB", 2.5, "ItemC", 13, "ItemD", 1.15]
price[-1]
#When I run this code, the last data in the list(1.15)return it
#Negative number index represents nth from the end of the list
#price[7]But also, 1.Returns 15

Of course, the extracted data can be calculated as it is as follows.

price = ["ItemA", 2, "ItemB", 2.5, "ItemC", 13, "ItemD", 1.15]
print(price[1] + price[3])
#If you run this code, 4.Returns 5
#price[1]=2
#price[3]=2.5

Now, the only way to extract elements from a list is to specify only one in the index. You can also specify a range of lists, extract only the data contained in that range, and create a new list. This is called a slice </ b>. In extracting the list by index, we used square brackets [], but this time we will use colons in addition to the square brackets.

price = ["ItemA", 2, "ItemB", 2.5, "ItemC", 13, "ItemD", 1.15]
price[3:5]

Now, what kind of answer will be returned when running this code? It can be expected that it will probably return 3 to 5 elements of the index, that is, [2.5, "ItemC", 13]. However, the actual return is as follows.

[2.5, "ItemC"]

As you can see, it is a list of only data with indexes 3 and 4, and does not include data with index 5. In other words, when slicing the data in the list to create a new data list, use square brackets and a colon. [Beginning (included): End (not included)] </ b> It is necessary to describe.

By the way, when slicing, if you want to specify the data from the beginning (that is, index 0 starts), or if you want to specify the data to the end, you can omit the index.

price = ["ItemA", 2, "ItemB", 2.5, "ItemC", 13, "ItemD", 1.15]
price[:4]
#Index 0 is optional
#When you run the above["ItemA", 2, "ItemB", 2.5]return it.
price[3:]
#When you run the above[2.5, "ItemC", 13, "ItemD", 1.15]return it.
price[:]
#When you run the above["ItemA", 2, "ItemB", 2.5, "ItemC", 13, "ItemD", 1.15]And returns the entire list of prices.

Now, let's apply a little combination of what we have learned so far. Previously, I tried to make a list in the following list.

price = [["ItemA", 2], ["ItemB", 2.5], ["ItemC", 13], ["ItemD", 1.15]]

So what do you get back when you type price [1] [-1]? The answer is as follows.

price = [["ItemA", 2], ["ItemB", 2.5], ["ItemC", 13], ["ItemD", 1.15]]
price[1][-1]
#Running the above 2.Returns 5.
#First, price[1]Is processed. The element corresponding to index 1 in the first list["ItemB", 2.5]
#next,[-1]Is processed.["ItemB", 2.5]The last element in.5
#Therefore 2.5 is returned

3. Edit / add / delete list

Now you know how to create a list and how to extract the data in that list. From here, let's go one step further and see how to edit, add, and delete data in the list. To edit the data in the list, use square brackets [] as you did for the subset, and write: List name [Index of data you want to edit] = Data after editing </ b> For example, let's say the price of ItemD in the data in the price list changes from $ 1.15 to $ 2. The data in the list is incorrect as it is, so you need to modify 1.15 to 2.

price = ["ItemA", 2, "ItemB", 2.5, "ItemC", 13, "ItemD", 1.15]
#Original list of prices
price[7] = 2
#[]When=を使用して、上記のように記すWhen、データが更新される
#The contents of the updated list are["ItemA", 2, "ItemB", 2.5, "ItemC", 13, "ItemD", 2]

If you have a lot of parts to edit, it can be difficult to change them one by one. In such a case, you can use a colon (:) to specify the range at once and edit it. In the example below, ItemA has changed to the new version (ItemA_2) and the price has been raised from $ 2 to $ 2.25.

price = ["ItemA", 2, "ItemB", 2.5, "ItemC", 13, "ItemD", 1.15]
#Original list of prices
price[0:2] = ["ItemA_2", 2.25]
#The contents of the updated list are["ItemA_2", 2.25, "ItemB", 2.5, "ItemC", 13, "ItemD", 2]

Now, after editing the data, how to add / delete data. By the way, I mentioned last time that the result is different between the addition of String and the addition of integer. The processing of addition differs depending on each data type, but you can add more lists by adding list type data. As a test, let's add a new $ 1.25 ItemE to the list of prices.

price = ["ItemA", 2, "ItemB", 2.5, "ItemC", 13, "ItemD", 1.15]
#Original list of prices
price + ["ItemE", 1.25]
#Add another list to the price list
price_ver2 = price + ["ItemE", 1.25]
#In this way, you can also add data to make a new list
2.5, "ItemC", 13, "ItemD", 2]

Now, here's how to add data to the list. So how do you delete the data? The deletion is also very simple, using the code del and writing: del (list name [index name]) </ b> Let's try removing Item B from the list

price = ["ItemA", 2, "ItemB", 2.5, "ItemC", 13, "ItemD", 1.15]
#Original list of prices
del(price[2])
#Remove data with index 2 from the list
#The new list is["ItemA", 2, 2.5, "ItemC", 13, "ItemD", 1.15]Becomes

One point to note is that if you delete the data in the middle of the list, the index of the data after that will also shift according to the deleted data. For example, in the above example, ItemC had an index of 4 before it was deleted, but ItemC has an index of 3 because ItemB of index 2 has been deleted. So running del (price [2]) again will remove the index 2 element of the new list, 2.5.

4. List operating principle

Here's how the list actually works. It's a bit conceptual and more abstract, but it's important. Now, what is happening behind the scenes when you create the list below?

price = ["ItemA", 2, "ItemB", 2.5, "ItemC", 13, "ItemD", 1.15]

In fact, when you create a list, each element is not stored in a variable called price. When you create a list, it is saved in your computer's memory. And the address of that list (where it is in your computer's memory) is stored in a variable called price. In other words, price (variable) does not store each element of the list, but the address of the list </ b>. This isn't very important for basic operations, but it's important for copying lists. Let's take a concrete example. It stores the list of prices in a new list called price2 and changes ItemB in the list of price2 to ItemE.

price = ["ItemA", 2, "ItemB", 2.5, "ItemC", 13, "ItemD", 1.15]
price2 = price
price2[2] = "ItemE"
#Index 2 data(ItemB)To ItemE

Well, at this point the list of price2 is  ["ItemA", 2, "ItemE", 2.5, "ItemC", 13, "ItemD", 1.15] It has become. It's because ItemB was changed to ItemE by the operation of price2 [2] = "ItemE". And originally, the price list hasn't changed anything, so the price list is  ["ItemA", 2, "ItemB", 2.5, "ItemC", 13, "ItemD", 1.15] It should be. However, in reality, the contents of price are  ["ItemA", 2, "ItemE", 2.5, "ItemC", 13, "ItemD", 1.15] It has become. Do you understand? Both price and price2 are the contents are not the elements of the list, but the location where the list is stored in computer memory </ b>. By the operation of price2 = price, what is stored in price2 is just the location of the price list, and if you edit the data of price2, the price data itself in the same location will be edited as well. It ends up </ b>. There are two variables, price and price2, but only one list actually exists. So what if you really want to create another list with the same elements? In that case, use the list function as follows. You can also use [:] to select all the elements of the list introduced earlier to create a list of price2.

price = ["ItemA", 2, "ItemB", 2.5, "ItemC", 13, "ItemD", 1.15]
price2 = list(price)
#Create another list option1
price2 = price[:]
#Create another list option1

If you do this, you've created another list, so editing the price2 list doesn't affect the price list.

That's all for the Python list!

Recommended Posts