设为首页 收藏本站
查看: 853|回复: 0

[经验分享] Google's Python Class 5 (Python Dict and File)

[复制链接]

尚未签到

发表于 2017-4-27 07:39:26 | 显示全部楼层 |阅读模式
  原文:http://code.google.com/edu/languages/google-python-class/dict-files.html

Python Dict and File

Google Code University › Programming Languages

Dict Hash Table

Python有一个高效的哈希表数据结构:"dict"(字典). Dict可以使用花括号包含一组key-value值(类似JSON), 如 dict = {key1:value1, key2:value2, ... }. {}可以表示一个空的字典.
使用如dict['foo']的结构形式可以访问字典中的数据。Strings, numbers, 和 tuples(元组) 可以用作key,所有类型都可以用作value. 使用上述类型以外的类型作为key有可能引发问题(这是因为上述类型都是不可变的,作为key比较合适).直接使用 dict['a']访问一个不存在的字典元素会抛出一个Error,所以访问之前需要使用in或者get(key)判断是否存在.

  ## Can build up a dict by starting with the the empty dict {}
 
## and storing key/value pairs into the dict like this:
 
## dict[key] = value-for-that-key
  dict
= {}
  dict
['a'] = 'alpha'
  dict
['g'] = 'gamma'
  dict
['o'] = 'omega'
 
print dict  ## {'a': 'alpha', 'o': 'omega', 'g': 'gamma'}
 
print dict['a']     ## Simple lookup, returns 'alpha'
  dict
['a'] = 6       ## Put new key/value into dict
 
'a' in dict         ## True
 
## print dict['z']                  ## Throws KeyError
 
if 'z' in dict: print dict['z']     ## Avoid KeyError
 
print dict.get('z')  ## None (instead of KeyError)

for循环默认会遍历字典的key. 这些key的顺序是不确定的. dict.keys() 和 dict.values()会返回键或值的序列. 字典有一个items()方法,可以把key-value转换成一个数组,这个数组中的每个元素都是两个元素形式的元组,这是遍历操作所有元素的最有效方法。

  ## By default, iterating over a dict iterates over its keys.
 
## Note that the keys are in a random order.
 
for key in dict: print key
 
## prints a g o
 
 
## Exactly the same as above
 
for key in dict.keys(): print key
 
## Get the .keys() list:
 
print dict.keys()  ## ['a', 'o', 'g']
 
## Likewise, there's a .values() list of values
 
print dict.values()  ## ['alpha', 'omega', 'gamma']
 
## Common case -- loop over the keys in sorted order,
 
## accessing each key/value
 
for key in sorted(dict.keys()):
   
print key, dict[key]
 
 
## .items() is the dict expressed as (key, value) tuples
 
print dict.items()  ##  [('a', 'alpha'), ('o', 'omega'), ('g', 'gamma')]
 
## This loop syntax accesses the whole dict by looping
 
## over the .items() tuple list, accessing one (key, value)
 
## pair on each iteration.
 
for k, v in dict.items(): print k, '>', v
 
## a > alpha    o > omega     g > gamma
有很多"iter"形式的变体函数,如iterkeys(), itervalues() 和 iteritems(),他们可以避免构造新的list -- 当数据量比较大的时候效果明显. 但我依然建议使用keys() 和 values() ,因为他们的名字比较有意义. 并且在Python3000以后性能有了较大改善,iterkeys不再需要了。
提示:从性能上考虑,字典是你应当使用的最有效的工具,在任何需要数据操作、传递的时候尽量使用.

Dict 格式化

% 可以方便的通过字典进行别名调用赋值:

  hash = {}
  hash
['word'] = 'garfield'
  hash
['count'] = 42
  s
= 'I want %(count)d copies of %(word)s' % hash  # %d for int, %s for string
 
# 'I want 42 copies of garfield'
Del

"del" 可以删除变量定义,同时也可以删除list或其他集合中的数据:

  var = 6
 
del var  # var no more!
 
  list
= ['a', 'b', 'c', 'd']
 
del list[0]     ## Delete first element
 
del list[-2:]   ## Delete last two elements
 
print list      ## ['b']
  dict
= {'a':1, 'b':2, 'c':3}
 
del dict['b']   ## Delete 'b' entry
 
print dict      ## {'a':1, 'c':3}
Files

open()函数会打开一个文件,按照正常的方法我们可以进行读写操作. f = open('name', 'r') 按照只读方式打开文件并把指针交给f,在结束操作后调用f.close()关闭文件。'r'、'w'、'a'分别表示读写和追加。'rU'被称作"Universal"模式,它可以自动在每一行结尾添加一个简单的 '\n'来进行换行,这样你就不需要手动追加了.for循环是最有效的遍历文件所有行的方法:

  # Echo the contents of a file
  f
= open('foo.txt', 'rU')
 
for line in f:   ## iterates over the lines of the file
   
print line,    ## trailing , so print does not add an end-of-line char
                   
## since 'line' already includes the end-of line.
  f
.close()
Reading one line at a time has the nice quality that not all the file needs to fit in memory at one time -- handy if you want to look at every line in a 10 gigabyte file without using 10 gigabytes of memory. The f.readlines() method reads the whole file into memory and returns its contents as a list of its lines. The f.read() method reads the whole file into a single string, which can be a handy way to deal with the text all at once, such as with regular expressions we'll see later.
For writing, f.write(string) method is the easiest way to write data to an open output file. Or you can use "print" with an open file, but the syntax is nasty: "print >> f, string". In python 3000, the print syntax will be fixed to be a regular function call with a file= optional argument: "print(string, file=f)".

Unicode文件

"codecs"模块提供访问unicode编码文件的功能:

import codecs
f
= codecs.open('foo.txt', 'rU', 'utf-8')
for line in f:
 
# 这里的line表示了一个unicode编码字符串
For writing, use f.write() since print does not fully support unicode.

持续改进编程

编写一个Python程序的时候,不要尝试一次性把所有东西写完. 试着把程序分成很多里程碑, 比如 "第一步是从这些词中提取list" ,这样逐步对程序进行完善并测试,这是python的编写后直接运行带给我们的优势,我们要尽情使用。

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-369675-1-1.html 上篇帖子: MySQLdb for Python使用指南/Python的数据库操作 下篇帖子: Live Lessons Python Fundaments | 2.2 GB
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表