English 中文(简体)
Python: 意外符号
原标题:Python: unexpected token
min(gameinfo.not_my_planets.values(), key=lambda p: p if p.num_ships < 35)

试图获得最小的行星 但只有在有35艘或少于35艘飞船的行星存在时

虽然有什么想法吗?

最佳回答

您错过了 ambda 表达式中的 else 部分, 但最好重写为 :

min(filter(lambda p: p.num_ships < 35, gameinfo.not_my_planets.values()))

filer(...) will reduce the sequence of planets to those having num_ships < 35. Are planet objects comparable among them selves or should you compare to an attribute planet, planet.size? If so, you have to add another lambda:

min(filter(lambda p: p.num_ships < 35, gameinfo.not_my_planets.values()), key=lambda p:p.size)

如果您是“强”不用于功能构筑

min( p for p in gameinfo.not_my_planets.values() if p.num_ships < 35)
问题回答

我不知道那羊羔的意义何在...

“选择拥有35艘或35艘以上船舶的行星”:

planets_35_or_higher = [p for p in gameinfo.not_my_planets.values() 
                        if p.num_ships >= 35])

选择其他的最小值 :

planets_below_35 = [p for p in gameinfo.not_my_planets.values() 
                    if p.num_ships < 35])
min_planet_below_35 = min(planets_below_35)

或者,如果行星天体没有比较运算符,请给 min 设定一个函数来获取它应该用来比较的属性,例如:

min_planet_below_35 = min(planets_below_35, key=lambda p: p.num_ships)

此外,为了处理35艘以下船舶没有行星的可能性:

if planets_below_35:
    min_planet_below_35 = min(planets_below_35)
else:
    # do something else

如果 p. num_ships< 35 本身是无效的语法, 则 p. num_ships & gt; = 35 ? 您需要用 else 来填写它吗? 例如, p is p. num_ships & lt; 35 else. 中用于 keys 参数的点是 < code > min 的点。 选择确定最小元素的每个元素的值应该是什么 。

你说"只要有35艘船或少于35艘船的行星"是什么意思?





相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...

热门标签