Continue from part 3.
Python Lists
In short, Lists are used to store multiple items in a single variable, they are convenient data structures for representing a
. In that regard, a list is similar to a string, except a string can hold only characters, however, a list can hold any Python object (can either be the same data type, or varying types).There are several other ways that Python provides for storing aggregate data: tuples, dictionaries, set. But let’s focus on Lists first.
Lists are created by putting elements, comma-separated, and enclosing by square brackets:
lst = [5, -6, 3, 0, -1]
Lists are pretty important and and widely used, you could see them in nearly every Python program. I’m interested in matrices, linear algebra, matrix transformations and arrays because I’m working with finite element software, so lists, as well as NumPy arrays (I would like to talk about NumPy later too) are an integral part to learn.
A list can also be empty to be put objects in later:
a = [ ]
To access the individual element contained in a list via their position within the list. We can use square brackets like list [index]:
lst = [1, -4, 0, 6, -3] # Assign a list wih objects lst[0] = 5 # Make the first element 5 print(lst[1]) # Print the second element lst[4] = 10 # Make the last element 10 print([1, 2, 3][1]) # Print second element of given list print(lst) # Print a list via a variable
The number within the square brackets is called the index. The subscript terminology is borrowed from mathematicians who use subscripts to reference elements in a mathematical vector or matrix. Unlike the convention often used in mathematics, however, the first element in a list is at position zero, not one. So in the above example, the first element of list lst is lst[0], not lst[1], and lst[1] is second element.
So it can be inferred that, of a zero beginning index, if list a holds n elements, the last element in lst is lst[n-1], not lst[n]
If lst is a list with n elements, and i is an integer such that 0 ≤ i <n, then lst[n] is an element in the list.
There is also negative list index. A negative list index represents a negative offset from an imaginary element one (index 0) past the end of the list. So that means for a list lst, the expression lst[-1] represents the last element in lst.
For the example below, I’ll print a list call data in reverse, to evince the use of negative indices.
def print_some_list(): data = [10, 20, 30, 40, 50, 60, 70, 80] for i in range(len(data) + 1): if i == 0: continue print(data[-i]) print_some_list()
Lists can contain any arbitrary objects, or heterogeneous, a list can hold elements of varying types.
Lists can be nested, and lists are also mutable.
Here’s an example of a convoluted list, but it’s really not that complicated when you already grasp the basic:
lst = [9.5, "apple", 69, "W", ["chocolate", 5, "pie"], 777, "KyoStinV"]
lst = [9.5, "apple", 69, "W", ["chocolate", 5, "pie"], 777, "KyoStinV"] print(lst[0], lst[-7]) # lst[0] == lst[-7] = 9.5 print(lst[1], lst[-6]) # lst[1] == lst[-6] = 'apple' print(lst[1][2], lst[-6][2]) # lst[1][2] == lst[-6][2] = 'p' print(lst[4][2][0], lst[-3][2][0], lst[4][-1][0], lst[-3][-1][-3]) # lst[4][2][0] == lst[-3][2][0] == lst[4][-1][0] == lst[-3][-1][-3] = 'p'
Note that, here the second and third indices come after lst are belong to str element, since string is also a sequence data type.
(To be continued…)
Để lại bình luận: