日韩久久久精品,亚洲精品久久久久久久久久久,亚洲欧美一区二区三区国产精品 ,一区二区福利

Python中的字典與成員運算符初步探究

系統(tǒng) 1875 0

Python元字典
字典(dictionary)是除列表以外python之中最靈活的內(nèi)置數(shù)據(jù)結(jié)構(gòu)類型。列表是有序的對象結(jié)合,字典是無序的對象集合。
兩者之間的區(qū)別在于:字典當中的元素是通過鍵來存取的,而不是通過偏移存取。
字典用"{ }"標識。字典由索引(key)和它對應的值value組成。

            
#!/usr/bin/python
# -*- coding: UTF-8 -*-

dict = {}
dict['one'] = "This is one"
dict[2] = "This is two"

tinydict = {'name': 'john','code':6734, 'dept': 'sales'}


print dict['one'] # 輸出鍵為'one' 的值
print dict[2] # 輸出鍵為 2 的值
print tinydict # 輸出完整的字典
print tinydict.keys() # 輸出所有鍵
print tinydict.values() # 輸出所有值


          

輸出結(jié)果為:

            
This is one This is two {'dept': 'sales', 'code': 6734, 'name': 'john'} ['dept', 'code', 'name'] ['sales', 6734, 'john']
          

Python成員運算符
除了以上的一些運算符之外,Python還支持成員運算符,測試實例中包含了一系列的成員,包括字符串,列表或元組。

以下實例演示了Python所有成員運算符的操作:

            
#!/usr/bin/python

a = 10
b = 20
list = [1, 2, 3, 4, 5 ];

if ( a in list ):
  print "Line 1 - a is available in the given list"
else:
  print "Line 1 - a is not available in the given list"

if ( b not in list ):
  print "Line 2 - b is not available in the given list"
else:
  print "Line 2 - b is available in the given list"

a = 2
if ( a in list ):
  print "Line 3 - a is available in the given list"
else:
  print "Line 3 - a is not available in the given list"


          

以上實例輸出結(jié)果:

            
Line 1 - a is not available in the given list
Line 2 - b is not available in the given list
Line 3 - a is available in the given list
          


更多文章、技術(shù)交流、商務合作、聯(lián)系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯(lián)系: 360901061

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦!!!

發(fā)表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 宁远县| 达孜县| 芷江| 霸州市| 泌阳县| 肇州县| 长泰县| 怀化市| 克山县| 象山县| 杂多县| 全州县| 克什克腾旗| 怀化市| 绥芬河市| 称多县| 肇源县| 金寨县| 高雄县| 济源市| 阳山县| 贵阳市| 灵丘县| 东平县| 长沙县| 宜川县| 岫岩| 廊坊市| 泾源县| 高清| 承德市| 扶绥县| 雷州市| 阳信县| 西乌珠穆沁旗| 西乌| 遂川县| 通道| 延津县| 霸州市| 英德市|