在具有子字符串的列表中查找元素
Find element in a list with a substring
我有一个 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]
我有一个 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]