我有一个dict有一大堆条目.我只对他们中的一小部分感兴趣.是否有一种简单的方法可以修剪所有其他的?
我正在从CSV导入并大致以格式获取数据
{ 'Field1' : 3000, 'Field2' : 6000, 'RandomField' : 5000 }
Run Code Online (Sandbox Code Playgroud)
字段的名称是动态的.(好吧,他们很有活力,因为可能会有超过Field1和Field2,但我知道Field1并且Field2总是会在那里.
我希望能够将这个字典传入我的班级,allMyFields以便我可以将上述数据作为属性访问.
class allMyFields:
# I think I need to include these to allow hinting in Komodo. I think.
self.Field1 = None
self.Field2 = None
def __init__(self,dictionary):
for k,v in dictionary.items():
self.k = v
#of course, this doesn't work. I've ended up doing this instead
#self.data[k] = v
#but it's not the way I want to access the data.
q = { …Run Code Online (Sandbox Code Playgroud)