我试图将列表中所有子列表中的所有None个值替换为从0开始的递增数字,但不包括跳过列表中的数字.还有一个要求.如果子列表的第一个元素与任何其他子列表的第一个元素匹配,则它们都需要具有相同的值,如果存在,则替换其中的None.这是我目前可以try 的.

skip = [1,2]
a = [[1, None, 2], [3, 4, 5], [1, None, 7], [8, 9, 10],[11, None, 12]]
b = 0
d = {}
for i in range(len(a)):
    if a[i][1]==None:
        if b in skip:
            print("found b in skip")
            b = b + 1
        if a[i][1] in d.keys():
            a[i][1] = d[a[i][1]]
        else:
            a[i][1] = b
        d[a[i][0]] = b
        b = b + 1
print(d)
print(a)

输出:

found b in skip
{1: 2, 11: 3}
[[1, 0, 2], [3, 4, 5], [1, 2, 7], [8, 9, 10], [11, 3, 12]]

预期输出:

[[1, 0, 2], [3, 4, 5], [1, 0, 7], [8, 9, 10], [11, 3, 12]]

推荐答案

您在缓存中的几个位置查找了错误的元素,并且在skip列表包含连续元素时没有正确跳过.下面是一个带有内联注释的最小修复程序,该注释指示已更改的行:

skip = [1,2]
a = [[1, None, 2], [3, 4, 5], [1, None, 7], [8, 9, 10],[11, None, 12]]
b = 0
d = {}
for i in range(len(a)):
    if a[i][1]==None:
        while b in skip:          # Loop until skipped all elements in skip
            print("found b in skip")
            b = b + 1
        if a[i][0] in d.keys():   # Check for first, not second element
            a[i][1] = d[a[i][0]]  # Use first element, not second, for lookup
        else:
            a[i][1] = b
            d[a[i][0]] = b  # Indent so we only set cached value on cache miss
            b = b + 1       # Indent so we only increment b on new first element
print(d)
print(a)

Try it online!

这里有一个修改更为严格的版本,它有点像python,使用名称,而不是索引(如果可能的话):

skip = {1,2}  # Use set instead of list; if skip is large, list membership check will be expensive, set will stay cheap
a = [[1, None, 2], [3, 4, 5], [1, None, 7], [8, 9, 10],[11, None, 12]]
b = 0
d = {}
for sublist in a:                      # Loop over values instead of indexing a over and over, C-style (slow and less readable)
    first, middle, _ = sublist         # Unpack to useful names (reducing risk of misindexing, and making more readable code)
                                       # Not unpacking in for loop itself because we need to reassign elements of sublist
    if middle is None:                 # Always use is/is not to compare to None
        while b in skip:
            b += 1                     # Use += to avoid repeating variable name
        if first in d:                 # No need for .keys(); "in d" has same effect as "in d.keys()" and avoids creating unnecessary keys view
            sublist[1] = d[first]      # Indexing needed for assignment to modify original list
        else:
            sublist[1] = d[first] = b  # Can populate cache and reassign middle value all at once
            b += 1
print(d)
print(a)

Try it online!

任何一个版本都会从问题中获得预期的输出.

Python相关问答推荐

使用图片生成PDF Django rest框架

模型序列化器中未调用现场验证器

Image Font生成带有条形码Code 128的条形码时出现枕头错误OSErsor:无法打开资源

如果条件为真,则Groupby.mean()

Django管理面板显示字段最大长度而不是字段名称

时间序列分解

NP.round解算数据后NP.unique

数据抓取失败:寻求帮助

如果值发生变化,则列上的极性累积和

Pandas计数符合某些条件的特定列的数量

使用Python和文件进行模糊输出

如何从列表框中 Select 而不出错?

如何使regex代码只适用于空的目标单元格

Matplotlib中的字体权重

如何在两列上groupBy,并使用pyspark计算每个分组列的平均总价值

如何在海上配对图中使某些标记周围的黑色边框

提高算法效率的策略?

使用__json__的 pyramid 在客户端返回意外格式

BeautifulSoup:超过24个字符(从a到z)的迭代失败:降低了首次深入了解数据集的复杂性:

没有内置pip模块的Python3.11--S在做什么?