[Solved] Python:how to get keys with same values? [closed]

If you want this to work for any arbitrary key(s) you can use a defaultdict of OrderedDicts.. from collections import defaultdict, OrderedDict result_dict = defaultdict(OrderedDict) data = [(‘Han Decane’,’12333′),(‘Can Decane’,’12333′),(‘AlRight’,’10110′)] for (v,k) in data: result_dict[k][v]=True >>> list(result_dict[‘12333’].keys()) [‘Han Decane’, ‘Can Decane’] And if you want all the results that had multiple values >>> [k for … Read more

[Solved] Why does this code not output the winner variable?

Try: import random class player: def __init__(self, name): self.name = name self.score = random.randint(1,6) print(self.name, self.score) player_1 = player(“Richard”) player_2 = player(“Bob”) winner=”” if player_1.score > player_2.score: winner = player_1.score print(winner) elif player_2.score> player_1.score: winner = player_2.score print(winner) else: print(“Tie.”) Output: 7 solved Why does this code not output the winner variable?

[Solved] Python Linked list minimum value

Answering this specifically would be difficult without knowing the specifics of your node implementation, and as this is almost certainly a homework problem wouldn’t be sporting anyway. What you want to do is go through each element of the list, and compare it to the Min you have. If it’s higher, then just go to … Read more

[Solved] Convert piglatin to english?

Something like this? def decode (line): return ‘ ‘.join (token [:-3] if token.endswith (‘WAY’) else (lambda a, b: b [:-3] + a) (*token.split (‘X’) ) for token in line.split () ) Sample: >>> decode (‘elloXHWEY orldXwWEY isXthWEY isWAY eatXgrWEY’) ‘Hello world this is great’ Nota bene: Might fail with words conaining ‘X’. But it should … Read more

[Solved] Maximum value from contiguous part of a list

A simple example, define a list with five elements, print the max from the second until the third index. max is a built-in function. max: Return the largest item in an iterable or the largest of two or more arguments. l = [1,2,3,4,5] print (max(l[2:3])) solved Maximum value from contiguous part of a list

[Solved] how to make condition with if to check type of variables in python?

If your object isn’t a dict but could be converted to a dict easily (e.g. list of pairs), you could use: def make_a_dict(some_object): if isinstance(some_object, dict): return some_object else: try: return dict(some_object) except: return {‘return’: some_object} print(make_a_dict({‘a’: ‘b’})) # {‘a’: ‘b’} print(make_a_dict([(1,2),(3,4)])) # {1: 2, 3: 4} print(make_a_dict(2)) # {‘return’: 2} solved how to make … Read more