Re: Need max values in list of tuples, based on position
On 11/11/2022 18:53, DFS wrote: On 11/11/2022 12:49 PM, Dennis Lee Bieber wrote: On Fri, 11 Nov 2022 02:22:34 -0500, DFS declaimed the following: [(0,11), (1,1), (2,1), (0,1) , (1,41), (2,2), (0,9) , (1,3), (2,12)] The set of values in elements[0] is {0,1,2} I want the set of max values in elements[1]: {11,41,12} Do they have to be IN THAT ORDER? Yes. Sets aren't ordered, which is why I gave my answer as a list. A wrongly ordered list, but I thought it rude to point out my own error, as no one else had. :-) Assuming you want numeric order of element[0], rather than first occurrence order of the element[0] in the original tuple list. In this example, they are both the same. Here is a corrected version from collections import OrderedDict def build_max_dict( tups): dict = OrderedDict() for (a,b) in tups: if (a in dict): if (b>dict[a]): dict[a]=b else: dict[a]=b return(dict.values()) This solution giving the answer as type odict_values. I'm not quite sure what this type is, but it seems to be a sequence/iterable/enumerable type, whatever the word is in Python. Caveat: I know very little about Python. -- https://mail.python.org/mailman/listinfo/python-list
Re: Need max values in list of tuples, based on position
On 11/11/2022 07:22, DFS wrote: [(0,11), (1,1), (2,1), (0,1) , (1,41), (2,2), (0,9) , (1,3), (2,12)] The set of values in elements[0] is {0,1,2} I want the set of max values in elements[1]: {11,41,12} def build_max_dict( tups): dict = {} for (a,b) in tups: if (a in dict): if (b>dict[a]): dict[a]=b else: dict[a]=b return(sorted(dict.values())) -- https://mail.python.org/mailman/listinfo/python-list
Re: Need max values in list of tuples, based on position
On 11/11/2022 20:58, Thomas Passin wrote: On 11/11/2022 2:22 PM, Pancho via Python-list wrote: On 11/11/2022 18:53, DFS wrote: On 11/11/2022 12:49 PM, Dennis Lee Bieber wrote: On Fri, 11 Nov 2022 02:22:34 -0500, DFS declaimed the following: [(0,11), (1,1), (2,1), (0,1) , (1,41), (2,2), (0,9) , (1,3), (2,12)] The set of values in elements[0] is {0,1,2} I want the set of max values in elements[1]: {11,41,12} Do they have to be IN THAT ORDER? Yes. Sets aren't ordered, which is why I gave my answer as a list. A wrongly ordered list, but I thought it rude to point out my own error, as no one else had. :-) Assuming you want numeric order of element[0], rather than first occurrence order of the element[0] in the original tuple list. In this example, they are both the same. Here is a corrected version from collections import OrderedDict def build_max_dict( tups): dict = OrderedDict() for (a,b) in tups: if (a in dict): if (b>dict[a]): dict[a]=b else: dict[a]=b return(dict.values()) This solution giving the answer as type odict_values. I'm not quite sure what this type is, but it seems to be a sequence/iterable/enumerable type, whatever the word is in Python. Caveat: I know very little about Python. Kindly do not use "dict" as a variable name, since that shadows the system's built-in name for a dictionary type. Yes, I half suspected it might cause subtle problems, I changed it to d, and then I changed it back, senility I guess :-). That was one of the things I didn't like about Python. Lack of types and subsequent loss of intellisense is the thing I find hardest to deal with. -- https://mail.python.org/mailman/listinfo/python-list
Re: Need max values in list of tuples, based on position
On 11/11/2022 19:56, DFS wrote: Edit: found a solution online: - x = [(11,1,1),(1,41,2),(9,3,12)] maxvals = [0]*len(x[0]) for e in x: maxvals = [max(w,int(c)) for w,c in zip(maxvals,e)] print(maxvals) [11,41,12] - So now the challenge is making it a one-liner! x = [(11,1,1),(1,41,2),(9,3,12)] print(functools.reduce( lambda a,b : [max(w,c) for w,c in zip(a,b)], x, [0]*len(x[0]))) -- https://mail.python.org/mailman/listinfo/python-list
Re: Find 6-letter words that are hidden (embedded) within
On 24/02/2023 18:34, Hen Hanna wrote: i just wrote a program, which... within[FunFunPython] finds: (funny,futon,python) ( 5- and 6- letter words ) (my program uses a Trie, but is pretty simple) Maybe someone would show me how it's done usingitertools, Permutations, etc. Wouldn't it get too slow for Letter-Seeds longer than 11 letters or so? For performance, generally you sort the characters in a word to create a new word (character list) and compare the sorted character lists (which you can use a string for). Two anagrams will produce the same sorted character list. The problem with going through all permutations is that the number of permutations tends to grow exponentially, so that is why you sort first. So first take an English dictionary and build a python dictionary with the key being the character sorted list and the value being a list of the English words that produce the sorted list, aka anagrams. You then sort your specific word and test each subset of this sorted word (each combination of characters) against your python anagram dictionary. -- https://mail.python.org/mailman/listinfo/python-list