Python 在列表列表中展平内部列表

Python 在列表列表中展平内部列表,python,Python,假设我有以下对象列表: lol = [['When was America discovered by Christopher Colombus?', ['1492', '1510', '1776', '1820'], '1492'], ['What is the name of the school that Harry Potter attends?', ['Hogwarts', 'Gryffindor', 'Voldemort', 'Magic School'], 'Hogwarts'],

假设我有以下对象列表:

lol = [['When was America discovered by Christopher Colombus?', ['1492', '1510', '1776', '1820'], '1492'], ['What is the name of the school that Harry Potter attends?', ['Hogwarts', 'Gryffindor', 'Voldemort', 'Magic School'], 'Hogwarts'], ['How many colors are there in the common depiction of the rainbow?', ['5', '7', '9', '11'], '7']
我正在尝试将内部列表展平(例如,删除括号)。结果看起来像

['When was America discovered by Christopher Colombus?', '1492', '1510', '1776', '1820', '1492']
我已经看到了整平整个列表的一般解决方案,如下所示:


但我不知道如何在一个更大的列表中选择性地展平一个列表

如果你知道如何展平整个列表,并且你想知道如何仅展平第一个列表,那么你不是在问如何展平列表,而是在问如何选择列表中的第一项,答案是执行
sublist=lol[0]
,然后将其他问题的答案应用到
sublist

我认为您的意思是,您不仅需要显示的输出,还需要示例输出中的其他两个。以下是如何做到这一点:

lol = [['When was America discovered by Christopher Colombus?', ['1492', '1510', '1776', '1820'], '1492'], ['What is the name of the school that Harry Potter attends?', ['Hogwarts', 'Gryffindor', 'Voldemort', 'Magic School'], 'Hogwarts'], ['How many colors are there in the common depiction of the rainbow?', ['5', '7', '9', '11'], '7']]

r = []
for x in lol:
    r.append([x[0]] + x[1] + [x[2]])

for rr in r:
    print(rr)
结果:

['When was America discovered by Christopher Colombus?', '1492', '1510', '1776', '1820', '1492']
['What is the name of the school that Harry Potter attends?', 'Hogwarts', 'Gryffindor', 'Voldemort', 'Magic School', 'Hogwarts']
['How many colors are there in the common depiction of the rainbow?', '5', '7', '9', '11', '7']