元組
基本概念、特性
- 順序存儲相同/不同類型的元素
- 定義:使用()將元素括起來,元素之間用“,”括開
- 特性:不可變,不支持添加,修改,刪除等操作
- 查詢:通過下標查詢元組指定位置的元素
- 其他
- 空元組定義:non_tuple = ()
- 只包含一個元素的元組:one_tuple = ("one",)
順序存儲相同/不同類型的元素
user_info = ("Wukong", 100, "male", "13834928470")
元組不同於列表,它不支持增,刪,改。
#不支持增刪改操作,例如刪除一個元組元素
del user_info[1]
輸出結果:
del user_info[1]
TypeError: 'tuple' object doesn't support item deletion
通過下標查詢元素
#查詢下標1的元素
age = user_info[1]
print("Age is %d"%age)
遍歷元組
for item in user_info:
print (item, end = "")
輸出結果:
Wukong
100
male
13834928470
字典
基本概念、特性
- 存儲key-value鍵值對類型的數據
- 定義:{key:value, key:value, ...};字典裏不能出現相同的鍵名
- 特性:具有增刪改操作
- 查詢:根據key查找value
- 內置方法:get,keys,values,items,clear
- 循環遍歷字典
內置方法keys的用法
user_info_dict = {"name":"zhangsan", "age":"30", "tel":"13523464219"}
for key in user_info_dict.keys(): #key值組成的列表
print(user_info_dict[key])
輸出結果:
zhangsan
30
13523464219
內置方法items的用法
#用法(1)
for item in user_info_dict.items():
print(type(item))
print(item[0]) #key
print(item[1]) #value
輸出結果:
<class 'tuple'>
name
zhangsan
<class 'tuple'>
age
30
<class 'tuple'>
tel
13523464219
#用法(二)
for key, value in user_info_dict.items():
print(key)
print(value)
輸出結果:
zhangsan
age
30
tel
13523464219
集合
基本概念、特性
- 無序存儲不同數據類型不重複元素的序列
- 定義:{“element1”,“element2”,element3“}
- 特性:無序存儲,可以對元素列表去重
- 方法:add,update(序列),remove,pop
- 集合操作:
- 交集:intersection
- 並集:union
- 差集:difference
- 對稱差集:symmetric_difference
集合對列表去重
id_list = ["id1", "id2", "id3", "id1", "id2"]
distinct_set = set(id_list) #去重
print(distinct_set)
輸出結果:
{'id1', 'id2', 'id3'}
集合對字符去重
string_set = set("hello")
print(string_set) #字符串看成是帶下標的列表,字符串會拆開然後列表去重
輸出結果:
{'h', 'o', 'e', 'l'}
Note:set是無序的。所以你再運行一次,列表的元素順序是變化的。
空集合
none_dict = {} #創建一個空字典
none_set = set() #創建一個空集合
print(none_set)
輸出結果:
set()
in 和not in
user_id = "id1"
if user_id in distinct_set:
print(user_id)
else:
print("not find")
輸出結果:
id1
add:添加元素到集合
name_set = {"zhangsan", "lisi"}
name_set.add("wangwu")
print(name_set)
輸出結果:
{'lisi', 'wangwu', 'zhangsan'}
update(序列)
name_set.update(["wukong", "lisi", "bajie"]) #列表中的每個元素去重後添加到set裏
print(name_set)
輸出結果:
{'wukong', 'bajie', 'lisi', 'zhangsan', 'wangwu'}
函數
函數定義
def FunName (parameter_list)
function block
return value
例子一:有參數有返回
def Sum(x, y):
sum = x + y
return sum
#函數調用
sum = Sum(1, 2)
print(sum)
輸出結果:
3
例子二:有多個返回
def x_y_comp_tuple(x, y):
res1 = x + y
res2 = x * y
return res1, res2
a, b = x_y_comp_tuple(2, 3)
print("a = {}, b = {}".format(a, b))
輸出結果:
a = 5, b = 6
例子三:列表作爲返回值
稍後填充
字符串 :常用內置方法
find(str[, start, end])
line = "hello world hello python"
print(line.find("world"))
print(line.find("hello"))
print(line.find("hello", 6)) #查找範圍從索引”6“開始
輸出結果:
6
0
12
count(str[, start, end])
print(line.count("hello")) #查找文本的某個字段或者某個字符串中某個單詞
輸出結果:
2
replace(old, new[, count])
new_line = line.replace("hello", "hi", 1) #count不指定就全部替換
print(line)
print(new_line)
輸出結果:
hello world hello python
hi world hello python
split(sep[, maxsplit])
line.split(" ") #以空格作爲分隔符,以列表方式返回
輸出結果:
['hello', 'world', 'hello', 'python']
#指定分隔的個數
line.split(" ", 1)
輸出結果:
['hello', 'world hello python']
startswith(prefix[, start, end])
endswith(suffix[, start, end])
upper:字符串所有字符大寫
lower:字符串所有字符小寫