一个可以存储多个元素的python数据类型
list(列表) tuple(元组) str(字符串) set(集合) dct(字典)
python的列表的数据类型可以是不同的?
my_list = ["1",123,True,[123,"3333",'d',False]]
for item in my_list:
print(f"type={type(item)}")
my_list = ["1",123,True,[123,"3333",'d',False]]
for item in my_list:
print(f"type={type(item)}")
#索引顺序取
print(my_list[0])
print(my_list[1])
print(my_list[2])
print(my_list[3])
#索引倒序取
print(my_list[-1])
print(my_list[-2])
print(my_list[-3])
print(my_list[-4])
#取嵌套元素
print(my_list[-1][0])
print(my_list[-1][1])
print(my_list[-1][2])
print(my_list[-1][3])
my_list = ["1",123,True,[123,"3333",'d',False]]
#获取元素下标
item = my_list.index("1")
print(f"item={item}")
item = my_list.index(123)
print(f"item={item}")
item = my_list.index(True)
print(f"item={item}")
my_list = ["1",123,True,[123,"3333",'d',False]]
#获取元素下标
item = my_list.index("1")
print(f"item={item}")
item = my_list.index(123)
print(f"item={item}")
item = my_list.index(True)
print(f"item={item}")
my_list = ["1",123,True,[123,"3333",'d',False]]
#list改
my_list[0] = "apple"
print(f"index=0{my_list[0]}")
#插入元素
my_list.insert(1,"origne")
print(f"my_list=0{my_list}")
#追加
my_list.append("water")
print(f"my_list=add {my_list}")
#把别的list里面的元素追加到一个list中
my_list2 = [1,3,5,8]
my_list.extend(my_list2)
print(f"my_list=extend {my_list}")
#删除
del my_list[1]
print(f"my_list=del {my_list}")
item = my_list.pop(0)
print(f"my_list=pop item= {item} list={my_list}")
my_list.remove(123)
print(f"my_list=remove {my_list}")
my_list.clear()
print(f"my_list= {my_list}")
#元素数量统计
my_list = [1,2,3,4,5,6,7,81,1,1,1,4,4,5,6,7,5,4]
count1 = my_list.count(1)
print(f"my_list= {my_list} item=1 count={count1} len={len(my_list)}")
#遍历
index = 0
while index < len(my_list):
print(f"index={index} element={my_list[index]}")
index += 1
for item in my_list:
print(f"type={type(item)}")
元组的内容是不支持修改的?
#定义 元组的元素类型不必统一
t1 = (1,"h",[1,2,3])
#空元组
t2 = ()
t3 = tuple()
#定义单个元素的元组
t4 = ("hello",)
#元组嵌套
t5 = ((1,2,3),(4,5,6))
print(f"element = {t5[1][2]}")
#元素索引
t6 = ("h","e","l","l","o")
print(f"index = {t6.index("o")}")
print(f"indexl = {t6.index("l")}")
print(f"count = {t6.count("l")}")
#长度
length = len(t6)
print(f"t6.length={length}")
#遍历
index = 0
while index < len(t6):
print(f"indx={index} item = {t6[index]}")
index += 1
for item in t6:
print(f"item = {item}")
str = "hello world"
value1 = str[0]
value2 = str[6]
print(f"value1={value1} value2={value2}")
value = str.index("w")
print(f"value={value}")
#字符串替换
newStr = str.replace("l","y")
print(f"newStr={newStr} str = {str}")
#字符串分割
my_str = "hello python item cast"
my_list = my_str.split(" ")
print(f"mystr={my_str} my_list={my_list}")
#字符串去除空格
my_str = " hello china "
my_str = my_str.strip()
print(f"mystr={my_str}")
#去除前后指定子符
my_str = "llc hello china elll"
new_str = my_str.strip("l")
print(f"mystr={my_str} new_str={new_str} lcount={my_str.count("l")}")
for item in my_str:
print(f"item={item}")
切片 从序列中取出的子序列
my_list = [0,1,2,3,4,5,6,7,8,9,10]
result = my_list[1:4]
print(f"result={result}")
my_tuple = (0,3,2,1,4,5,7,8)
result2 = my_tuple[:]
print(f"result2={result2}")
#开始:结尾:步长
my_str = "0123456789"
result3=my_str[::2]
print(f"结果是3:{result3}")
my_str = "0123456789"
result3=my_str[::-1]
print(f"结果是4:{result3}")
my_list = [0,1,2,3,4,5,6,7,8,9,10]
result4=my_list[3:1:-1]
print(f"result4: = {result4}")
my_tuple = (0,1,2,3,4,5,6,7)
result5 = my_tuple[::-2]
print(f"result5: = {result5}")
相对于list 无序 不重复 不支持索引
my_set = {"hello","world","good","morning","hello"}
set2 = set()
my_set.add("qwe")
print(f"myset={my_set}")
my_set.remove("hello")
print(f"myset={my_set}")
element = my_set.pop()
print(f"element = {element} myset={my_set}")
#清空
my_set.clear()
print(f"myset={my_set}")
#集和差集
set1 = {1,2,3,4,5}
set2 = {2,4,6,7,8}
set3 = set1.difference(set2)
print(f"set1={set1}")
print(f"set2={set2}")
print(f"set3={set3}")
print("=-=======")
#消除差集
set1.difference_update(set2)
print(f"set1={set1}")
print(f"set2={set2}")
print("=-=======")
#合并集合
set1 = {1,2,3,4,5}
set2 = {2,4,6,7,8}
set3 = set1.union(set2)
print(f"set1={set1}")
print(f"set2={set2}")
print(f"set3={set3}")
#统计集合元素个数
print(f"len={len(set3)}")
for item in set3:
print(f"item={item}")
key : value
#定义
dic = {"num":10,"name":"xxxx"}
dic1 = {}
dic3 = dict()
dic4 = {"title":"xxx","des":"qqqq","score":100,"downloadCount":33333}
allkey = dic4.keys()
print(f"allkey={allkey}")
for key in allkey:
print(f"key={key} value = {dic4[key]}")
#增删改查
#增
dic4["ccc"] = "dddddd"
print(f"dic4={dic4}")
#删
del dic4["score"]
print(f"dic4={dic4}")
dic4.pop("des")
print(f"dic4pop={dic4}")
dic4.clear()
print(f"dic4clear{dic4}")
#改=
dic4["title"] = "111"
print(f"dict[title]={dic4["title"]}")
#查
print(f"dict[title]={dic4["title"]}")