首页 > 其他 > 详细

list 列表

时间:2020-09-30 09:26:16      阅读:56      评论:0      收藏:0      [点我收藏+]

list

字符串、列表互转

print("abc#def#gd".split("#"))  # [‘abc‘, ‘def‘, ‘gd‘]

print(‘‘.join([abc, def, gd]))  # abcdefgd

 

list = [ abcd, 786 , 2.23, runoob, 70.2 ]
tinylist = [123, runoob]

print (list)            # 输出完整列表
print (list[0])         # 输出列表第一个元素
print (list[1:3])       # 从第二个开始输出到第三个元素
print (list[2:])        # 输出从第三个元素开始的所有元素
print (tinylist * 2)    # 输出两次列表
print (list + tinylist) # 连接列表

 与Python字符串不一样的是,列表中的元素是可以改变的:

>>> a = [1, 2, 3, 4, 5, 6]
>>> a[0] = 9
>>> a[2:5] = [13, 14, 15]
>>> a
[9, 2, 13, 14, 15, 6]
>>> a[2:5] = []   # 将对应的元素值设置为 []
>>> a
[9, 2, 6]

列表的一些骚操作:


list = [ ‘abcd‘, 786 , 2.23, ‘runoob‘, 70.2 ]
# list[0] = []
# print(list) # [[], 786, 2.23, ‘runoob‘, 70.2]
# list[0:1] = []
# print(list) # [786, 2.23, ‘runoob‘, 70.2]
# list[0:3] = []
# print(list) # [‘runoob‘, 70.2]
# list[0:3] = "a"
# print(list) # [‘a‘, ‘runoob‘, 70.2]
# list[0:3] = ‘a‘, ‘b‘, ‘c‘
# print(list) # [‘a‘, ‘b‘, ‘c‘, ‘runoob‘, 70.2]
# list[0:3] = [‘a‘, ‘b‘, ‘c‘]
# print(list) # [‘a‘, ‘b‘, ‘c‘, ‘runoob‘, 70.2]
# list[0:3] = (‘a‘, ‘b‘, ‘c‘)
# print(list) # [‘a‘, ‘b‘, ‘c‘, ‘runoob‘, 70.2]
# list[0:3] = {‘a‘, ‘b‘, ‘c‘}
# print(list) # [‘a‘, ‘b‘, ‘c‘, ‘runoob‘, 70.2]
# print(list[-1::-1]) # [70.2, ‘runoob‘, 2.23, 786, ‘abcd‘] 反转
 

 

list 列表

原文:https://www.cnblogs.com/tangjun112/p/13752745.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!