在具有子字符串的列表中查找元素

2024-10-01 19:17:52 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个pickle文件,其中存储了以下数据:

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)

Tags: 文件数据字符串in元素nploadresult
1条回答
网友
1楼 · 发布于 2024-10-01 19:17:52

作为你问题的输出,我看了词典。你知道吗

{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'}

如果您将“index”理解为字典中的一个键,只需迭代它。你知道吗

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

相关问题 更多 >

    热门问题