1.8 比较列表和字符串

列表和字符串两种对象类型有不少相似的地方,也有很大的区别。有必要对它们做个简要的比较,以便能深刻理解,此外也是复习,“温故而知新”。

1.8.1 相同点

两者都属于序列类型,由此,那些属于序列的操作对两者都适用。

在对序列类型理解的基础上,用趋于专业的语言描述:它的每一个元素都可以通过指定一个编号(行话叫做“偏移量”或者“索引值”)的方式得到,而要想一次得到多个元素,可以使用切片。偏移量或者索引值从0开始,到总元素数减1结束。

  1. >>> welcome_str = "Welcome you"
  2. >>> welcome_str[0]
  3. 'W'
  4. >>> welcome_str[len(welcome_str) - 1]
  5. 'u'
  6. >>> welcome_str[:4]
  7. 'Welc'
  8. >>> a = "python"
  9. >>> a * 3
  10. 'pythonpythonpython'
  11.  
  12. >>> git_list = ["qiwsir","github","io"]
  13. >>> git_list[0]
  14. 'qiwsir'
  15. >>> git_list[len(git_list) - 1]
  16. 'io'
  17. >>> git_list[0:2]
  18. ['qiwsir', 'github']
  19. >>> b = ['qiwsir']
  20. >>> b * 7
  21. ['qiwsir', 'qiwsir', 'qiwsir', 'qiwsir', 'qiwsir', 'qiwsir', 'qiwsir']

还有一些操作是类似的:

  1. >>> first = "hello,world"
  2. >>> welcome_str
  3. 'Welcome you'
  4. >>> first+","+welcome_str
  5. 'hello,world,Welcome you'
  6.  
  7. >>> language = ['python']
  8. >>> git_list
  9. ['qiwsir', 'github', 'io']
  10. >>> language + git_list
  11. ['python', 'qiwsir', 'github', 'io']
  12.  
  13. >>> len(welcome_str)
  14. 11
  15. >>> len(git_list)
  16. 3

其他关于序列的基本操作,此处不再重复。

1.8.2 区别

列表和字符串的最大区别是:列表是可以改变,字符串不可变。

首先看对列表的这些操作,其特点是在原处将列表进行了修改:

  1. >>> git_list
  2. ['qiwsir', 'github', 'io']
  3.  
  4. >>> git_list.append("python")
  5. >>> git_list
  6. ['qiwsir', 'github', 'io', 'python']
  7.  
  8. >>> git_list[1]
  9. 'github'
  10. >>> git_list[1] = 'github.com'
  11. >>> git_list
  12. ['qiwsir', 'github.com', 'io', 'python']
  13.  
  14. >>> git_list.insert(1, "algorithm")
  15. >>> git_list
  16. ['qiwsir', 'algorithm', 'github.com', 'io', 'python']
  17.  
  18. >>> git_list.pop()
  19. 'python'
  20.  
  21. >>> del git_list[1]
  22. >>> git_list
  23. ['qiwsir', 'github.com', 'io']

以上这些操作,如果用在str上都会报错,比如:

  1. >>> welcome_str
  2. 'Welcome you'
  3.  
  4. >>> welcome_str[1]='E'
  5. Traceback (most recent call last):
  6. File "<stdin>", line 1, in <module>
  7. TypeError: 'str' object does not support item assignment
  8.  
  9. >>> del welcome_str[1]
  10. Traceback (most recent call last):
  11. File "<stdin>", line 1, in <module>
  12. TypeError: 'str' object doesn't support item deletion
  13.  
  14. >>> welcome_str.append("E")
  15. Traceback (most recent call last):
  16. File "<stdin>", line 1, in <module>
  17. AttributeError: 'str' object has no attribute 'append'

如果要修改一个str,不得不这样。

  1. >>> welcome_str
  2. 'Welcome you'
  3. >>> welcome_str[0]+"E"+welcome_str[2:] #重新生成一个str
  4. 'WElcome you'
  5. >>> welcome_str #对原来的没有任何影响
  6. 'Welcome you'

其实,在这种做法中就是重新生成了一个str。

1.8.3 多维列表

在字符串中,每个元素只能是字符,在列表中,元素可以是任何类型。前面见到的大多是数字或者字符,其实还可以这样:

  1. >>> matrix = [[1,2,3],[4,5,6],[7,8,9]]
  2. >>> matrix = [[1,2,3],[4,5,6],[7,8,9]]
  3. >>> matrix[0][1]
  4. 2
  5. >>> mult = [[1,2,3],['a','b','c'],'d','e']
  6. >>> mult
  7. [[1, 2, 3], ['a', 'b', 'c'], 'd', 'e']
  8. >>> mult[1][1]
  9. 'b'
  10. >>> mult[2]
  11. 'd'

以上显示了多维列表以及访问方式。在多维的情况下,里面的列表被当成一个元素对待。

1.8.4 列表和字符串的互相转化

以下涉及split()和join()两个函数,在前面字符串部分已经见过。一回生,二回熟,特别是在已经学习了列表的基础上见面,应该有更深刻的理解。

str.split()

这个内置函数实现的是将str转化为list。其中str=""是分隔符。

请先在交互模式下做如下操作:

  1. >>>help(str.split)

得到了对这个内置函数的完整说明。特别强调:这是一种非常好的学习方法(本书的特色就是教给读者一些方法,所谓“授人以鱼不如授人以渔”)。

split(…)S.split([sep[,maxsplit]])->list of strings

Return a list of the words in the string S,using sep as the delimiter string.If maxsplit is given,at most maxsplit splits are done.If sep is not specified or is None,any whitespace string is a separator and empty strings are removed from the result.

不管是否看懂上面这段话,都来看一下例子。

  1. >>> line = "Hello.I am qiwsir.Welcome you."
  2. >>> line.split(".") #以英文的句点为分隔符
  3. ['Hello', 'I am qiwsir', 'Welcome you', '']
  4.  
  5. #这个1,就是表达了官方文档中的:If maxsplit is given, at most maxsplit splits are done.
  6. >>> line.split(".",1)
  7. ['Hello', 'I am qiwsir.Welcome you.']
  8.  
  9. >>> name = "Albert Ainstain" #也有可能用空格作为分隔符
  10. >>> name.split(" ")
  11. ['Albert', 'Ainstain']

下面的例子,让你更有点惊奇了。

  1. >>> s = "I am, writing\npython\tbook on line"
  2. #这个字符串中有空格、逗号、换行\n、tab缩进\t 符号
  3.  
  4. >>> print s
  5. I am, writing
  6. python book on line
  7. >>> s.split() #用split(),但是括号中不输入任何参数
  8. ['I', 'am,', 'writing', 'python', 'book', 'on', 'line']

如果split()不输入任何参数,显示就是见到任何分割符号,就用其分割了。

1.8.5 "[sep]".join(list)

join可以说是split的逆运算,举例:

  1. >>> name
  2. ['Albert', 'Ainstain']
  3. >>> "".join(name)
  4. 'AlbertAinstain'
  5. >>> ".".join(name)
  6. 'Albert.Ainstain'
  7. >>> " ".join(name)
  8. 'Albert Ainstain'

回到上面那个神奇的例子中,可以这样使用join.:

  1. >>> s = "I am, writing\npython\tbook on line"
  2. >>> print s
  3. I am, writing
  4. python book on line
  5. >>> s.split()
  6. ['I', 'am,', 'writing', 'python', 'book', 'on', 'line']
  7. >>> " ".join(s.split()) #重新连接,不过有一点遗憾,am后面逗号还是有的。
  8. #怎么去掉?
  9. 'I am, writing python book on line'