[Solved] Python dictionary printing specific value [closed]

In this simple case, you can create a dictionary with the keys being the roles themselves: > roles = {d[‘role’]:d for d in dic} > roles[‘NOC’][’email’] Note that you are still looping through all dictionary items to create the new one. 0 solved Python dictionary printing specific value [closed]

[Solved] How to merge two dictionaries with same keys and keep values that exist in the same key [closed]

def intersect(a, b): a, b = set(a), set(b) return list(a & b) def merge_dicts(d1, d2): return {k: intersect(v1, v2) for (k, v1), v2 in zip(d1.items(), d2.values())} dictionary_1 = {‘A’ :[‘B’,’C’,’D’],’B’ :[‘A’]} dictionary_2 = {‘A’ :[‘A’,’B’], ‘B’ :[‘A’,’F’]} merge = merge_dicts(dictionary_1, dictionary_2) print(merge) # {‘A’: [‘B’], ‘B’: [‘A’]} 4 solved How to merge two dictionaries with … Read more

[Solved] A dictionary that returns number of word and each word’s lenght Python

sample = “i am feeling good” output = {} output[“word”] = len(sample.split()) output[“chars”] = len(list(sample)) for elem in sample.split(): output[elem] = len(list(elem)) print(output) output: {‘word’: 4, ‘chars’: 17, ‘i’: 1, ‘am’: 2, ‘feeling’: 7, ‘good’: 4} solved A dictionary that returns number of word and each word’s lenght Python

[Solved] Create python dictionary from TXT file – value aggregation

No need to bother with pandas. Text files are iterable. Just open it, operate on the line (string) and fill a dictionnary. file = “font.txt” with open(file, “r”) as f: dic = dict() for line in f: x = line.strip(“\n”).split(” “) key = int(x[0].strip(“px”)) value = int(x[1]) if key not in dic.keys(): dic[key] = [value] … Read more

[Solved] How to sort python dictionary/list?

Assuming I understood your question, these should do it: for person in sorted(cat1): print(person, max(cat1.get(person))) result: ben 9 jeff 6 sam 9 then: for person in sorted(cat1, key=lambda x: max(cat1.get(x)), reverse=True): print(person, max(cat1.get(person))) result: ben 9 sam 9 jeff 6 then: for person in sorted(cat1, key=lambda x: sum(cat1.get(x))/len(cat1.get(x)), reverse=True): print(person, sum(cat1.get(person))/len(cat1.get(person))) result: ben 7.666666666666667 sam … Read more

[Solved] Create a dictionnary from defaultdict(list) with nested list inside [closed]

Just loop through your dictionary elements, reformatting the list of lists into list of dictionary using a list comprehension. original = { ‘t0’: [[‘cat0’, [‘eagle0’]], [‘cat1’, [‘eagle1’]]], ‘t1’: [[‘cat2’, [‘eagle2’, ‘eagle3’]]] } result = [] for key, cats in original.items(): cats = [{‘cat’: cat, ‘eagles’: eagles} for cat, eagles in cats] result.append({‘t’: key, ‘cats’: cats}) … Read more

[Solved] Spell check with google dictionary [closed]

@Override public void onGetSuggestions(final SuggestionsInfo[] arg0) { isSpellCorrect = false; final StringBuilder sb = new StringBuilder(); for (int i = 0; i < arg0.length; ++i) { // Returned suggestions are contained in SuggestionsInfo final int len = arg0[i].getSuggestionsCount(); if(editText1.getText().toString().equalsIgnoreCase(arg0[i].getSuggestionAt(j)) { isSpellCorrect = true; break; } } } You can find the whole project from this … Read more

[Solved] Python Form a List based from the values in dictionary

dictA = {“f1” : [“m”], “f2” : [“p”,”y”,”o”,”a”,”s”,”d”,”f”,”g”], “f3” : [“w”,”t”], “f5” : [“z”,”x”], “f6” : [“c”,”v”]} result = [] limit_size = 3 values_list = [] for values in dictA.itervalues(): values_list.append(len(values)) for i in range(0,max(values_list)): keys = list(dictA.keys()) count = 0 while count < len(keys): try: result.append(dictA[keys[count]][i]) except IndexError: dictA.pop(keys[count]) count = count + 1 … Read more

[Solved] Connecting data in python to spreadsheets

Are you trying to write your dictionary in a Excel Spreadsheet? In this case, you could use win32com library: import win32com.client xlApp = win32com.client.DispatchEx(‘Excel.Application’) xlApp.Visible = 0 xlBook = xlApp.Workbooks.Open(my_filename) sht = xlBook.Worksheets(my_sheet) row = 1 for element in dict.keys(): sht.Cells(row, 1).Value = element sht.Cells(row, 2).Value = dict[element] row += 1 xlBook.Save() xlBook.Close() Note that … Read more