我有一个 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)
最佳答案
作为你问题的输出,我看到了字典。
{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]
关于python - 查找列表中带有子字符串的元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57647275/