暂无图片
暂无图片
暂无图片
暂无图片
暂无图片

【PCAP考试】Tuples、lists、dictionaries异同

学之初 学之时 2021-04-20
199


课程(免费)来源:
PCAP - Programming Essentials In Python 
English 0321b
撰文/Iris帆
排版/Iris帆
全文/1440字


序言

本期分享的重点围绕tuples、lists和dictionaries的异同展开,且所有解释都在代码内部。




## index使用区别


dictionary = {"cat": "chat", "dog": "chien", "horse": "cheval"}
phone_numbers = {'boss' : 5551234567, 'Suzy' : 22657854310}
empty_dictionary = { }


print(dictionary["cat"]) 
#输出:chat
print(dictionary.get('cat')) 
#输出:chat
print(phone_numbers['Suzy']) 
#输出:22657854310
print(phone_numbers.get('Suzy')) 
#输出:22657854310
#用key来输出value,在这里'cat'和'Suzy'为key。也可用dict.get()得到相同结果


print(dictionary[0]) 
# KeyError
print(phone_numbers[1]) 
# KeyError

lst = ["apple","banana","orange"]
tup = (1,('banana',"grape"),"orange")
print(lst[0]) 
#输出:apple
print(tup[1]) 
#输出:('banana', 'grape')



## sorted()
dictionary = {"cat": "chat", "dog": "chien", "horse": "cheval"}
lst = [2,6,3,0,9,1]
tup =(2,6,3,0,9,1)
print(sorted(dictionary)) 
#输出仅包括dict的key,未包含value
print(sorted(lst))
print(sorted(tup))
#输出:
#['cat', 'dog', 'horse']
#[0, 1, 2, 3, 6, 9]
#[0, 1, 2, 3, 6, 9]


## tuples形式读取dictionaries的信息


dictionary = {"cat": "chat", "dog": "chien", "horse": "cheval"}
for key in dictionary.keys():
    print(key, "->", dictionary[key])


dictionary = {"cat": "chat", "dog": "chien", "horse": "cheval"}
for english, french in dictionary.items():
    print(english, "->", french)


#两种方式的输出均为:
#cat -> chat
#dog -> chien
#horse -> cheval


dictionary = {"cat": "chat", "dog": "chien", "horse": "cheval"}
for french in dictionary.values():
    print(french)


#输出:
#chat
#chien
#cheval


dictionary = {"cat": "chat", "dog": "chien", "horse": "cheval"}
for english in dictionary.keys():
    print(english)


#输出:
#cat
#dog
#horse


### Dictionaries内部处理:以pol_eng_dictionary为例


pol_eng_dictionary = {
    "zamek": "castle",
    "woda": "water",
    "gleba": "soil"
    }
#上面的dictionary写法更简洁明了


## 修改diction内某个key的value


pol_eng_dictionary["zamek"] = "lock"
item = pol_eng_dictionary["zamek"]    
print(item)  
#输出: lock


## 删除dict内某个key(删除key同时也会删除value)


del pol_eng_dictionary['woda']
print(pol_eng_dictionary)
#输出:{'zamek': 'castle', 'gleba': 'soil'}


## 向dict添加新的key-value pair:dict.update()
## 删除dictionary最后一个key-value元素:dict.popitem()


pol_eng_dictionary = {"kwiat": "flower"}
pol_eng_dictionary.update({"gleba": "soil"})
print(pol_eng_dictionary)    
#输出: {'kwiat': 'flower', 'gleba': 'soil'}


pol_eng_dictionary.popitem()
print(pol_eng_dictionary)    
#输出: {'kwiat': 'flower'}


## del用于删除整个dictionary
##dict.clear()用来去除dictionary内所有元素


print(len(pol_eng_dictionary))    
#输出: 3
del pol_eng_dictionary["zamek"]   #remove an item
print(len(pol_eng_dictionary))    
#输出: 2


pol_eng_dictionary.clear()   
#removes all the items
print(len(pol_eng_dictionary))    
#输出: 0
print(pol_eng_dictionary) 
#输出:{ }


del pol_eng_dictionary    
#removes the dictionary
print(pol_eng_dictionary) 
#输出:Error


## dict.copy()用于复制一个dictionary


copy_dictionary = pol_eng_dictionary.copy()
print(copy_dictionary) 
#输出:{'zamek': 'castle', 'woda': 'water', 'gleba': 'soil'}


###Tuples
## tuple.count()用于计算某个元素在tuples里一共多少个


tup = 3, 2, 3, 2, 4, 5, 6, 2, 7, 2, 8, 9
print(tup.count(2))    
#输出: 4
print(tup.count(3))    
#输出: 2
print(tup.count(4))    
#输出: 1


## Tuples一些规则
tuple_1 = (1, 2, 4, 8)
print(tuple_1) 
tuple_2 = 1, 2, 4, 8
print(tuple_2)
tuple_3 = (1,)
print(tuple_3)
tuple_4 = (1)
print(tuple_4)


#输出:
#(1, 2, 4, 8)
#(1, 2, 4, 8)
#(1,)
#1


t1 = [1, ]
t2 = [1]
print(t1)
print(t2)


#输出:
[1]
[1]


var = 123
t1 = (1, )
t2 = (2, )
t3 = (3, var)
t1, t2, t3 = t2, t3, t1
print(t1, t2, t3)


#输出
(2,) (3, 123) (1,)

var = 123
t1 = [1, ]
t2 = [2, ]
t3 = [3, var]
t1, t2, t3 = t2, t3, t1
print(t1, t2, t3)


#输出
[2] [3, 123] [1]

今天的分享就到这里了,感谢学习路上的你我TA!



扫码关注我吧

学之初 学之时
我努力前行,想在停下来的时候,把人生分享给您
文章转载自学之初 学之时,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

评论