Python Note

Page 36

˜ Python0 D.haskey(key) ^u äD ¼´Ă„škkey§Âƒ ukey in D.keys()" >>> a {’four’: 4, ’three’: 3, ’two’: 2, ’one’: 1} >>> print a.has_key("one") 1 >>> print a.has_key("abc") 0 D.items() ˜‡list§Ă™ÂĽ z‡ ƒ´Â˜Â‡šk߇ ƒ tuple(pair)§tupleÂĽc¥´key§ ¥´value >>> a {’four’: 4, ’three’: 3, ’two’: 2, ’one’: 1} >>> a.items() [(’four’, 4), (’three’, 3), (’two’, 2), (’one’, 1)] D.copy()ƒ udict(D.items()) § ´e– B §^copy()Â?ÂŻ" D.keys() list§škDÂĽ ¤kkey" >>> a {’four’: 4, ’three’: 3, ’two’: 2, ’one’: 1} >>> a.keys() [’four’, ’three’, ’two’, ’one’] a.copy()Â„ÂŒÂą ¤dict([(k,a[k]) for k in a.keys() ]) >>> dict([ (k,a[k]) for k in a.keys()]) {’four’: 4, ’one’: 1, ’three’: 3, ’two’: 2} ,§XJ==´Â? copy§Ă˜Â‡ĂšoZ§^a.copy()§Ă?Â?Â?ÂŻ" D.values() list§škDÂĽ ¤kvalue" >>> a {’four’: 4, ’three’: 3, ’two’: 2, ’one’: 1} >>> a.values() [4, 3, 2, 1] annCharles@tom.com

126Â?, 208Â?

Python ÆS)P


Issuu converts static files into: digital portfolios, online yearbooks, online catalogs, digital photo albums and more. Sign up and create your flipbook.