本文介绍了更好的写在python中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是想知道,如果我能写一个更好的话。这个代码的方式


lMandatory = []

lOptional = []

for cls.dArguments中的arg:

如果arg为True:

lMandatory.append(arg)

else:

lOptional.append(arg)

返回(lMandatory,lOptional)


我认为有更好的方法,但我看不出......

I''m just wondering, if I could write a in a "better" way this code

lMandatory = []
lOptional = []
for arg in cls.dArguments:
if arg is True:
lMandatory.append(arg)
else:
lOptional.append(arg)
return (lMandatory, lOptional)

I think there is a better way, but I can''t see how...

推荐答案



删除前缀。 `l`是为了清单? `d`是为了什么!?不能是
字典,因为代码没有多大意义。


来自`cls`的地方在哪里?


Ciao,

Marc''BlackJack''Rintsch

Drop the prefixes. `l` is for list? `d` is for what!? Can''t be
dictionary because the code doesn''t make much sense.

Where is `cls` coming from?

Ciao,
Marc ''BlackJack'' Rintsch




导入运算符

返回过滤器(cls.dArguments),过滤器(operator.not_,cls.dArguments)


或者只是:


强制= [arg for arg in cls.dArguments in arg]

optional = [arg for arg in cls.dArguments in not arg]

返回强制性,可选


-

Paul Hankin

import operator
return filter(cls.dArguments), filter(operator.not_, cls.dArguments)

Or just:

mandatory = [arg for arg in cls.dArguments in arg]
optional = [arg for arg in cls.dArguments in not arg]
return mandatory, optional

--
Paul Hankin




导入运算符

返回过滤器(cls.dArguments),过滤器(operator.not_,cls.dArguments)


或者只是:


强制= [arg for arg in cls.dArguments in arg]

optional = [arg for arg in cls.dArguments in not arg]

返回强制性,可选


-

Paul Hankin

import operator
return filter(cls.dArguments), filter(operator.not_, cls.dArguments)

Or just:

mandatory = [arg for arg in cls.dArguments in arg]
optional = [arg for arg in cls.dArguments in not arg]
return mandatory, optional

--
Paul Hankin


这篇关于更好的写在python中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-30 06:31