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

 

posted @ 2010-03-25 21:59  Delcpp  阅读(931)  评论(1编辑  收藏  举报