第 12 章

测试题

  1. 可以使用 append()insert()extend() 向列表增加元素。

  2. 可以使用 remove()pop()del() 从列表删除元素。

  3. 要得到列表的一个有序副本,可以采用下面任意一种做法:

  • 建立列表的副本,使用分片:new_list = my_list[:],然后对新列表排序:new_list.sort()

  • 使用 sorted() 函数:new_list = sorted(my_list)

  1. 使用 in 关键字可以得出一个特定值是否在一个列表中。

  2. 使用 index() 方法可以得出一个值在列表中的位置。

  3. 元组是一个与列表类似的集合,只不过元组不能改变。元组是不可改变的, 而列表是可改变的。

  4. 可以采用多种方法建立一个双重列表。

  • 使用嵌套的中括号:
  1. >>> my_list =[[1, 2, 3],['a','b','c'],['red', 'green', blue']]
  • 使用 append(),并追加一个列表:
  1. >>> my_list = []
  2. >>> my_list.append([1, 2, 3])
  3. >>> my_list.append(['a', 'b', 'c'])
  4. >>> my_list.append(['red', 'green', 'blue'])
  5. >>> print my_list
  6. [[1, 2, 3], ['a', 'b', 'c'], ['red', 'green', 'blue']]
  • 建立单个列表,再合并这些列表:
  1. >>> list1 = [1, 2, 3]
  2. >>> list2 = ['a', 'b', 'c']
  3. >>> list3 = ['red', 'green', 'blue']
  4. >>> my_list = [list1, list2, list3]
  5. >>> print my_list
  6. [[1, 2, 3], ['a', 'b', 'c'], ['red', 'green', 'blue']]
  1. 可以使用两个索引得到双重列表中的一个值:
  1. my_list = [[1, 2, 3], ['a', 'b', 'c'], ['red', 'green', 'blue']]
  2. my_color = my_list[2][1]

这个答案是 'green'

  1. 字典是键值对的集合。

  2. 你可以通过指定键和值的方式在字典中添加条目:

  1. phone_numbers['John'] = '555-1234'
  1. 要通过键在字典中查找一个条目,可以使用索引:
  1. print phone_numbers['John']

动手试一试

  1. 下面这个程序会得到 5 个名字,把它们放在一个列表中,然后打印出来:
  1. nameList = []
  2. print "Enter 5 names (press the Enter key after each name):"
  3. for i in range(5):
  4. name = raw_input()
  5. nameList.append(name)
  6. print "The names are:", nameList
  1. 下面这个程序会打印原来的列表和排序后的列表:
  1. nameList = []
  2. print "Enter 5 names (press the Enter key after each name):"
  3. for i in range(5):
  4. name = raw_input()
  5. nameList.append(name)
  6. print "The names are:", nameList
  7. print "The sorted names are:", sorted(nameList)
  1. 下面这个程序只打印列表中的第 3 个名字:
  1. nameList = []
  2. print "Enter 5 names (press the Enter key after each name):"
  3. for i in range(5):
  4. name = raw_input()
  5. nameList.append(name)
  6. print "The third name is:", nameList[2]
  1. 下面这个程序允许用户替换列表中的一个名字:
  1. nameList = []
  2. print "Enter 5 names (press the Enter key after each name):"
  3. for i in range(5):
  4. name = raw_input()
  5. nameList.append(name)
  6. print "The names are:", nameList
  7. print "Replace one name. Which one? (1-5):",
  8. replace = int(raw_input())
  9. new = raw_input("New name: ")
  10. nameList[replace - 1] = new
  11. print "The names are:", nameList
  1. 下面这个程序允许用户创建包含单词和对应定义的词典:
  1. user_dictionary = {}
  2. while 1:
  3. command = raw_input("'a' to add word,'l' to lookup a word,'q' to quit")
  4. if command == "a":
  5. word = raw_input("Type the word: ")
  6. definition = raw_input("Type the definition: ")
  7. user_dictionary[word] = definition
  8. print "Word added!"
  9. elif command == "l":
  10. word = raw_input("Type the word: ")
  11. if word in user_dictionary.keys():
  12. print user_dictionary[word]
  13. else:
  14. print "That word isn't in the dictionary yet."
  15. elif command == 'q':
  16. break