2

I have a JSON object that looks like this:

[{'key1': 'yes', 'key2': 'yes', 'key3': 'yes'},
 {'key1': 'yes', 'key2': 'no', 'key3': 'yes'},
 {'key1': 'yes', 'key2': 'null', 'key3': 'yes'}]

I am trying to count the occurrences of Key1 when the value of Key2 is not yes and I can use that to rank Key1 by the appearance of the value yes in Key2.

1
  • Hi @alecxe I edited the question slightly. I must be really low on Caffeine. What I intend to do is find us my initial count of Key1 which I have as Yes : 409, No: 334, N/A:40 etc and rank its values depending on the appearance of yes in Key2. So my output i something like Yes:60%, No:30%, N/A: 30% Commented Jan 30, 2016 at 2:04

2 Answers 2

2

You can use sum():

>>> l = [
    {"key1": "yes", "key2": "yes", "key3": "yes"}, 
    {"key1": "yes", "key2": "no", "key3": "yes"}, 
    {"key1": "yes", "key2": "null", "key3": "yes"}
]
>>> sum(item["key2"] != "yes" for item in l)
2
Sign up to request clarification or add additional context in comments.

Comments

0

I have another answer, it's implemented by 'filter' and 'len':

l = [
    {"key1": "yes", "key2": "yes", "key3": "yes"}, 
    {"key1": "yes", "key2": "no", "key3": "yes"}, 
    {"key1": "yes", "key2": "null", "key3": "yes"}
]

f = filter(lambda x: x['key2']!='yes', l)
print f
print len(f)

The print f outputs the filter result. The print len(f) is to further output the number of the filter result.

[{'key3': 'yes', 'key2': 'no', 'key1': 'yes'}, {'key3': 'yes', 'key2': 'null', 'key1': 'yes'}]
2

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.