這里寫自定義目錄標題
- 串列 list
- 常用操作
- 回圈遍歷
- 元組 Tuple
- 常用操作:取值取索引、計數
- 元組的應用場景
- 元組、串列 的轉換
- 字典 dictionary
- 字典 常用操作:增刪改查
- 統計、合并
- 字典的遍歷
- 字典和串列
串列 list
- 其他語言中——陣列
- 存盤一串資訊
- [x,y,z]
- 串列的索引從0開始
- 不可超出所應范圍,會報錯
常用操作


list01 = ["1","2","3","3","2","6"]
# del 是從記憶體中洗掉
del list01[1]
print(list01)
# 統計長度
print(len(list01))
# 統計出現幾次
count = list01.count("3")
print(count)
# remove 串列中第一個出現的
list01.remove("3")
print(list01)
# 排序
# 升序
list01.sort()
print(list01)
# 降序
list01.sort(reverse=1)
print(list01)
# 反轉
list01.reverse()
print(list01)
回圈遍歷
list02 =["2","3","4"]
for i in list02:
print(i)
2
3
4
元組 Tuple
- 與list相似,但元素不能修改
- (x,y,z)
- 索引從0開始
- 創建空元組 tuple=()
- 通常存盤不同型別的資料
tuple =("woai",1,0.99)
print(type(tuple))
# python解釋器,直接認為是資料型別
# <class 'int'>
t=(2)
print(type(t))
# 要定義一個只包含一個元素的元組
# 要跟上逗號
# <class 'tuple'>
t1=(2,)
print(type(t1))
常用操作:取值取索引、計數
t = ("where are you now",1,2,2)
# 取值
print(t[0]) # where are you now
# 取索引
print(t.index("where are you now")) # 0
# 統計計數
print(t.count(2)) # 2
# 統計元組中元素的個數
print(len(t)) # 4
元組的應用場景
- 元組可作為函式的引數、回傳值
- 格式字串,本質上就是元組
- 讓串列不可以被修改,保護資料安全
tuple1 = ("aaa",21,188)
print("%s 年齡是%d的身高是%d" % tuple1)
tuple1str = "%s 年齡是 %d的身高是%d"%tuple1
print(tuple1str)
元組、串列 的轉換
# <class 'tuple'>
t = (1,23,34)
print(type(t))
# <class 'list'>
list(t)
print(type(list(t)))
字典 dictionary
- 串列是有序的 物件集合
- 字典是無序的 物件集合
{x,y,z}- 鍵值對 鍵:值

dict = {"name":"gggg",
"age":20,
"height":190
}
print(dict)
# {'name': 'gggg', 'age': 20, 'height': 190}
# 列印順序可能和字典的順序不一致
字典 常用操作:增刪改查
dict = {"name":"gggg",
"age":20,
"height":190
}
print(dict)
print("取值:",end="")
print(dict["name"])
print("增加、修改:",end="")
dict["weight"]=200 # key不存在,新增鍵值對
dict["name"]="ggg" # key存在,修改為小小明
print(dict)
print("洗掉:",end="")
dict.pop("name")
print(dict)
{'name': 'gggg', 'age': 20, 'height': 190}
取值:gggg
增加、修改:{'name': 'ggg', 'age': 20, 'height': 190, 'weight': 200}
洗掉:{'age': 20, 'height': 190, 'weight': 200}
統計、合并
dict = {"name":"gggg",
"age":20,
"height":190
}
# 鍵值對的數量
print("鍵值對的數量")
print(len(dict))
# 合并字典
# {'name': 'gggg', 'age': 20, 'height': 190, 'perfer': 111}
temp_dict = {
"perfer":111
}
dict.update(temp_dict)
print(dict)
# 清空字典
dict.clear()
print(dict)
字典的遍歷
for i in dict:
print("%s-%s"%(i,dict[i]))
字典和串列
字典嵌在 串列中
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/299818.html
標籤:python
上一篇:【RPC??012】必看的比json更好用的proto檔案導包說明[值得收藏]
下一篇:大小端存盤
