首页
注册
登录
V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请
登录
推荐学习书目
›
Learn Python the Hard Way
Python Sites
›
PyPI
- Python Package Index
›
http://diveintopython.org/toc/index.html
›
Pocoo
值得关注的项目
›
PyPy
›
Celery
›
Jinja2
›
Read the Docs
›
gevent
›
pyenv
›
virtualenv
›
Stackless Python
›
Beautiful Soup
›
结巴中文分词
›
Green Unicorn
›
Sentry
›
Shovel
›
Pyflakes
›
pytest
Python 编程
›
pep8 Checker
Styles
›
PEP 8
›
Google Python Style Guide
›
Code Style from The Hitchhiker's Guide
V2EX
›
Python
下面的代码有什么更好看的写法么?
ry_wang
·
saipanno
·
2015-01-12 16:46:01 +08:00
· 3181 次点击
这是一个创建于 3421 天前的主题,其中的信息可能已经有所发展或是发生改变。
在写代码的时候遇到下面的问题,截取代码的一段。
代码的最开始回初始化一个mapping字典,里面定义了一些初始值,但是下面给字典更新值的时候由于在一个列表中要用多次,所以必须得deepcopy一下,请问这块儿代码有什么更好的写法么?
我能想到的是再定义一个更新函数,把mapping更新的操作包起来。
谢谢
写法
代码
初始值
4 条回复
•
2015-01-12 18:59:08 +08:00
1
zhyu
2015-01-12 18:22:12 +08:00
1
https://gist.github.com/zhyu/360f78140a679317ac62
2
zhyu
2015-01-12 18:27:07 +08:00
个人意见,避免无意义的重复代码
keys 应该写在 config 里,便于扩展
3
ry_wang
OP
2015-01-12 18:56:58 +08:00
@
zhyu
enumerate解决了大问题,赞
另外 {key: v for key in keys} 这样真的可以么?py3?
4
zhyu
2015-01-12 18:59:08 +08:00
@
ry_wang
dict comprehension 是 python 2.7 加入的,3 也可以
关于
·
帮助文档
·
博客
·
API
·
FAQ
·
实用小工具
·
2380 人在线
最高记录 6679
·
Select Language
创意工作者们的社区
World is powered by solitude
VERSION: 3.9.8.5 · 25ms ·
UTC 12:14
·
PVG 20:14
·
LAX 05:14
·
JFK 08:14
Developed with
CodeLauncher
♥ Do have faith in what you're doing.