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‘] 反转
原文:https://www.cnblogs.com/tangjun112/p/13752745.html