1

I have a dictionary like this

my_d = {"a": [1, 2, 2, 5, 2],"b": [2, 1, 2, 4, 5],"c": [7, 2, 2, 6, 2],"d": [7, 2, 2, 2, 1]}

I am looking for the keys whose dictionary values contain "2 and less than 2" more than twice. In the example, that would be "a","b", "c","d". the following code only find those whose dictionary values contain 2 more than twice:

for key, item in my_d.items():
    if item.count(2) > 2:
        print key,
Iguananaut
  • 21,810
  • 5
  • 50
  • 63
ali
  • 9
  • 4
  • I don't think this has a great deal to do with the fact that the lists are in a dict, and is more just a question about some particular counting of items in a list. In other words, the part where dict keys are returned for associated values that meet some criterion is trivial. – Iguananaut Dec 19 '14 at 16:22
  • The description is a little ambiguous. Would you want `[ 1, 2, 3, 4]` to match the criteria? It has two numbers <= 2, but neither of them occur >= 2 times. – mojo Dec 19 '14 at 16:25

6 Answers6

1

You could sort the items and check if the first two elements are ≤2:

>>> for key, item in my_d.items():
...   i=sorted(item)
...   if all(map(lambda x:x<=2,i[:2])):
...     print key,
...
a c b d

or, since you only have 2 elements to check:

>>> for key, item in my_d.items():
...   i=sorted(item)
...   if i[0]<=i[1]<=2:
...     print key,
...
a c b d
fredtantini
  • 15,966
  • 8
  • 49
  • 55
1
[key for key, item in my_d.items() if len([i for i in item if i<3]) > 2]
IgorBosko
  • 11
  • 2
0

The following should work, it's a simple generator expression

for key, item in my_d.items():
    if len ([x for x in item if x <= 2]) > 2:
        print key

What it does is building a list of all elements in item that are less or equal to 2, and checks the length of the generated list.

Dunno
  • 3,632
  • 3
  • 28
  • 43
  • Dunno, the code works perfectly for the dictionary in the question. but I have a dataset with around 32000 rows and 52 columns. I already exported that into a dictionary. when I test out your code for such a big dictionary, it gave this error: ValueError: too many values to unpack. – ali Dec 19 '14 at 16:23
  • @ali that error may be caused by something else: http://stackoverflow.com/questions/5466618/too-many-values-to-unpack-iterating-over-a-dict-key-string-value-list – Dunno Dec 19 '14 at 16:29
  • Dunno. I think I just discovered the problem. when I make the dictionary the values would be string not integer like this: my_d = {"a": ['1', '2', '2', '5', '2'],"b": ['2', '1', '2', '4', '5'],"c": ['7', '2', '2', '6', '2'],"d": ['7', '2', '2', '2', '1'],"e":['1','3','4','2','6']}. I used int() function but did not work – ali Dec 19 '14 at 17:01
  • Comparing a string to integer doesn't cause `ValueError` – Dunno Dec 19 '14 at 17:18
0
print [k for k, v in my_d.items() if len(filter(lambda x: x <= 2, v)) > 2] # ['a', 'c', 'b', 'd']
markcial
  • 9,041
  • 4
  • 31
  • 41
0

You could do something like this -

f1 = lambda x: [obj for obj in x if obj <= 2]

results = [k for k, v in d1.items() if len(f1(v)) >= 2]

Which runs the lambda function on every value

thomas
  • 1,133
  • 1
  • 12
  • 31
0
>>> my_d = {"a": [1, 2, 2, 5, 2],"b": [2, 1, 2, 4, 5],"c": [7, 2, 2, 6, 2],"d": [7, 2, 2, 2, 1],"e":[1,3,4,2,6]}
>>> [k for k,v in my_d.items() if len([i for i in v if i<3])>2]
['a', 'c', 'b', 'd']
Irshad Bhat
  • 8,479
  • 1
  • 26
  • 36