Array from dictionary keys in swift
Swift 3 & Swift 4 componentArray = Array(dict.keys) // for Dictionary componentArray = dict.allKeys // for NSDictionary
Swift 3 & Swift 4 componentArray = Array(dict.keys) // for Dictionary componentArray = dict.allKeys // for NSDictionary
Yes. Google Collections, or Guava as it is named now has something called MapMaker which can do exactly that. ConcurrentMap<Key, Graph> graphs = new MapMaker() .concurrencyLevel(4) .softKeys() .weakValues() .maximumSize(10000) .expiration(10, TimeUnit.MINUTES) .makeComputingMap( new Function<Key, Graph>() { public Graph apply(Key key) { return createExpensiveGraph(key); } }); Update: As of guava 10.0 (released September 28, 2011) many … Read more
Here you go: import java.lang.reflect.Type; import com.google.gson.reflect.TypeToken; Type type = new TypeToken<Map<String, String>>(){}.getType(); Map<String, String> myMap = gson.fromJson(“{‘k1′:’apple’,’k2′:’orange’}”, type);
While your solution should work, it can be difficult to read depending on the skill level of your fellow programmers. Additionally, it moves functionality away from the call site. Which can make maintenance a little more difficult. I’m not sure if your goal is to get the keys into a vector or print them to … Read more
Use df.to_dict(‘records’) — gives the output without having to transpose externally. In [2]: df.to_dict(‘records’) Out[2]: [{‘customer’: 1L, ‘item1’: ‘apple’, ‘item2’: ‘milk’, ‘item3’: ‘tomato’}, {‘customer’: 2L, ‘item1’: ‘water’, ‘item2’: ‘orange’, ‘item3’: ‘potato’}, {‘customer’: 3L, ‘item1’: ‘juice’, ‘item2’: ‘mango’, ‘item3’: ‘chips’}]
There is no such function; the easiest way to do this is to use a dict comprehension: my_dictionary = {k: f(v) for k, v in my_dictionary.items()} In python 2.7, use the .iteritems() method instead of .items() to save memory. The dict comprehension syntax wasn’t introduced until python 2.7. Note that there is no such method … Read more
You are looking for collections.defaultdict (available for Python 2.5+). This from collections import defaultdict my_dict = defaultdict(int) my_dict[key] += 1 will do what you want. For regular Python dicts, if there is no value for a given key, you will not get None when accessing the dict — a KeyError will be raised. So if … Read more
You don’t need any special code to do this, because it is what a dictionary already does. When you fetch dict[key] you know whether the dictionary contains the key, because the Optional that you get back is not nil (and it contains the value). So, if you just want to answer the question whether the … Read more
If you want to know how many values match in both the dictionaries, you should have said that 🙂 Maybe something like this: shared_items = {k: x[k] for k in x if k in y and x[k] == y[k]} print(len(shared_items))
>>> d = {‘1’: ‘one’, ‘3’: ‘three’, ‘2’: ‘two’, ‘5’: ‘five’, ‘4’: ‘four’} >>> ‘one’ in d.values() True Out of curiosity, some comparative timing: >>> T(lambda : ‘one’ in d.itervalues()).repeat() [0.28107285499572754, 0.29107213020324707, 0.27941107749938965] >>> T(lambda : ‘one’ in d.values()).repeat() [0.38303399085998535, 0.37257885932922363, 0.37096405029296875] >>> T(lambda : ‘one’ in d.viewvalues()).repeat() [0.32004380226135254, 0.31716084480285645, 0.3171098232269287] EDIT: And in case … Read more