Python convert a list of nested tuples into a dict

semi-extrinsic

Ok, so I am trying to write a Python function that turns the first line here, a list of nested tuples, into the second line, a flattened dictionary:

[('Ka',0.6), ('La', 0.6), (('Ma', 0.7), ('Na', 0.8), ('Oa', 0.9))]   
{'La': 0.6, 'Ma': 0.7, 'Ka': 0.6, 'Na': 0.8, 'Oa': 0.9}

A slight complication is that the outer tuples in the list are members of different objects, and that the argument of the function is a list of these objects. Hopefully the code below explains this.

Now I have actually managed to cobble together a solution to this problem, but it is so hideous that I have to ask how to do this in a more pythonic / less obfuscated way:

def theFunction(args):
    # Please don't ask me how this works. It just does.
    flatten = lambda *n: (e for a in n for e in (flatten(*a) if
        isinstance(a, (tuple, list)) else (a,)))
    return dict(list(zip(*([iter(list(flatten(list(l.sn for l in args))))]*2))))

class le:
    def __init__(self,n):
        self.sn = (n,0.6)

class lf:
    def __init__(self,n,m,o):
        self.sn = (n,0.7), (m,0.8), (o, 0.9)

l1 = le("Ka")
l2 = le("La")
l3 = lf("Ma","Na","Oa")
theList = [l1,l2,l3]
print([l.sn for l in theList])
print(theFunction(theList))

The two print statements produce as output the two lines at the top of the question.

senshin

Can you change the definition of le so that self.sn is a tuple of tuples there, like it is in lf? If so, this is easy:

class le:
    def __init__(self, n):
        self.sn = (n, 0.6), 
        #                 ^ make self.sn a tuple of tuples in all cases

class lf:
    def __init__(self, n, m, o):
        self.sn = (n, 0.7), (m, 0.8), (o, 0.9)

l1 = le("Ka")
l2 = le("La")
l3 = lf("Ma","Na","Oa")
theList = [l1, l2, l3]
result = dict([tup for thing in theList for tup in thing.sn])
# result == {'Na': 0.8, 'Ka': 0.6, 'Ma': 0.7, 'Oa': 0.9, 'La': 0.6}

Also, maybe consider not using lowercase "L"s so liberally in short variable names, because it's a bear and a half to read in most fonts.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Python convert list of nested tuples to list of tuples

From Dev

How to convert a two nested list of lists into a nested list of tuples in Python?

From Dev

How to convert a dict of lists to a list of tuples of key and value in python?

From Dev

How to convert a dictionary into a list of tuples without dict functions? [PYTHON]

From Dev

How to convert nested list of lists into a list of tuples in python 3.3?

From Dev

Python Nested tuples to list

From Dev

Convert from tuple of tuples to nested tuples in Python

From Dev

Convert Nested dictionaries to tuples python

From Dev

How to merge and convert list of tuples to Dict

From Dev

convert flat dict to nested list and dict and intersect this

From Java

Convert nested Python dict to object?

From Dev

Python: using a dict to speed sorting of a list of tuples

From Dev

Python dict of lists of tuples. print list of element from tuples

From Dev

Python - Dict nest in List nested in Dict

From Dev

Append to a nested list or dict in Python

From Dev

Append to a nested list or dict in Python

From Dev

Convert nested list to dict where first element of list is key for dict

From Dev

Convert a list of strings to a list of tuples in python

From Dev

Python convert a list of lists into a list of tuples

From Dev

Nested list comprehension dict and list python

From Dev

Convert list of tuples in tabular format in python

From Dev

Convert list of tuples of several values into dictionary in Python

From Dev

How to convert a list of tuples to a dictionary of dictionaries in Python?

From Dev

how to convert a list of tuples into a lists of part of tuples in python

From Dev

python - Nested list in dict to csv files

From Dev

python searching through dict that contain list of nested dict

From Dev

Coercing Dict to Read List as Tuples

From Dev

How to convert python string data to list or in dict?

From Dev

Python convert list to dict with multiple key value

Related Related

  1. 1

    Python convert list of nested tuples to list of tuples

  2. 2

    How to convert a two nested list of lists into a nested list of tuples in Python?

  3. 3

    How to convert a dict of lists to a list of tuples of key and value in python?

  4. 4

    How to convert a dictionary into a list of tuples without dict functions? [PYTHON]

  5. 5

    How to convert nested list of lists into a list of tuples in python 3.3?

  6. 6

    Python Nested tuples to list

  7. 7

    Convert from tuple of tuples to nested tuples in Python

  8. 8

    Convert Nested dictionaries to tuples python

  9. 9

    How to merge and convert list of tuples to Dict

  10. 10

    convert flat dict to nested list and dict and intersect this

  11. 11

    Convert nested Python dict to object?

  12. 12

    Python: using a dict to speed sorting of a list of tuples

  13. 13

    Python dict of lists of tuples. print list of element from tuples

  14. 14

    Python - Dict nest in List nested in Dict

  15. 15

    Append to a nested list or dict in Python

  16. 16

    Append to a nested list or dict in Python

  17. 17

    Convert nested list to dict where first element of list is key for dict

  18. 18

    Convert a list of strings to a list of tuples in python

  19. 19

    Python convert a list of lists into a list of tuples

  20. 20

    Nested list comprehension dict and list python

  21. 21

    Convert list of tuples in tabular format in python

  22. 22

    Convert list of tuples of several values into dictionary in Python

  23. 23

    How to convert a list of tuples to a dictionary of dictionaries in Python?

  24. 24

    how to convert a list of tuples into a lists of part of tuples in python

  25. 25

    python - Nested list in dict to csv files

  26. 26

    python searching through dict that contain list of nested dict

  27. 27

    Coercing Dict to Read List as Tuples

  28. 28

    How to convert python string data to list or in dict?

  29. 29

    Python convert list to dict with multiple key value

HotTag

Archive