Python字典筛选仅返回第一个有效出现。

huangapple go评论92阅读模式
英文:

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]}

huangapple
  • 本文由 发表于 2023年5月8日 00:42:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/76195132.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定