我们在前面的文章中已经看到过字符串、列表等序列类型。元组是另外一种序列类型。这一章我们学习更多关于字符串、元组、字典和集合的操作。
>>> book = "Alchemist"
>>> book[0]
'A'
>>> book[3]
'h'
>>> book[-1]
't'
>>> book[10]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
IndexError: string index out of range
>>> book[2:6]
'chem'
>>> book[:5]
'Alche'
>>> book[5:]
'mist'
>>> book[::-1]
'tsimehclA'
>>> book[:]
'Alchemist'
>>> list(book)
['A', 'l', 'c', 'h', 'e', 'm', 'i', 's', 't']
>>> import string
>>> string.ascii_lowercase[:10]
'abcdefghij'
>>> list(string.digits)
['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']>>> book
'Alchemist'
>>> for char in book:
... print(char)
...
A
l
c
h
e
m
i
s
t>>> book
'Alchemist'
>>> len(book)
9
>>> book.index('A')
0
>>> book.index('t')
8
>>> 'A' in book
True
>>> 'B' in book
False
>>> 'z' not in book
True
>>> min('zealous')
'a'
>>> max('zealous')
'z'>>> north_dishes = ('Aloo tikki', 'Baati', 'Khichdi', 'Makki roti', 'Poha')
>>> north_dishes
('Aloo tikki', 'Baati', 'Khichdi', 'Makki roti', 'Poha')
>>> north_dishes[0]
'Aloo tikki'
>>> north_dishes[-1]
'Poha'
>>> north_dishes[6]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
IndexError: tuple index out of range
>>> north_dishes[::-1]
('Poha', 'Makki roti', 'Khichdi', 'Baati', 'Aloo tikki')
>>> north_dishes[0] = 'Poori'
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'tuple' object does not support item assignment>>> 'roti' in north_dishes
False
>>> 'Makki roti' in north_dishes
True
>>> len(north_dishes)
5
>>> min(north_dishes)
'Aloo tikki'
>>> max(north_dishes)
'Poha'
>>> for dish in north_dishes:
... print(dish)
...
Aloo tikki
Baati
Khichdi
Makki roti
Pohaenumerate用于迭代列表时我们已经看过例子了>>> a = 5
>>> b = 20
>>> a, b = b, a
>>> a
20
>>> b
5
>>> c = 'foo'
>>> a, b, c = c, a, b
>>> a
'foo'
>>> b
20
>>> c
5
>>> def min_max(arr):
... return min(arr), max(arr)
...
>>> min_max([23, 53, 1, -34, 9])
(-34, 53)()>>> words = 'day', 'night'
>>> words
('day', 'night')
>>> coordinates = ((1,2), (4,3), (92,3))
>>> coordinates
((1, 2), (4, 3), (92, 3))
>>> prime = [2, 3, 5, 7, 11]
>>> prime_tuple = tuple((idx + 1, num) for idx, num in enumerate(prime))
>>> prime_tuple
((1, 2), (2, 3), (3, 5), (4, 7), (5, 11))list()相似>>> tuple('books')
('b', 'o', 'o', 'k', 's')
>>> a = [321, 899.232, 5.3, 2, 1, -1]
>>> tuple(a)
(321, 899.232, 5.3, 2, 1, -1)>>> a = [(1,2), ['a', 'b'], ('good', 'bad')]
>>> a
[(1, 2), ['a', 'b'], ('good', 'bad')]
>>> b = ((1,2), ['a', 'b'], ('good', 'bad'))
>>> b
((1, 2), ['a', 'b'], ('good', 'bad'))>>> nums = {3, 2, 5, 7, 1, 6.3}
>>> nums
{1, 2, 3, 5, 6.3, 7}
>>> primes = {3, 2, 11, 3, 5, 13, 2}
>>> primes
{2, 3, 11, 13, 5}
>>> nums.union(primes)
{1, 2, 3, 5, 6.3, 7, 11, 13}
>>> primes.difference(nums)
{11, 13}
>>> nums.difference(primes)
{1, 6.3, 7}>>> len(nums)
6
>>> nums[0]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'set' object does not support indexing
>>> book
'Alchemist'
>>> set(book)
{'i', 'l', 's', 'A', 'e', 'h', 'm', 't', 'c'}
>>> set([1, 5, 3, 1, 9])
{1, 9, 3, 5}
>>> list(set([1, 5, 3, 1, 9]))
[1, 9, 3, 5]
>>> nums = {1, 2, 3, 5, 6.3, 7}
>>> nums
{1, 2, 3, 5, 6.3, 7}
>>> nums.pop()
1
>>> nums
{2, 3, 5, 6.3, 7}
>>> nums.add(1)
>>> nums
{1, 2, 3, 5, 6.3, 7}
>>> 6.3 in nums
True
>>> for n in nums:
... print(n)
...
1
2
3
5
6.3
7>>> marks = {'Rahul' : 86, 'Ravi' : 92, 'Rohit' : 75}
>>> marks
{'Ravi': 92, 'Rohit': 75, 'Rahul': 86}
>>> fav_books = {}
>>> fav_books['fantasy'] = 'Harry Potter'
>>> fav_books['detective'] = 'Sherlock Holmes'
>>> fav_books['thriller'] = 'The Da Vinci Code'
>>> fav_books
{'thriller': 'The Da Vinci Code', 'fantasy': 'Harry Potter', 'detective': 'Sherlock Holmes'}
>>> marks.keys()
dict_keys(['Ravi', 'Rohit', 'Rahul'])
>>> fav_books.values()
dict_values(['The Da Vinci Code', 'Harry Potter', 'Sherlock Holmes'])>>> for book in fav_books.values():
... print(book)
...
The Da Vinci Code
Harry Potter
Sherlock Holmes
>>> for name, mark in marks.items():
... print(name, mark, sep=': ')
...
Ravi: 92
Rohit: 75
Rahul: 86
>>> import pprint
>>> pp = pprint.PrettyPrinter(indent=4)
>>> pp.pprint(fav_books)
{ 'detective': 'Sherlock Holmes',
'fantasy': 'Harry Potter',
'thriller': 'The Da Vinci Code'}>>> marks
{'Ravi': 92, 'Rohit': 75, 'Rahul': 86}
>>> marks['Rajan'] = 79
>>> marks
{'Ravi': 92, 'Rohit': 75, 'Rahul': 86, 'Rajan': 79}
>>> del marks['Ravi']
>>> marks
{'Rohit': 75, 'Rahul': 86, 'Rajan': 79}
>>> len(marks)
3
>>> fav_books
{'thriller': 'The Da Vinci Code', 'fantasy': 'Harry Potter', 'detective': 'Sherlock Holmes'}
>>> "fantasy" in fav_books
True
>>> "satire" in fav_books
Falsekeys()方法的输出必须改变为像list或者tuple这样的序列类型传入random.choice>>> north = ['aloo tikki', 'baati', 'khichdi', 'makki roti', 'poha']
>>> south = ['appam', 'bisibele bath', 'dosa', 'koottu', 'sevai']
>>> west = ['dhokla', 'khakhra', 'modak', 'shiro', 'vada pav']
>>> east = ['hando guri', 'litti', 'momo', 'rosgulla', 'shondesh']
>>> dishes = {'North': north, 'South': south, 'West': west, 'East': east}
>>> rand_zone = random.choice(tuple(dishes.keys()))
>>> rand_dish = random.choice(dishes[rand_zone])
>>> print("Try the '{}' speciality '{}' today".format(rand_zone, rand_dish))
Try the 'East' speciality 'rosgulla' today进一步阅读
[1]Python文档 - 元组: https://docs.python.org/3/library/stdtypes.html#tuple
[2]Python文档 - 元组教程: https://docs.python.org/3/tutorial/datastructures.html#tuples-and-sequences
[3]Python文档 - 集合: https://docs.python.org/3/library/stdtypes.html#set
[4]Python文档 - frozenset: https://docs.python.org/3/library/stdtypes.html#frozenset
[5]集合教程: http://www.programiz.com/python-programming/set
[6]Python文档 - 字典: https://docs.python.org/3/library/stdtypes.html#dict
[7]Python文档 - pprint: https://docs.python.org/3/library/pprint.html
[8]字典详细教程: http://www.sharats.me/posts/the-python-dictionary/