Hướng dẫn python store multiple lists
In the previous episode, we analyzed a single file of clinical trial inflammation data. However, after finding some peculiar and potentially suspicious trends in the trial data we ask Dr. Maverick if they have performed any other clinical trials. Surprisingly, they say that they have and provide us with 11 more CSV files for a further 11 clinical trials they have undertaken since the initial trial. Our goal now is to process all the inflammation data we have, which means that we still have eleven more files to go! The natural first step is to collect the names of all the files that we have to process. In Python, a list is a way to store multiple values together. In this episode, we will learn how to store multiple values in a list as well as how to work with lists. Python listsUnlike NumPy arrays, lists are built into the language so we do not have to load a library to use them. We create a list by putting values inside square brackets and separating the values with commas:
We can access elements of a list using indices – numbered positions of elements in the list. These positions are numbered starting at 0, so the first element has an index of 0.
Yes, we can use negative numbers as indices in Python. When we do so, the index There is one important difference between lists and strings: we can change the values in a list, but we cannot change individual characters in a string. For example:
works, but:
does not.
There are many ways to change the contents of lists besides assigning new values to individual elements:
While modifying in place, it is useful to remember that Python treats lists in a slightly counter-intuitive way. As we saw earlier, when we modified the
This is because Python stores a list in memory, and then can use multiple names to refer to the same list. If all we want to do is copy a (simple) list, we can again use the
Subsets of lists and strings can be accessed by specifying ranges of values in brackets, similar to how we accessed ranges of positions in a NumPy array. This is commonly referred to as “slicing” the list/string.
If you want to take a slice from the beginning of a sequence, you can omit the first index in the range:
And similarly, you can omit the ending index in the range to take a slice to the very end of the sequence:
|