英文:
Python dictionary filter only returning first valid occurrence
问题
I'm implementing filter functionality for a dictionary.
class Filter:
def __init__(self, obj):
self.obj = obj
def isin(self, key, lst):
self.obj = {k:[v[i] for i in range(len(self.obj)+1) if self.obj[key][i] in lst]
for k,v in self.obj.items()}
def gt_x(self,key,value):
self.obj = {k:[v[i] for i in range(len(self.obj)+1) if self.obj[key][i] > value]
for k,v in self.obj.items()}
The interesting thing I've noted is that it only returns the first occurrence which meets filter requirements.
d = {'a':[1,1,2,2,3,3],
'b':[3,3,2,2,1,1,]}
f = Filter(d)
f.isin(key='a',lst=[2,3])
f.obj
# desired: {'a': [2,2,3,3], 'b': [2,2,1,1]}
And
d = {'a':[1,1,2,2,3,3],
'b':[3,3,2,2,1,1,]}
f = Filter(d)
f.gt_x('a', value=1)
f.obj
# desired: {'a': [2,2,3,3], 'b': [2,2,1,1]}
I know that in practicality I could do this functionality in Pandas or other data libraries. But this is more of a curiosity now than anything else. Why are these class methods only returning the first valid occurrence?
英文:
I'm implementing filter functionality for a dictionary.
class Filter:
def __init__(self, obj):
self.obj = obj
def isin(self, key, lst):
self.obj = {k:[v[i] for i in range(len(self.obj)+1) if self.obj[key][i] in lst]
for k,v in self.obj.items()}
def gt_x(self,key,value):
self.obj = {k:[v[i] for i in range(len(self.obj)+1) if self.obj[key][i] > value]
for k,v in self.obj.items()}
The interesting thing I've noted is that it only returns the first occurrence which meets filter requirements.
d = {'a':[1,1,2,2,3,3],
'b':[3,3,2,2,1,1,]}
f = Filter(d)
f.isin(key='a',lst=[2,3])
f.obj
>>> {'a': [2], 'b': [2]} # desired: {'a': [2,2,3,3], 'b': [2,2,1,1]}
And
d = {'a':[1,1,2,2,3,3],
'b':[3,3,2,2,1,1,]}
f = Filter(d)
f.gt_x('a', value=1)
f.obj
>>> {'a': [2], 'b': [2]} # desired: {'a': [2,2,3,3], 'b': [2,2,1,1]}
I know that in practicality I could do this functionality in Pandas or other data libraries. But this is more of a curiosity now than anything else. Why are these class methods only returning the first valid occurrence?
答案1
得分: 2
你必须将range(len(self.obj)+1)
替换为range(len(self.obj[k]))
,因为第一个会得到len(keys) + 1
,在你的示例中是3,我认为期望的行为应该是数组本身的长度。
class Filter:
def __init__(self, obj):
self.obj = obj
def isin(self, key, lst):
self.obj = {k: [v[i] for i in range(len(self.obj[k])) if self.obj[key][i] in lst] for k, v in self.obj.items()}
def gt_x(self, key, value):
self.obj = {k: [v[i] for i in range(len(self.obj[k])) if self.obj[key][i] > value] for k, v in self.obj.items()}
d = {
"a": [1, 1, 2, 2, 3, 3],
"b": [3, 3, 2, 2, 1, 1],
}
f = Filter(d)
f.isin(key="a", lst=[2, 3])
print(f.obj) # {'a': [2, 2, 3, 3], 'b': [2, 2, 1, 1]}
d = {
"a": [1, 1, 2, 2, 3, 3],
"b": [3, 3, 2, 2, 1, 1],
}
f = Filter(d)
f.gt_x("a", value=1)
print(f.obj) # {'a': [2, 2, 3, 3], 'b': [2, 2, 1, 1]}
英文:
You have to replace range(len(self.obj)+1)
to range(len(self.obj[k]))
as 1st one gives you len(keys) + 1
which is 3 in your example, I think desired behaviour was len of array itself.
class Filter:
def __init__(self, obj):
self.obj = obj
def isin(self, key, lst):
self.obj = {k: [v[i] for i in range(len(self.obj[k])) if self.obj[key][i] in lst] for k, v in self.obj.items()}
def gt_x(self, key, value):
self.obj = {k: [v[i] for i in range(len(self.obj[k])) if self.obj[key][i] > value] for k, v in self.obj.items()}
d = {
"a": [1, 1, 2, 2, 3, 3],
"b": [3, 3, 2, 2, 1, 1],
}
f = Filter(d)
f.isin(key="a", lst=[2, 3])
print(f.obj) # {'a': [2, 2, 3, 3], 'b': [2, 2, 1, 1]}
d = {
"a": [1, 1, 2, 2, 3, 3],
"b": [3, 3, 2, 2, 1, 1],
}
f = Filter(d)
f.gt_x("a", value=1)
print(f.obj) # {'a': [2, 2, 3, 3], 'b': [2, 2, 1, 1]}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论