python list
Slides in this set (2)
-
- list concatenation and replication
- >>> [1, 2, 3] + ['A', 'B', 'C'] [1, 2, 3, 'A', 'B', 'C']
- >>> ['X', 'Y', 'Z'] * 3 ['X', 'Y', 'Z', 'X', 'Y', 'Z', 'X', 'Y', 'Z']
- >>> spam = [1, 2, 3]
- >>> spam = spam + ['A', 'B', 'C']
- >>> spam [1, 2, 3, 'A', 'B', 'C']
- remove value from the list
- >>> spam = ['cat', 'bat', 'rat', 'elephant']
- >>> del spam[2]
- >>> spam ['cat', 'bat', 'elephant']
- >>> del spam[2]
- >>> spam ['cat', 'bat']
- example
- catNames = [] while True: print('Enter the name of cat ' + str(len(catNames) + 1) + ' (Or enter nothing to stop.):') name = input() if name == '': break catNames = catNames + [name] # list concatenation print('The cat names are:') for name in catNames: print(' ' + name)
- in and not in operator
- >>> 'howdy' in ['hello', 'hi', 'howdy', 'heyas']
- True
- >>> spam = ['hello', 'hi', 'howdy', 'heyas']
- >>> 'cat' in spam
- False
- >>> 'howdy' not in spam
- False
- >>> 'cat' not in spam True
- multiple assignment trick
- >>> cat = ['fat', 'black', 'loud']
- >>> size, color, disposition = cat
- get the index of element in a list, if mutiple occurrence, the first index will be returned
- >>> spam = ['hello', 'hi', 'howdy', 'heyas']
- >>> spam.index('hello')
- 0
- >>> spam.index('heyas')
- 3
- append values to list
- >>> spam = ['cat', 'dog', 'bat']
- >>> spam.append('moose')
- >>> spam
- ['cat', 'dog', 'bat', 'moose']
- insert values into the list
- >>> spam = ['cat', 'dog', 'bat']
- >>> spam.insert(1, 'chicken')
- >>> spam
- ['cat', 'chicken', 'dog', 'bat']
- remove value form the list
- >>> spam = ['cat', 'bat', 'rat', 'elephant']
- >>> spam.remove('bat')
- >>> spam
- ['cat', 'rat', 'elephant']
- sort the list
- >>> spam = [2, 5, 3.14, 1, -7]
- >>> spam.sort()
- >>> spam
- [-7, 1, 2, 3.14, 5]
- >>> spam = ['ants', 'cats', 'dogs', 'badgers', 'elephants']
- >>> spam.sort()
- >>> spam
- ['ants', 'badgers', 'cats', 'dogs', 'elephants']
- >>> spam.sort(reverse=True)
- >>> spam
- ['elephants', 'dogs', 'cats', 'badgers', 'ants']
- dont try this: spam = spam.sort().
- dont sort a list with number value and string values
- >>> spam = [1, 3, 2, 4, 'Alice', 'Bob']
- >>> spam.sort()
- Traceback (most recent call last): File "", line 1, in spam.sort() TypeError: unorderable types: str() < int()
- string and tuple are immutable, lists are mutable
- But the main way that tuples are different from lists is that tuples, like strings, are immutable. Tuples cannot have their values modified, appended, or removed. Enter the following into the interactive shell, and look at the TypeError error message:
- >>> eggs = ('hello', 42, 0.5)
- >>> eggs[1] = 99
- Traceback (most recent call last): File "", line 1, in eggs[1] = 99 TypeError: 'tuple' object does not support item assignment
- we can add a , at the end of the value to show the input type is tuple, not a string
- >>> type(('hello',)) //type tuple
- >>> type(('hello')) //type string
- If you need an ordered sequence of values that never changes, use a tuple.
- value type vs reference type
- >>> spam = 42
- >>> cheese = spam
- >>> spam = 100
- >>> spam
- 100
- >>> cheese
- 42
- >>> spam = [0, 1, 2, 3, 4, 5]
- >>> cheese = spam
- >>> cheese[1] = 'Hello!'
- >>> spam
- [0, 'Hello!', 2, 3, 4, 5]
- >>> cheese
- [0, 'Hello!', 2, 3, 4, 5]
- copy.copy() will copy the list into a new list, not just the reference. deepcopy() will copy the inner list also
- >>> import copy
- >>> spam = ['A', 'B', 'C', 'D']
- >>> cheese = copy.copy(spam)
- >>> cheese[1] = 42
- >>> spam
- ['A', 'B', 'C', 'D']
- >>> cheese
- ['A', 42, 'C', 'D']
-
- example of dictionary
- >>> myCat = {'size': 'fat', 'color': 'gray', 'disposition': 'loud'}
- dictionary doesnt have order like list
- >>> spam = ['cats', 'dogs', 'moose']
- >>> bacon = ['dogs', 'moose', 'cats']
- >>> spam == bacon False
- >>> eggs = {'name': 'Zophie', 'species': 'cat', 'age': '8'}
- >>> ham = {'species': 'cat', 'age': '8', 'name': 'Zophie'}
- >>> eggs == ham
- True
- example:
- python automation page 107
Information
- Description
- Slide Set on python list, created by Elvin Lee on 25/08/2018.
- Created by
- Elvin Lee
- Views
- 0
- Rating