You can use a dict comprehension:
{k: v for k, v in points.items() if v[0] < 5 and v[1] < 5}
And in Python 2, starting from 2.7:
{k: v for k, v in points.iteritems() if v[0] < 5 and v[1] < 5}
Answer from Thomas on Stack OverflowYou can use a dict comprehension:
{k: v for k, v in points.items() if v[0] < 5 and v[1] < 5}
And in Python 2, starting from 2.7:
{k: v for k, v in points.iteritems() if v[0] < 5 and v[1] < 5}
dict((k, v) for k, v in points.items() if all(x < 5 for x in v))
You could choose to call .iteritems() instead of .items() if you're in Python 2 and points may have a lot of entries.
all(x < 5 for x in v) may be overkill if you know for sure each point will always be 2D only (in that case you might express the same constraint with an and) but it will work fine;-).
filter items in a python dictionary where keys contain a specific string - Stack Overflow
Filtering a dictionary value from a list of dictionary using lambda and filter in python - Stack Overflow
Filtering a nested (default) dict based on values
How to filter dictionary by value?
Videos
How about a dict comprehension:
filtered_dict = {k: v for k, v in d.iteritems() if filter_string in k}
One you see it, it should be self-explanatory, as it reads like English pretty well.
This syntax requires Python 2.7 or greater.
In Python 3, there is only dict.items(), not iteritems() so you would use:
filtered_dict = {k: v for k, v in d.items() if filter_string in k}
You can use the built-in filter function to filter dictionaries, lists, etc. based on specific conditions.
filtered_dict = dict(
filter(lambda item: filter_str in item[0], d.items())
)
The advantage is that you can use it for different data structures.
Seeing your initial code, this should do the same thing
>>> l = [{'abc': 'def'}, {'ghi': 'jul'}, {'Name': 'my-name'}]
>>> [i["Name"] for i in l if "Name" in i][0]
'my-name'
Your code returns only the first occurence.Using my approach, and probably also if you would use filter, you'll get (first) a list of all occurrences than only get the first (if any). This would make it less "efficient" IMHO, so I would probably change your code to be something more like this:
def get_json_val(l, key):
for item in l:
if not key in item: continue
return item[key]
Why are you iterating over your dict? That defeats the purpose. Just do
[d for d in l if key in d]
Or if you feel some compulsion to use lambda and filter:
filter(lambda d: key in d, l)
Note, these return lists instead of the corresponding value. If you want the value, you'll have to index into the list. Simply out, using a loop is probably the most reasonable approach, just don't iterate over your dict:
def f(key, l):
for d in l:
if key in d:
return d[key]
Hello.
I've gotten some help from redditors on a very similar problem before, but I've modified the problem slightly.
I have a nested defaultdict, the structure of which is {'source_word_string':{'target_word_string':frequency_int}}. When (pretty) printed, it looks like this:
defaultdict(<function <lambda> at 0x10948ee50>,
{'around': defaultdict(<class 'int'>, {'autour-de': 1}),
'from': defaultdict(<class 'int'>, {'en': 1}),
'in-front-of': defaultdict(<class 'int'>, {'devant': 1}),
'on': defaultdict(<class 'int'>, {'sur': 2, 'au-bord-d’': 1}),
'to': defaultdict(<class 'int'>, {'en': 1, 'à':3}),
'under': defaultdict(<class 'int'>, {'sous': 1})})I would like to delete all source-target word pairs whose frequency count is less than 2.
I would thus expect as output:
defaultdict(<function <lambda> at 0x108c57e50>,
{'on': defaultdict(<class 'int'>, {'sur': 2}),
{'to': defaultdict(<class 'int'>, {'à': 3})}) I think I need to do two nested loops (over source words and over target words) and then write to a new filtered_dict, but I'm not sure what that looks like in practice.
Any help would be greatly appreciated. Thank you in advance.