users = [ (lambda orgs, contact: { 'type': contact.type if contact else 1, 'uid': user.sid, 'name': user.realname, 'pinyin': json.dumps(pinyin_service.get_pinyin_searches_list(user.realname)), 'level': contact.priority if contact else 0, for o in filter(lambda org:org.role == 3, orgs) ]), 'department': ','.join([ o.name for o in filter(lambda org:org.role == 2, orgs) ]), "birthday": "", # TODO: birthday "avatar": "", # TODO: avatar 'details': json.loads(contact.contacts) if contact else [], 'groups': [ { 'id': o.id, 'type': 1, 'name': o.name, 'title': ' ', #TODO: } for o in filter(lambda org:org.role == 2, orgs) ] }) (organization_service.get_user_org_info(user.sid), contacts_service.get_by_uid(user.sid)) for user in user_service.get_users(key=g.key, type=2) ]users = []for user in user_service.get_users(key=g.key, type=2): orgs = organization_service.get_user_org_info(user.sid) # 用户所在分组单位(可以有多个) contact = contacts_service.get_by_uid(user.sid) # role # 1-单位 2-部门 3-职位 positions = filter(lambda org:org.role == 3, orgs) titles = [] for o in positions: titles.append('{}-{}'.format(org_name,o.name)) title = ','.join(titles) depts = filter(lambda org:org.role == 2,orgs) dept_names = ','.join([ dept.name for dept in depts]) groups = [{ 'id':o.id, 'type':1, 'name':o.name, 'title':' ', #TODO } for o in depts ] r = { 'type':contact.type if contact else 1, 'uid':user.sid, 'name':user.realname, 'pinyin':json.dumps(pinyin_service.get_pinyin_searches_list(user.realname)), 'level':contact.priority if contact else 0, 'title':title, 'department':dept_names, 'birthday':'' ,#TODO:birthday 'avatar':'', #TODO:AVATAR 'details':json.loads(contact.contacts) if contact else [], 'groups':groups } users.append(r)--
--
邮件来自: `CPyUG`华蟒用户组(中文Python技术邮件列表)
规则: http://code.google.com/p/cpyug/wiki/PythonCn
发言: pyth...@googlegroups.com
详情: http://code.google.com/p/cpyug/wiki/CpyUg
G+: https://plus.google.com/u/0/communities/108786798869709602787
严正: 理解列表! 智慧提问! http://wiki.woodpecker.org.cn/moin/AskForHelp
---
您收到此邮件是因为您订阅了Google网上论坛中的“python-cn(华蟒用户组,CPyUG 邮件列表)”论坛。
要退订此论坛并停止接收此论坛的电子邮件,请发送电子邮件到python-cn+...@googlegroups.com。
要在网络上查看此讨论,请访问https://groups.google.com/d/msgid/python-cn/6c1f938c-85d1-4878-890c-dff71d2ac919%40googlegroups.com。
要查看更多选项,请访问https://groups.google.com/d/optout。
大家觉得那一种易读,易维护一些。我当然觉得我是的好一些,我是从C学起的,Python虽然接触好几年了,但是尚未真正入得门道。C式风格流程式的写法,从上到下有组织的写好,我比较容易理解。列表推导太长了,真的很让人难受,中间又加了那么些的表达式。而且开头的,通过两个循环来得出 单位-职称 这一做法让人很觉得 奇怪。怎么这个职位也变成一个orginazation来对待了,看来可能是设计有点不规范。再者:代码还可以怎么样修改,还请一同指教。谢谢
--
--
邮件来自: `CPyUG`华蟒用户组(中文Python技术邮件列表)
规则: http://code.google.com/p/cpyug/wiki/PythonCn
发言: pyth...@googlegroups.com
详情: http://code.google.com/p/cpyug/wiki/CpyUg
G+: https://plus.google.com/u/0/communities/108786798869709602787
严正: 理解列表! 智慧提问! http://wiki.woodpecker.org.cn/moin/AskForHelp
---
您收到此邮件是因为您订阅了Google网上论坛中的“python-cn(华蟒用户组,CPyUG 邮件列表)”论坛。
要退订此论坛并停止接收此论坛的电子邮件,请发送电子邮件到python-cn+...@googlegroups.com。
要在网络上查看此讨论,请访问https://groups.google.com/d/msgid/python-cn/6c1f938c-85d1-4878-890c-dff71d2ac919%40googlegroups.com。
要查看更多选项,请访问https://groups.google.com/d/optout。
if '离职' in dept_names or '退休' in dept_names: continue--
--
邮件来自: `CPyUG`华蟒用户组(中文Python技术邮件列表)
规则: http://code.google.com/p/cpyug/wiki/PythonCn
发言: pyth...@googlegroups.com
详情: http://code.google.com/p/cpyug/wiki/CpyUg
G+: https://plus.google.com/u/0/communities/108786798869709602787
严正: 理解列表! 智慧提问! http://wiki.woodpecker.org.cn/moin/AskForHelp
---
您收到此邮件是因为您订阅了Google网上论坛中的“python-cn(华蟒用户组,CPyUG 邮件列表)”论坛。
要退订此论坛并停止接收此论坛的电子邮件,请发送电子邮件到python-cn+...@googlegroups.com。
要在网络上查看此讨论,请访问https://groups.google.com/d/msgid/python-cn/aba54af1-c338-4adf-accc-5e031a40de16%40googlegroups.com。
要查看更多选项,请访问https://groups.google.com/d/optout。
def _get_org_ids_by_uid(uid): return [ item.organization_id for item in db.query(OrganizationMember).filter(OrganizationMember.uid == uid).all()]
def get_user_org_info(uid): #return db.query(OrganizationMember).filter(OrganizationMember.uid == uid).first() ids = _get_org_ids_by_uid(uid) if ids: return db.query(Organization).filter(Organization.id.in_(ids)).all() else: return [] total_orgs = organization_service.get_all_orgs_by_key(g.key) total_org_members = organization_service.get_all_organization_members()
def get_org_ids_by_uid(uid): return [ item.organization_id for item in total_org_members if item.uid == uid ]
def get_user_org_info(uid): ids = get_org_ids_by_uid(uid) if ids: return [org for org in total_orgs if org.id in ids] else: return []要在网络上查看此讨论,请访问https://groups.google.com/d/msgid/python-cn/53BD1908.8030704%40gmail.com。
要查看更多选项,请访问https://groups.google.com/d/optout。
您收到此邮件是因为您订阅了 Google 网上论坛的“python-cn(华蟒用户组,CPyUG 邮件列表)”论坛。
要退订此论坛并停止接收此论坛的电子邮件,请发送电子邮件到python-cn+...@googlegroups.com。
要在网络上查看此讨论,请访问 https://groups.google.com/d/msgid/python-cn/CAFL5w3UtaUXqwDyvkTqhR5R1rohA24hCGxMRAuP1nXM_JG-DGw%40mail.gmail.com。
要查看更多选项,请访问 https://groups.google.com/d/optout。
--
--
邮件来自: `CPyUG`华蟒用户组(中文Python技术邮件列表)
规则: http://code.google.com/p/cpyug/wiki/PythonCn
发言: pyth...@googlegroups.com
详情: http://code.google.com/p/cpyug/wiki/CpyUg
G+: https://plus.google.com/u/0/communities/108786798869709602787
严正: 理解列表! 智慧提问! http://wiki.woodpecker.org.cn/moin/AskForHelp
---
您收到此邮件是因为您订阅了Google网上论坛中的“python-cn(华蟒用户组,CPyUG 邮件列表)”论坛。
要退订此论坛并停止接收此论坛的电子邮件,请发送电子邮件到python-cn+...@googlegroups.com。
要退订此论坛并停止接收此论坛的电子邮件,请发送电子邮件到python-cn+unsubscribe@googlegroups.com。
>> 要退订此论坛并停止接收此论坛的电子邮件,请发送电子邮件到python-cn+unsubscribe@googlegroups.com。
>>
>> 要在网络上查看此讨论,请访问https://groups.google.com/d/msgid/python-cn/aba54af1-c338-4adf-accc-5e031a40de16%40googlegroups.com。
>> 要查看更多选项,请访问https://groups.google.com/d/optout。
>>
>>
>> --
>> --
>> 邮件来自: `CPyUG`华蟒用户组(中文Python技术邮件列表)
>> 规则: http://code.google.com/p/cpyug/wiki/PythonCn
>> 发言: pyth...@googlegroups.com
>> 详情: http://code.google.com/p/cpyug/wiki/CpyUg
>> G+: https://plus.google.com/u/0/communities/108786798869709602787
>> 严正: 理解列表! 智慧提问! http://wiki.woodpecker.org.cn/moin/AskForHelp
>> ---
>> 您收到此邮件是因为您订阅了Google网上论坛中的“python-cn(华蟒用户组,CPyUG 邮件列表)”论坛。
>> 要退订此论坛并停止接收此论坛的电子邮件,请发送电子邮件到python-cn+unsubscribe@googlegroups.com。
>>
>> 要在网络上查看此讨论,请访问https://groups.google.com/d/msgid/python-cn/53BD1908.8030704%40gmail.com。
>> 要查看更多选项,请访问https://groups.google.com/d/optout。
>
>
>
>
> --
> My GitHub
> https://github.com/yueyoum
>
> --
> --
> 邮件来自: `CPyUG`华蟒用户组(中文Python技术邮件列表)
> 规则: http://code.google.com/p/cpyug/wiki/PythonCn
> 发言: pyth...@googlegroups.com
> 详情: http://code.google.com/p/cpyug/wiki/CpyUg
> G+: https://plus.google.com/u/0/communities/108786798869709602787
> 严正: 理解列表! 智慧提问! http://wiki.woodpecker.org.cn/moin/AskForHelp
> ---
> 您收到此邮件是因为您订阅了Google网上论坛中的“python-cn(华蟒用户组,CPyUG 邮件列表)”论坛。
> 要退订此论坛并停止接收此论坛的电子邮件,请发送电子邮件到python-cn+unsubscribe@googlegroups.com。
> 要在网络上查看此讨论,请访问https://groups.google.com/d/msgid/python-cn/CALZWPGiieOz_WqewOmxTMesRcWG1Th17qvn%3D%3Dg4VXz%3DFQK%3DXdw%40mail.gmail.com。
> 要查看更多选项,请访问https://groups.google.com/d/optout。
>
>
> --
> --
> 邮件来自: `CPyUG`华蟒用户组(中文Python技术邮件列表)
> 规则: http://code.google.com/p/cpyug/wiki/PythonCn
> 发言: pyth...@googlegroups.com
> 详情: http://code.google.com/p/cpyug/wiki/CpyUg
> G+: https://plus.google.com/u/0/communities/108786798869709602787
> 严正: 理解列表! 智慧提问! http://wiki.woodpecker.org.cn/moin/AskForHelp
> ---
> 您收到此邮件是因为您订阅了Google网上论坛中的“python-cn(华蟒用户组,CPyUG 邮件列表)”论坛。
> 要退订此论坛并停止接收此论坛的电子邮件,请发送电子邮件到python-cn+unsubscribe@googlegroups.com。
你说的对,应该只取需要的值。SQL Alchemy 也有这样的用法,谢谢提醒。一时之间竟忘了,这样优化。
要在网络上查看此讨论,请访问https://groups.google.com/d/msgid/python-cn/CACTx4_-D%3DZQviiv_6sqDmLPMbsFpwu597Bf6MyJVttPmS1%2BU8Q%40mail.gmail.com。
要查看更多选项,请访问https://groups.google.com/d/optout。
来个恶心人的。
lambda f :(dict(((k, v[1:]) for k, v in m.iteritems() if f(v[0]))) if hasattr(m, '__getitem__') else ([v[1:] for v in m if f(v[0])] if hasattr(m, '__iter__') else v[1:]))(lambda x: ord(x) < 60)
手机写程序累死了,帮我看看有没有错。。。
from nexus 4
来个恶心人的。
lambda f :(dict(((k, v[1:]) for k, v in m.iteritems() if f(v[0]))) if hasattr(m, '__getitem__') else ([v[1:] for v in m if f(v[0])] if hasattr(m, '__iter__') else v[1:]))(lambda x: ord(x) < 60)手机写程序累死了,帮我看看有没有错。。。
from nexus 4
On Jul 13, 2014 2:20 AM, "lee Alexander" <superp...@gmail.com> wrote:
[i for i in x if (True if i > a else (False if i < b else i % c))] 这样的不似人话的例子主要是一些一行代码怎么怎么样的脑洞题搞出来,变量定义过于随意造成的,替换成for也会头痛。且列表解析格式化一下就容易看明白了
[ifor i in xif (True if i > a else (False if i < b else i % c))]
分行出来一看复杂的还是这个if的条件,那么放在for里嵌套if 一样的难懂
我觉得楼主的命令式编程的思维过于根深蒂固才会觉得列表推导难以理 解吧
在 2014年7月10日 上午10:01,Shell Xu <shell...@gmail.com>写 道:
类似代码总共有三种写法:1. 列表推导2. for循环3. 函数映射三种写法还可以交叉,组合起来会非常复杂。
一般建议如下:
如果过程是严密的函数映射过程,函数映射总是最好懂的。因为函数映射组合中,每个函数的作用都是固定的。 map调用的函数只能映射,filter的只能过滤,reduce的只能——呃,reduce。所以即使多个函 数对对象反复操作,每个部分也很容易理解。但是同样的,函数映射也是最死板的。同样的中间变量,在map中求值 过了,reduce中还得另行求值——除非你使用对象传递。
而for循环是最不好懂的,因为for循环的边界效用太高。你可以在for循环里面反复使用上一轮迭代的 中间值进行累加递推,还可以使用直接取元素的方法操纵非生成器的对象,获得前趋和后置元素。甚至可以利用这种效 应直接做算法。就是因为for的灵活性,所以如果在代码里面看到一个for,而这个for不能显然的变形为函数 映射(变量传递)。那么我第一反应都会比较头痛。
列表推导相比起来,近乎介于两者之间。既不像函数那样严密,又不像for那么灵活。一般来说,用来写短小句子非 常合适。用在长句子上总有点这种感觉:
您收到此邮件是因为您订阅了Google网上论坛中的“python-cn(华蟒用户 组,CPyUG 邮件列表)”论坛。
要在网络上查看此讨论,请访问https://groups.google.com/d/msgid/python-cn/53C3A34D.3060904%40gmail.com。
要查看更多选项,请访问https://groups.google.com/d/optout。
--
--
邮件来自: `CPyUG`华蟒用户组(中文Python技术邮件列表)
规则: http://code.google.com/p/cpyug/wiki/PythonCn
发言: pyth...@googlegroups.com
详情: http://code.google.com/p/cpyug/wiki/CpyUg
G+: https://plus.google.com/u/0/communities/108786798869709602787
严正: 理解列表! 智慧提问! http://wiki.woodpecker.org.cn/moin/AskForHelp
---
您收到此邮件是因为您订阅了 Google 网上论坛的“python-cn(华蟒用户组,CPyUG 邮件列表)”论坛。
要退订此论坛并停止接收此论坛的电子邮件,请发送电子邮件到python-cn+...@googlegroups.com。
要在网络上查看此讨论,请访问 https://groups.google.com/d/msgid/python-cn/CABK5mSb0mdMhWJ4yJRD%3DOXo71tDYOiLi%2BfOiMimE4Q3atpk0BQ%40mail.gmail.com。
要查看更多选项,请访问 https://groups.google.com/d/optout。
这段代码其实逻辑很简单。过滤dict/list类容器的值,选出第一个字符序数小于60的,并把第一个字符切掉。写成dict(...)是因为对老版本的兼容性。__getitem__是必须的,因为没有__getitem__,就不能当dict容器了。即使有iteritems也没用。这种对象我宁可直接在v[1:]上报错。如果有了__getitem__却没有iteritems,这种对象我也没法下嘴啊。总不能iter完了再__getitem__吧。这点没做到我认为是dict类对象实现的错。当然,稳妥起见一般配的都是items,这个更加合理。这个语句的恶心之处绝对不是排版,而是出了错没法调。当一句错误提示提到这句的时候,鬼才知道问题在哪个子句。而且连调试语句都没地方插。。。BTW:这个其实是随手写报复社会,不过代码是有原型的。