字典使用名稱—內容進行資料結構的構建,在python中分別對應著鍵(key)—值(value),習慣上稱為鍵值對,下面舉個例子:
上面就是字典的寫法,那麼我們來一一試驗字典的性質:city_popularity=
這是字典元素的新增city_popularity=
city_popularity[
'深圳']=
'fourth'
(city_popularity)
#
新增多個元素用update()方法city_popularity=
city_popularity.update(
(city_popularity)
#
刪除字典中元素可以用del方法city_popularity=
del city_popularity[
'杭州'
(city_popularity)
#
注意字典中只能用相應鍵來獲得對應值,不能用索引city_popularity=
(city_popularity[
'上海'])
# first
集合中元素是無序的而且各不相同,所以不能用索引和切片
只可以新增和刪除其中的元素,例如:
上面是新增和刪除集合元素的例子a=
a.add(5)
(a)#
a.discard(5)
(a)#
python之集合與字典
用索引關鍵字 brand 鯉魚 校園 西安市 fill daa vxx poo print 西安市對應的是 fill brand.index 西安市 用字典 dict1 print 校園對應的為 dict1 校園 dict2 print dict2 2 dict3 dict f 1 a 2 prin...
python之字典與集合
1 字典中每一條資料都是有乙個鍵值對來組成 key value 2 空字典的定義 dic 2 取值 dic key value 1 通過鍵直接賦值,可以直接新增元素,對已存在的鍵,可以修改對應的資料 2 字典一次性新增多個元素 dic.update print dic.keys dict keys ...
python基礎之字典與集合
字典 python內建了字典 使用鍵 值 key value 儲存,具有極快的查詢速度。用dict實現,無論這個表有多大,查詢速度都不會變慢。用python寫乙個dict如下 把資料放入dict的方法,除了初始化時指定外,還可以通過key放入 由於乙個key只能對應乙個value,所以,多次對乙個k...