Hey, this is an English text. I want you to translate it into the Tunisian dialect.…
- User
Hey, this is an English text. I want you to translate it into the Tunisian dialect. Please follow these steps:
- Translate step by step and highlight differences between Tunisian dialect and Modern Standard Arabic.
- Highlight sections you are confident translating into the Tunisian dialect. Translate this text into the Tunisian dialect, focusing on capturing the essence of the language nuances:
Q: Dictionary lookup efficiency I have a list of list of lists to which I want to add a list with summed values from a dictionary based on a lookup. The code below works but is not fast enough to process the amount of data I have, so the question is how to do this more efficiently. I'm using Python 2.7. people = [ [['A'], ['B']], [['B'], ['A', 'C', 'E', 'D']], [['C'], ['B']], [['D'], ['B', 'E']], [['E'], ['H', 'B', 'D']], [['F']], [['G']], [['H'], ['E']], [['I']] ] dollars = {'A': 575, 'B': 628, 'C': 693, 'D': 456, 'E': 1595, 'F': 1366, 'G': 1596, 'H': 2667, 'I': 2145} I have tried (variations of) the following: num_dos = 2 # a placeholder, will be user input for groups in people: groups.append([]) for i in range(min(len(groups), num_dos)): groups[-1].extend([v for k, v in dollars.items() if k in groups[i]]) groups[-1] = sum(groups[-1]) people.sort(key = lambda x: x[-1], reverse = True) which gives the desired result: [ [['E'], ['H', 'B', 'D'], 5346], [['H'], ['E'], 4262], [['B'], ['A', 'C', 'E', 'D'], 3947], [['D'], ['B', 'E'], 2679], [['I'], 2145], [['G'], 1596], [['F'], 1366], [['C'], ['B'], 1321], [['A'], ['B'], 1203] ] A: Instead of iterating the items in the dictionary and testing whether they are in the list, you should do it the other way around: Iterate the items in the lists and get their value from the dictionary (if any). This way, it's much faster, as lookup in a list is O(n), whereas its more like O(1) in a dictionary. Also, there seems to be no need for all those intermediate lists of numbers; just add the sums and add those to the list at the end. for groups in people: s = 0 for i in range(min(len(groups), num_dos)): s += sum(dollars.get(x, 0) for x in groups[i]) groups.append(s)
- Assistant