码迷,mamicode.com
首页 > 其他好文 > 详细

List和string

时间:2016-02-23 09:31:25      阅读:175      评论:0      收藏:0      [点我收藏+]

标签:

在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

 

List和string

标签:

原文地址:http://www.cnblogs.com/shouce/p/5208706.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!