列表
列表由中括号定义,里面元素由逗号分割,是一个内部放任何东西的集合
字符串在内存中连续存储,所以字符串的值没法改变,列表在内存中不连续存储,以链表形式存储,所以列表中的元素可以修改
telma = [5,"2",[9,0],"你是谁", False]
print(telma[3])
print(telma[2:-1])
for _ in telma:
print(_)
你是谁 [[9, 0], '你是谁'] 5 2 [9, 0] 你是谁 False
telma = [5,"2",[9,0],"你是谁", False] print(telma) telma[3] = ["i",2,3] #改元素 print(telma) del telma[4] #删除元素 print(telma) telma[0:2]=["2",5] print(telma) print(telma[2][1])
[5, '2', [9, 0], '你是谁', False] [5, '2', [9, 0], ['i', 2, 3], False] [5, '2', [9, 0], ['i', 2, 3]] ['2', 5, [9, 0], ['i', 2, 3]] 0
telma = "Iloveyou北京"
print(list(telma)) #字符串转列表,每个字符一个元素
lem=""
amlet = [1,2,3,"i","love","美女"] #列表转字符串
for i in amlet:
lem+=str(i)
print(lem)
list1 = ["1","2","3","lsk","我"] #列表里只有字符串
print("".join(list1))
['I', 'l', 'o', 'v', 'e', 'y', 'o', 'u', '北', '京'] 123ilove美女 123lsk我
telma = [1,3,"wer","中"]
amlet = telma.copy() # 浅拷贝
print(amlet)
telma.append([1,3]) #在最后加
print(telma)
amlet.extend([1,3]) #在最后拓展,比较和append的区别
print(amlet)
print(amlet.count(1)) #统计列表里元素数量
print(amlet.index(1))
telma.clear()
print(telma)
telma.extend("123")
print(telma)
telma.insert(0,"美") #在制定位置加
print(telma)
x = telma.pop() #m默认从最后一个元素删除
print(x)
print(telma)
y = telma.pop(1) #从制定位置删除
print(y)
print(telma)
telma.extend([3,2,3,2])
print(telma)
telma.remove(2) #删除第一个指定值的元素
print(telma)
telma.reverse()
print(telma)
[1, 3, 'wer', '中'] [1, 3, 'wer', '中', [1, 3]] [1, 3, 'wer', '中', 1, 3] 2 0 [] ['1', '2', '3'] ['美', '1', '2', '3'] 3 ['美', '1', '2'] 1 ['美', '2'] ['美', '2', 3, 2, 3, 2] ['美', '2', 3, 3, 2] [2, 3, 3, '2', '美']
浙公网安备 33010602011771号