developer

Get key by value in dictionary

Question: ❓❓❓ I made a function which will look up ages in dictionary and show the matching name: list = {'george':16,'amber':19} search_age = raw_input("Provide age") for age in list.values(): if age == search_age: name = list[age] print name I know how to compare and find the age I just don’t Read more…

By admin, ago
developer

How to define hash tables in Bash?

Question: ❓❓❓ What is the equivalent of Python dictionaries but in Bash (should work across OS X and Linux). Good Answer: 👌👌👌 Bash 4 Bash 4 natively supports this feature. Make sure your script’s hashbang is #!/usr/bin/env bash or #!/bin/bash or anything else that references bash and not sh. Make Read more…

By admin, ago
developer

Merging dictionaries in C#

Question: ❓❓❓ What’s the best way to merge 2 or more dictionaries (Dictionary<T1,T2>) in C#? (3.0 features like LINQ are fine). I’m thinking of a method signature along the lines of: public static Dictionary<TKey,TValue> Merge<TKey,TValue>(Dictionary<TKey,TValue>[] dictionaries); or public static Dictionary<TKey,TValue> Merge<TKey,TValue>(IEnumerable<Dictionary<TKey,TValue>> dictionaries); EDIT: Got a cool solution from JaredPar and Read more…

By admin, ago
developer

Convert Python dict to object?

Question: ❓❓❓ I’m searching for an elegant way to convert a normal Python dict with some nested dicts to an object. For example: >>> d = {'a': 1, 'b': {'c': 2}, 'd': ["hi", {'foo': "bar"}]} Should be accessible in this way: >>> x = dict2obj(d) >>> x.a 1 >>> x.b.c Read more…

By admin, ago