Python Forum
Transforming nested key-tuples into their dictionary values
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Transforming nested key-tuples into their dictionary values
#1
Surely there has got to be a better way of doing this?


>>> d = dict(enumerate(list('abcde')))
>>> INPUT = [[(0, 1, 2), (3, 4)], [(0, 1, 3), (2, 4)], [(0, 1, 4), (2, 3)], [(0, 2, 3), 
(1, 4)], [(0, 2, 4), (1, 3)], [(0, 3, 4), (1, 2)], [(1, 2, 3), (0, 4)], [(1, 2, 4), (0, 3)], 
[(1, 3, 4), (0, 2)], [(2, 3, 4), (0, 1)]]
>>> list(map(lambda grouping: tuple(map(lambda group: tuple(map(lambda id_: d[id_], group)), grouping)), INPUT))
Output:
[(('a', 'b', 'c'), ('d', 'e')), (('a', 'b', 'd'), ('c', 'e')), (('a', 'b', 'e'), ('c', 'd')), (('a', 'c', 'd'), ('b', 'e')), (('a', 'c', 'e'), ('b', 'd')), (('a', 'd', 'e'), ('b', 'c')), (('b', 'c', 'd'), ('a', 'e')), (('b', 'c', 'e'), ('a', 'd')), (('b', 'd', 'e'), ('a', 'c')), (('c', 'd', 'e'), ('a', 'b'))]
Reply
#2
even without going into understanding your goal - instead of 3 lamdas - define separarte functions... oneliners are not always better coding
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
In what way do you want it to be "better"? Run faster, clearer code, different output, something else?
Reply
#4
My goal is to substitute the keys in the input object with their corresponding dictionary value. I want to write clearer code or faster code (or both). In particular, I'm wondering if there is no more standard way of doing what I'm doing -- for example, if there is some function I can import.
Reply
#5
No, there's no obvious mapping function that does mapping within nested structures natively. I'd probably write it as separate loops to improve readability if the performance weren't critical.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  I'm assuming you're asking about accessing nested dictionary keys and values in Pytho DavidSah 0 580 Dec-18-2025, 01:54 AM
Last Post: DavidSah
Question [SOLVED] Access keys and values from nested dictionary? Winfried 4 914 Nov-17-2025, 11:47 AM
Last Post: Winfried
  Replace values in Yaml file with value in dictionary PelleH 1 3,818 Feb-11-2025, 09:51 AM
Last Post: alexjordan
Question Using Lists as Dictionary Values bfallert 8 3,938 Apr-21-2024, 06:55 AM
Last Post: Pedroski55
  need to compare 2 values in a nested dictionary jss 2 2,974 Nov-30-2023, 03:17 PM
Last Post: Pedroski55
  Printing specific values out from a dictionary mcoliver88 6 3,856 Apr-12-2023, 08:10 PM
Last Post: deanhystad
  Adding values with reduce() function from the list of tuples kinimod 10 7,585 Jan-24-2023, 08:22 AM
Last Post: perfringo
Question How to print each possible permutation in a dictionary that has arrays as values? noahverner1995 2 3,502 Dec-27-2021, 03:43 AM
Last Post: noahverner1995
  Nested dictionary acting strange Pedroski55 2 3,645 May-13-2021, 10:37 PM
Last Post: Pedroski55
  format the output from a nested dictionary. nostradamus64 9 8,528 May-03-2021, 04:45 PM
Last Post: nostradamus64

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020