Вы находитесь на странице: 1из 3

Python Data Structures: Lists

Lists
A list is an ordered set of values, where each value is identified by an index.
The values that make up a list are called its elements.
Lists are similar to strings, which are ordered sets of characters, except that the elements of a list can
have any type.
Lists and strings and other things that behave like ordered sets are called sequences.
Python lists can contain:
numbers
strings
numbers and strings
numbers, strings and functions
numbers, strings, functions and lists
The empty list
An empty list is often used to initialize a list:
>>> fillMe = []
Like numeric 0 values and the empty string, the empty list is false in a boolean expression:
>>> if fillMe:
# can also write if []:
...
print 'This is true.'
... else:
...
print 'This is false.'
...
This is false.
Example of different types of lists
A list of numbers:
>>> xlist = [1, 2, 3, 4]
Strings:
>>> ylist = ['a', 'b', 'c', 'd']
Numbers and strings:
>>> comboList1 = [1, 2, 'd', 'e']
Numbers, strings and functions:
>>> # Need to define a function first if it is to be contained in a
list
>>> def sumCart(item1, item2):
...
totalCost = item1 + item2
...
print totalCost
...
>>> zlist = [1, 4, 'a', 'wassup?', sumCart]

>>> print zlist


[1, 4, 'a', 'wassup?', <function sumCart at 0x2c98f50>]
Numbers, strings, functions and lists. A list within a list is said to be nested:
>>> blist = [1, 5, xlist, sumCart, 'go there']
>>> print blist
[1, 5, [9, 2, 3, 4], <function sumCart at 0x2c98f50>, 'go there']

Generating a list
Using a while loop
>>>
>>>
>>>
...
...
...
...
...
[4]
[4,
[4,

nums = list()
i = 4
while (i < 9):
nums.append(i)
i = i + 2
print nums

6]
6, 8]

Using the range function


There is actually a much simpler way to generate the previous sequences, using a further variation of
the range function. Enter these lines separately in the Shell. As in the simpler applications of range,
the values are only generated one at a time, as needed. To see the entire sequence at once, convert the
sequence to a list before printing:
>>> nums = range(4, 9, 2)
>>> print list(nums)
[4, 6, 8]
The third parameter for the range function is the step size. It is needed when the step size from one
element to the next is not 1.
The most general syntax is
range( start , pastEnd , step )
The value of the second parameter is always past the final element of the list. Each element after the
first in the list is step more than the previous one. Predict and try in the Shell:
>>> list(range(4, 10, 2))
[4, 6, 8]
Actually the range function is even more sophisticated than indicated by the while loop above. The
step size can be negative. Try in the Shell:
>>> list(range(10, 0, -1))

[10, 9, 8, 7, 6, 5, 4, 3, 2, 1]

Copying a list by slicing


Suppose we want to copy xlist, and we do the following:
>>> alist = xlist
>>> print alist
[1, 2, 3, 4]
Now, if we change the xlist:
>>> xlist[0] = 9
>>> print xlist
[9, 2, 3, 4]
We find that alist has also changed, which we DID NOT WANT:
>>> print alist
[9, 2, 3, 4]
Why does this happen? Because both xlist and alist point to the same list.
Solution: use slicing.
Slice the list. This makes a copy of xlist and stores it in the new list.
Example of list copying by slicing
>>> clist = blist[:]
>>> blist[1] = 100 # change a blist value
>>> print blist
[1, 100, [9, 2, 3, 4], <function sumCart at 0x2c98f50>, 'go there']
>>> print clist # clist remains a copy of original blist
[1, 5, [9, 2, 3, 4], <function sumCart at 0x2c98f50>, 'go there']

Вам также может понравиться