【问题标题】:Attribute can only be updated once in a nested for loop属性只能在嵌套的 for 循环中更新一次
【发布时间】:2020-11-01 06:53:11
【问题描述】:

我是 Python 新手,我的第一个 Python 脚本有一些问题。我有一个带有 .tag 属性的数据列表,该属性最初设置为 0。

lst = [a, b, c, d, e, f]
for x in lst:
    x.tag = 0

然后我得到了列表的真实标签和相应元素索引的结果。它存储在字典列表中。显然,列表中的某些元素具有多个标签。现在我想更新列表中所有元素的标签,以便每个元素都具有类似f.tag = [2,3,4,5] 的标签属性。我写了以下脚本。这个想法是遍历每个字典并获取索引和标签。然后循环遍历每个索引,检查列表的对应元素是否有tag = 0。如果有,将0改为新的tag作为单元素列表;如果没有,这意味着它之前已经更新,所以只需附加新标签。我的脚本如下:

results = [{'indices':(2), 'tag':0}, {'indices':(1,3), 'tag':1}, {'indices':(0,3,4,5), 'tag':2}, {'indices':(4,5), 'tag':3}, {'indices':(1,5), 'tag':4}, {'indices':(5), 'tag':5}]

for dct in results:
    indices = dct['indices']
    t = dct['tag']
    for i in indices:
        if lst[i].tag == 0:
            lst[i].tag = [t]
        elif t not in lst[i].tag:
            print('The code ever goes here')
            lst[i].tag.append(t) 

但是,在我运行此程序后,我再也没有看到打印出“The code ever going here”。然后我检查列表,我只看到所有具有非零实际标记的元素都被更新,但只有一次。这意味着元素的标签不能被第二次更新。这绝对不行,因为有些元素有多个标签,需要不断更新。我不知道我的代码可能出了什么问题。

【问题讨论】:

  • 你能提供一个你的输入和预期输出的例子吗?

标签: python list dictionary for-loop if-statement


【解决方案1】:

逻辑相当复杂,但我认为它确实做你想做的事。我怀疑问题出在您的输入results 上。特别是:

  • 对于像(2) 这样的单元素索引,将它们更改为(2,),以便它们可以正确迭代。 (2) 仅计算为 2 并且不能被迭代。
  • 最后一个元素应该是{'indices':(5,), 'tag':5} 而不是{'5':, 'tag':5}

更改这些后,我可以运行一个最小的工作示例:

class A:
    def __init__(self):
        self.tag = 0

lst = [A() for _ in range(6)]
results = [
    {"indices": (2,), "tag": 0},
    {"indices": (1, 3), "tag": 1},
    {"indices": (0, 3, 4, 5), "tag": 2},
    {"indices": (4, 5), "tag": 3},
    {"indices": (1, 5), "tag": 4},
    {"indices": (5,), "tag": 5},
]

for dct in results:
    indices = dct["indices"]
    t = dct["tag"]
    for i in indices:
        if lst[i].tag == 0:
            lst[i].tag = [t]
        elif t not in lst[i].tag:
            print("The code does go here")
            lst[i].tag.append(t)

紧随其后

for x in lst:
  print(x.tag)
>
[2]
[1, 4]
[0]
[1, 2]
[2, 3]
[2, 3, 4, 5]

【讨论】:

    【解决方案2】:

    如果元素有标签属性,我会使用该属性来保存标签列表...非常干净:

    In [38]: class thing(): 
        ...:     def __init__(self): 
        ...:         self.tags = [0,]   # the default 
        ...:          
        ...:          
        ...:                                                                        
    
    In [39]: # make some "things"                                                   
    
    In [40]: x = thing()                                                            
    
    In [41]: y = thing()                                                            
    
    In [42]: z = thing()                                                            
    
    In [43]: x.tags                                                                 
    Out[43]: [0]
    
    In [44]: x.tags.append(44)                                                      
    
    In [45]: x.tags.append(-2)                                                      
    
    In [46]: z.tags.append(3)                                                       
    
    In [47]: for t in [x, y, z]: 
        ...:     print(t.tags) 
        ...:                                                                        
    [0, 44, -2]
    [0]
    [0, 3]
    

    【讨论】:

    • [0,] 你不需要结尾的逗号。
    • 有点啰嗦……但没关系。
    猜你喜欢
    • 2018-07-24
    • 1970-01-01
    • 1970-01-01
    • 2017-09-20
    • 1970-01-01
    • 2015-10-21
    • 1970-01-01
    • 2022-06-15
    • 1970-01-01
    相关资源
    最近更新 更多