将列表插入包含多个列表的列表中

时间:2014-04-13 13:30:05

标签: python list add

用于知识目的

Input:

    [[[[[], [], []]], ['trynda'], [[[], [], []]]], ['cait'], [[[[], [], []]], ['veigar'], [[[], [], []]]]]

Output:

    [[[[[], [x], []]], ['trynda'], [[[], [], []]]], ['cait'], [[[[], [], []]], ['veigar'], [[[], [], []]]]]

如何将x添加到列表列表的第二个列表中?

2 个答案:

答案 0 :(得分:3)

只需使用嵌套索引(适用于嵌套列表):

original_list[0][0][0][1].append(x)  # 2nd of 1st of 1st of 1st.

假设x是一个变量。否则,请使用字符串'x'

演示:

>>> original_list = [[[[[], [], []]], ['trynda'], [[[], [], []]]], ['cait'], [[[[], [], []]], ['veigar'], [[[], [], []]]]]
>>> original_list[0][0][0][1].append('x')
>>> original_list
[[[[[], ['x'], []]], ['trynda'], [[[], [], []]]], ['cait'], [[[[], [], []]], ['veigar'], [[[], [], []]]]]
          ^

答案 1 :(得分:1)

my_list[0][0][0][1].append(x)

假设x包含值。如果是字符串,您需要'x'