从python字典中提取

从python字典中提取,python,dictionary,extract,Python,Dictionary,Extract,假设我的字典如下: class Airplane: def __init__(self, colour, Airplane_type): self.colour = colour self.Airplane_type = Airplane_type def is_Airplane(self): return self.Airplane_type=="Jet" def is_orange(self):

假设我的字典如下:

class Airplane:

    def __init__(self, colour, Airplane_type): 
        self.colour = colour
        self.Airplane_type = Airplane_type

    def is_Airplane(self):
        return self.Airplane_type=="Jet"

    def is_orange(self):
          return self.colour=="orange"


class Position:

    def __init__(self, horizontal, vertical):
        self.horizontal = int(horizontal)
        self.vertical = int(vertical)





from airplane import Airplane
from position import Position

Class test():

    def __init__(self):

    self.landings:{Position(1,0) : Airplane("Jet", "orange"),
                   Position(3,0) : Airplane("Boeing", "blue"),}

例如,如何提取所有橙色飞机并返回橙色飞机的数量。

此代码应提供您想要的结果:

result = []
keys = []
for key in self.landings:
    if self.landings[key].color == "orange":
        result.append(self.landings[key])
        keys.append(key)
#at the end of the loop, "result" has all the orange planes
#at the end of the loop, "keys" has all the keys of the orange planes
number=len(result) #len(result) gives the number of orange planes

请注意,
len
在许多情况下都适用于列表或字典中的x数。

此代码应提供您想要的结果:

result = []
keys = []
for key in self.landings:
    if self.landings[key].color == "orange":
        result.append(self.landings[key])
        keys.append(key)
#at the end of the loop, "result" has all the orange planes
#at the end of the loop, "keys" has all the keys of the orange planes
number=len(result) #len(result) gives the number of orange planes

请注意,
len
在许多情况下都适用于列表或字典中的x数。

一种优雅的方法是使用列表理解:

oranges = [plane for plane in self.landings.itervalues()
           if plane.is_orange()]

正如M.K.Hunter所说,您可以调用列表中的len()来获取号码。

一种优雅的方法是使用列表理解:

oranges = [plane for plane in self.landings.itervalues()
           if plane.is_orange()]

正如M.K.Hunter所说,你可以调用列表中的len()来获取号码。

如果你想找到橙色飞机的位置,你可能需要迭代字典中的
项,这样你既可以测试飞机的颜色,又可以同时看到位置:

orange_plane_positions = [pos for pos, plane in self.landings.values()
                              if plane.is_orange()]
如果您使用的是Python 2,并且在
self.landings
字典中有许多值,那么最好使用
iterms
而不是直接使用
items
(后者在Python 3中总是最好的)


请注意,如果您希望经常执行此查询,那么使用不同的字典组织可能是有意义的。不是按位置索引,而是按平面颜色索引,并将位置存储为
飞机
实例的属性。

如果要查找橙色飞机的位置,您可能希望迭代字典的
,以便既可以测试平面的颜色,又可以同时查看位置:

orange_plane_positions = [pos for pos, plane in self.landings.values()
                              if plane.is_orange()]
如果您使用的是Python 2,并且在
self.landings
字典中有许多值,那么最好使用
iterms
而不是直接使用
items
(后者在Python 3中总是最好的)



请注意,如果您希望经常执行此查询,那么使用不同的字典组织可能是有意义的。不是按位置索引,而是按平面颜色索引,并将位置存储为
飞机
实例的属性。

什么是
位置
函数还是字符串?很抱歉,我会将其添加到代码中它应该是
self.landings={...
还向我们展示
位置
飞机
将返回什么。如果您提供飞机的构造器,您将需要某种方法获得给定飞机实例的颜色。(我假设
plane1.color
将返回
“橙色”
“蓝色”
分别,例如。)
位置是一个函数还是一个字符串?对不起,我将把它添加到代码中应该是
self.landings={...
还向我们展示
位置
飞机
将返回什么。如果您提供飞机的构造器,您将需要某种方法获得给定飞机实例的颜色。(我假设
plane1.color
将返回
“橙色”
“蓝色”
分别,例如。)可能要执行
。颜色==“橙色”
而不是
=
。我如何在字典中检索橙色飞机的位置?我添加了两行新行来渲染与橙色飞机相对应的一组键。非常感谢您的帮助。欢迎您。看起来您试图接受我的答案和Arda Xi的答案。实际上,他的答案更好——更简单thonic。我投了赞成票。可能想做
。颜色==“橙色”
而不是
=
。我如何在字典中检索橙色飞机的位置?我添加了两行新行来渲染与橙色飞机相对应的一组键。非常感谢您的帮助。欢迎您。看起来您试图接受我的答案和Arda Xi的答案。实际上,他的答案更好——更简单thonic.我投了赞成票。与其索引
self.landings
两次,不如迭代
self.landings.values()
(或者
.itervalues()
,如果您使用的是Python2)。是的,您是对的。编辑以反映这一点(老实说,除非提到3,否则我假设Python2。)与其索引两次
self.landings
,不如迭代
self.landings.values()
(或者
.itervalues()
,如果您使用的是Python2)。是的,您是对的。编辑以反映这一点(老实说,除非提到3,否则我假设Python2。)