特殊的資料型別
>>> list1 = [1, 'wang', 'tao'] >>> tuple1 = (2, 'wang', 'tao', 'yuan') #tuple的内容不可更改 >>> dict1 = { #dict的元素分为 key:value两部分,key不能重复 'wang':90, 'tao':99, 'yuan':100 }
dict 的用法,轉自
OperationResultNoteslen(a)the number of items in a 得到字典中元素的個數 a[k]the item of a with key k 取得鍵K所對應的值(1), (10)a[k ] = vset a[k] to v 設定鍵k所對應的值成為v del a[k]remove a[k] from a 從字典中刪除鍵為k的元素(1)a.clear()remove all items from a 清空整個字典 a.copy()a (shallow) copy of a 得到字典副本 k in aTrue if a has a key k, else False 字典中存在鍵k則為返回True,沒有則返回False(2) k not in aEquivalent to not k in a 字典中不存在鍵k則為返回true,反之返回False(2)a.has_key(k)Equivalent to k in a, use that form in new code 等價於k in a k in a, use that form in new code 等價於k in a a k in a, new code 等價於k in a a a0 a.items()a copy of a's list of (key, value) pairs 得到一個鍵,值的list(3)a.keys()a copy of a's list of keys 得到鍵的list(3)a.update( [b])updates (and overwrites) key/value pairs from b從b字典更新a字典,如果鍵相同則更新,a中不存在則追加(9)a.fromkeys(seq[, value])Creates a new dictionary with keys from seq and values set to value (7)a.values()a copy of a's list of values(3)a.get(k[, x])a[k]if k k (4)a.setdefault(k[, x])a[k] if k in a, else x (also setting it)(5)a.pop(k[, x])a[k] if k in a , else x (and remove k)(8)a.popitem()remove and return an arbitrary (key, value) pair(6)a.iteritems()return an iterator over (key, value) pairs(2), ( 3)a.iterkeys()return an iterator over the mapping's keys(2), (3)a.itervalues()return an iterator over the mapping's values(2), (3) |
以上是Python基礎點介紹的詳細內容。更多資訊請關注PHP中文網其他相關文章!