0

列表的列表正在踢我的屁股。我已经进行了大量研究,试图找到如何更新列表 python 数据结构列表中第 n 个列表的第 n 个元素。我看到很多关于更新第 n 个元素或将列表组合到现有列表中的建议,但没有关于更新嵌套列表中的特定元素的建议。

我的实际代码通读文件搜索 tid:somestring 然后将 tid:somestring 添加到集合中。我现在想遍历该集合并将该值设为每次出现的第 0 个 y。我在代码中的下一步将在第 1 个 y 中添加另一个搜索项,以获取我之前放置在第 0 个 y 位置的相应 tid 值(对于剩余的搜索值,依此类推)。我计划用一个计数器递增以移动 x 的索引值......基本上,我需要更新一个二维数组。y 轴上有 9 个值。x 轴取决于 tid:somestring 的唯一实例数,每个文件有 10,000 个或更多。

如何更新/插入或以其他方式更改列表列表中第 n 个列表的第 n 个项目?

samlfile="2013-08-18 22:47:55,248 tid:b7c31fbac DEBUG" #I have a file with 10000+ "tid"
tidset = set()
tidcount = len(tidset)
tidassert = [[0 for x in range(tidcount)] for y in range(9)]
xcount = 0


for line in samlfile:

    if "tid:" in line:
        str=line
        tid = re.search(r'(tid:.*?)(?= )', str)
        if tid.group() not in tidset:
            tidset.add(tid.group())
            tid = tid.group()
            for tid in tidset:
                tidassert[xcount][0] = tid
            xcount = xcount + 1

        print tidassert
4

1 回答 1

1
try:

    _list[n][n] = 'new val'

except IndexError:

    print 'No such index'
于 2013-10-25T11:48:28.230 回答