List和string

在Python的List处理中,string好像被看成是由单个字符组成的List了。。。。

请看下面代码的lst4和lst6的操作

代码
#coding:gb2312
lst1 = [1,2,3,4]
lst1.append([
5,6,7,8])
print lst1 #打印结果:[1, 2, 3, 4, [5, 6, 7, 8]]

lst2
= [1,2,3,4]
lst2.append(
"5678")
print lst2 #打印结果:[1, 2, 3, 4, '5678']

lst3
= [1,2,3,4]
lst3.extend([
5,6,7,8])
print lst3 #打印结果:[1, 2, 3, 4, 5, 6, 7, 8]

lst4
= [1,2,3,4]
lst4.extend(
"5678")
print lst4 #打印结果:[1, 2, 3, 4, '5', '6', '7', '8']

lst5
= []
for i in range(10):
lst5
+= [i]
print lst5 #打印结果:[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

lst6
= []
for i in range(10): #打印结果:['a', 'b', 'a', 'b', 'a', 'b', 'a', 'b', 'a', 'b', 'a', 'b', 'a', 'b', 'a', 'b', 'a', 'b', 'a', 'b']
lst6 += 'ab'
print lst6

但是直接用list+string就不可以。

lst1 = []
print lst1
lst2
= lst1 + '123456' #TypeError: can only concatenate list (not "str") to list
print lst2
没事,别怕,这是签名→→┃ 青山幽谷笛声扬,白鹤振羽任翱翔。往事前尘随风逝,携手云峰隐仙乡。 ┃
原文地址:https://www.cnblogs.com/dabiao/p/1696179.html