展平元组列表内的列表

Joe*_*Joe -1 python tuples list

我有以下元组列表。

lst = 
    [
        ('LexisNexis', ['IT Services and IT Consulting ', ' New York City, NY']),
        ('AbacusNext', ['IT Services and IT Consulting ', ' La Jolla, California']), 
        ('Aderant', ['Software Development ', ' Atlanta, GA']),
        ('Anaqua', ['Software Development ', ' Boston, MA']),
        ('Thomson Reuters Elite', ['Software Development ', ' Eagan, Minnesota']),
        ('Litify', ['Software Development ', ' Brooklyn, New York'])
    ]
Run Code Online (Sandbox Code Playgroud)

我想将每个元组中的列表展平为lst. 我发现这个如何从列表列表中制作平面列表?但不知道如何使其适合我的情况。

j1-*_*lee 5

您可以使用解包

lst = [('LexisNexis', ['IT Services and IT Consulting ', ' New York City, NY']),
       ('AbacusNext', ['IT Services and IT Consulting ', ' La Jolla, California']), 
       ('Aderant', ['Software Development ', ' Atlanta, GA']),
       ('Anaqua', ['Software Development ', ' Boston, MA']),
       ('Thomson Reuters Elite', ['Software Development ', ' Eagan, Minnesota']),
       ('Litify', ['Software Development ', ' Brooklyn, New York'])]

output = [(x, *l) for (x, l) in lst]

print(output)
# [('LexisNexis', 'IT Services and IT Consulting ', ' New York City, NY'),
#  ('AbacusNext', 'IT Services and IT Consulting ', ' La Jolla, California'),
#  ('Aderant', 'Software Development ', ' Atlanta, GA'),
#  ('Anaqua', 'Software Development ', ' Boston, MA'),
#  ('Thomson Reuters Elite', 'Software Development ', ' Eagan, Minnesota'),
#  ('Litify', 'Software Development ', ' Brooklyn, New York')]
Run Code Online (Sandbox Code Playgroud)

  • PEP Index 的绝佳资源,我将其添加到我的书签中。谢谢,@j1-lee。 (2认同)