python函數調用另一個函數,python字典調用_python 字典訪問的三種方法

 2023-10-08 阅读 29 评论 0

摘要:定義字典 dic = {'a':"hello",'b':"how",'c':"you"} 方法一: python函數調用另一個函數。for key in dic: print key,dic[key] print key + str(dic[key]) python中如何將字典里的值取出來。結果: a h

定義字典 dic = {'a':"hello",'b':"how",'c':"you"}

方法一:

python函數調用另一個函數。for key in dic:

print key,dic[key]

print key + str(dic[key])

python中如何將字典里的值取出來。結果:

a hello

ahello

python調用c語言?c you

cyou

b how

bhow

細節:

print key,dic[key],后面有個逗號,自動生成一個空格

print key + str(dic[key]),連接兩個字符串,用的是加號,直接輸出,中間不加逗號

方法二:

for (k,v) in dic.items():

print "dic[%s]="%k,v

結果:

dic[a]= hello

dic[c]= you

dic[b]= how

方法三:

for k,v in dic.iteritems():

print "dic[%s]="%k,v

結果:

dic[a]= hello

dic[c]= you

dic[b]= how

對比:

items()返回的是列表對象,而iteritems()返回的是iterator對象。例如:

print dic.items() #[('a', 'hello'), ('c', 'you'), ('b', 'how')]

print dic.iteritems() #

深究:iteritor是迭代器的意思,一次反悔一個數據項,知道沒有為止

for i in dic.iteritems():

print i

結果:('a', 'hello')

('c', 'you')

('b', 'how')

版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。

原文链接:https://hbdhgg.com/5/128913.html

发表评论:

本站为非赢利网站,部分文章来源或改编自互联网及其他公众平台,主要目的在于分享信息,版权归原作者所有,内容仅供读者参考,如有侵权请联系我们删除!

Copyright © 2022 匯編語言學習筆記 Inc. 保留所有权利。

底部版权信息