在python中使用三个括号从数组中获取值。

问题描述 投票:0回答:1

萃取的 tsbuf 来自 pcap 文件。并将这些值追加到 scr_ports 阵列。

而我不确定的是 [i][0][1] 使用的语法如下。请你解释一下好吗?

for ts, buf in pcap: 
    src_ports = []

    src_ports[index].append([ts, buf])

    buf = src_ports[i][0][1]
python arrays pcap
1个回答
2
投票

[n] 是索引操作符,它给出了列表中的第 n 个项目。在python中,你没有多维数组,取而代之的是列表的列表。当你有了:

arr = [[0, 1], [2, 3]]
print(arr[0]) # this prints [0, 1], that is 0th list in arr
print(arr[0][0]) # this prints 0, 0th item of 0th item an arr;
# 0th item in arr is [0, 1]; 0th item of that is 0

你可以将其扩展为更多维的数组。


1
投票

src_ports 将是一个三维数组。[ [[ts1, buf1], [ts2, buf2], ....], [...], ...].

所以,第一个 [i] 将给你第一个2D数组,在 src_portsజజజజజజజజజజజజజజజజజజజజజజజజజజజజజజజజజజజజజజజ [0] 将得到其中的第1个数组,而 [1] 会给你 buf 内的1D数组。

另外,你在循环中清除数组似乎很奇怪。这基本上意味着你 append 每次都会被清空数组,这对我来说毫无意义。

© www.soinside.com 2019 - 2024. All rights reserved.