简体   繁体   中英

How to unpack into tuples the following nested list of dict?

I have a very large nested list like this one:

a_lis = [[{'A': 'the', 'B': 'US3---'}, {'A': 'the', 'B': 'PS3N'}, {'A': 'the', 'B': 'P3N'}, {'A': 'quick', 'B': 'GS'}, {'A': 'quick', 'B': 'NCMSN-'}, {'A': 'fox', 'B': 'YN-'}, {'A': 'it', 'B': 'VI--2PSA--N-'}, {'A': 'jumping', 'B': 'GNM-'}]]

How can to transform it into?:

[('the', 'US3---'), ('the', 'PS3N'), ('the', 'P3N'), ('quick', 'GS'), ('quick', 'NCMSN-'), ('fox', 'YN-'), ('it's, 'VI--2PSA--N-'), ('jumping', 'GNM-')]

I tried to:

tuples = ['{}'.join(x) for x in a_list[0]]

And:

values = [','.join(str(v) for v in a_list)]

The main issue is that I do not how to manage the }{ characters. Could somebody explain which is the best way to manage them with a comprehension list?.

Fixing the syntax error with strings in your input line, you could the correct ensure order with something like

>>> list(map(lambda d: (d['A'], d['B']), a_lis[0]))

[('the', 'US3---'),
 ('the', 'PS3N'),
 ('the', 'P3N'),
 ('quick', 'GS'),
 ('quick', 'NCMSN-'),
 ('fox', 'YN-'),
 ("it's", 'VI--2PSA--N-'),
 ('jumping', 'GNM-')]

or equivalently with a list comprehension

>>> [(d['A'], d['B']) for d in a_lis[0]]

[('the', 'US3---'),
 ('the', 'PS3N'),
 ('the', 'P3N'),
 ('quick', 'GS'),
 ('quick', 'NCMSN-'),
 ('fox', 'YN-'),
 ("it's", 'VI--2PSA--N-'),
 ('jumping', 'GNM-')]

If a_lis had items beyond further lists beyond the first index you wanted to also have in the list of tuples, you could unpack.

list(map(lambda d: (d['A'], d['B']), *a_lis))
[tuple(j.values()) for i in a_lis for j in i]

your nested list has a "quote" issue somewhere. Once fixed you can recreate the tuples from the dictionary values using a list comprehension:

a_lis = [[{'A': 'the', 'B': 'US3---'}, {'A': 'the', 'B': 'PS3N'}, {'A': 'the', 'B': 'P3N'}, {'A': 'quick', 'B': 'GS'}, {'A': 'quick', 'B': 'NCMSN-'}, {'A': 'fox', 'B': 'YN-'}, {'A': "it's", 'B': 'VI--2PSA--N-'}, {'A': 'jumping', 'B': 'GNM-'}]]

n = [tuple(a.values()) for a in a_lis[0]]

print(n)

you get:

[('US3---', 'the'), ('PS3N', 'the'), ('P3N', 'the'), ('GS', 'quick'), ('NCMSN-', 'quick'), ('YN-', 'fox'), ('VI--2PSA--N-', "it's"), ('GNM-', 'jumping')]

As someone noted, unless you're using Python 3.6, you get the natural internal order of the dictionary by doing this (not necessarily the input order), which may not be what you want.

Chain the lists and provide them to tuple :

from itertools import chain

tps = [tuple(i.values()) for i in chain.from_iterable(a_lis)]

The variable tps now (randomly) holds:

[('the', 'US3---'),
 ('the', 'PS3N'),
 ('the', 'P3N'),
 ('quick', 'GS'),
 ('quick', 'NCMSN-'),
 ('fox', 'YN-'),
 ('its', 'VI--2PSA--N-'),
 ('jumping', 'GNM-')]

If you need to deterministically handle the creation of tuples you should first transform all nested dictionaries into ordered dictionaries:

from collections import OrderedDict

a_lis = [OrderedDict(d) for d in a_lis[0]]

and then perform the dictionary conversion as previously.

You can get what you want by calling tuple on the values in each dictionary:

nested = a_lis[0]
value_tuples = [tuple(dictionary.values()) for dictionary in nested]

If you need the tuples to be sorted on key:

nested = a_lis[0]
value_tuples = [tuple(dictionary[k] for k in sorted(dictionary)) for dictionary in nested]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM