python:将这一行与下一行进行比较

标签 python list

c1 是这样的列表列表:

c1=[[1,2,3],[1,2,6],[7,8,6]]

for row in c1:

我想跟踪 row[0] 是否有变化

例如:

[1, 2, 3][1, 2, 6] row[0] 中没有变化/p>

但是在 [1, 2, 6][7, 8, 6]row[0] 发生了变化

我如何捕捉这种变化? 我也想知道这个改变发生在哪一行

最佳答案

如果你有矩阵数据,你基本上想要第一个“列”的差异。 您可能想要报告更改和更改的位置,并且可能想要稀疏地存储它们:

c1=[[1,2,3],[1,2,6],[7,8,6]]
ans=[]    # a list of [indices,differences]
col=0
for i in range(len(c1)-1):
    diff = c1[i+1][col]-c1[i][col]
    if diff!=0:
        ans.append([i,diff])

关于python:将这一行与下一行进行比较,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3398879/

相关文章:

python - 如何在不使用密码的情况下连接到 MySQL? (PyMySQL)

python - 有人可以向我解释这个井字棋棋盘代码吗?

python - 如何从 VS Code 的输出中删除 [Running] python -u "c:\Users\####\Downloads\Tutorial\HelloWorld\app.py"等消息?

python - 从两个列表列表中查找共同元素

python - 二维列表仅按第一位排序

c# - 两个列表之间的实际差异 - 不除外

python - Qt - 子部件后面的背景图像

python - 使用 string.format 的整数列表连接

list - SharePoint 2013 - 要素架构包含错误的字段名称 : feature name = [GUID], 字段名称 [字段名称]

python - 类型错误 : unsupported operand type(s) for -: 'str' and 'int' Writing a story?