【问题标题】:Find element in a list with a substring在带有子字符串的列表中查找元素
【发布时间】:2019-08-25 15:20:25
【问题描述】:

我有一个包含以下存储数据的泡菜文件:

indices = np.load("descritores/indices_pessoa.pickle")
print(indices) # result

 {0: 'fotos\\pessoa.1.1.jpg', 1: 'fotos\\pessoa.1.2.jpg', 2: 'fotos\\pessoa.1.3.jpg', 3: 'fotos\\pessoa.2.1.jpg', 4: 'fotos\\pessoa.2.2.jpg', 5: 'fotos\\pessoa.2.3.jpg'}

我想获取包含“pessoa.1”作为子字符串的元素的所有索引,并将它们从列表中删除。

到目前为止,我已经尝试过了,但没有成功:

r = [i for i in indices if "pessoa.1" in i]
print(r)

【问题讨论】:

    标签: python arrays find element


    【解决方案1】:

    作为您问题的输出,我看到了字典。

    {0: 'fotos\pessoa.1.1.jpg', 1: 'fotos\pessoa.1.2.jpg', 2: 'fotos\pessoa.1.3.jpg', 3: 'fotos\pessoa.2.1.jpg ', 4: 'fotos\pessoa.2.2.jpg', 5: 'fotos\pessoa.2.3.jpg'}

    如果您将“索引”理解为字典中的键,只需对其进行迭代。

    indexes = []
    for index, value in indices.items():
        if 'pessoa.1' in value:
            indexes.append(index)
    for index in indexes:
        del indices[index]
    

    【讨论】:

      猜你喜欢
      • 2022-10-19
      • 2019-04-25
      • 1970-01-01
      • 2012-05-16
      • 1970-01-01
      • 1970-01-01
      • 2018-07-17
      • 1970-01-01
      相关资源
      最近更新 更多