首页 > 解决方案 > 从列表中删除所有可重复的值以及其他值中的值,Python

问题描述

我的列表包含句子。有些句子可以重复,有些句子在其他句子中。例如:

  1. '杂环化合物'
  2. '具有氧的杂环化合物'

1 号在 2 号内,所以我只需要保留唯一的 2 号。

我的清单的一部分:


    ['HUMAN NECESSITIES',
     'MEDICAL OR VETERINARY SCIENCE; HYGIENE',
     'PREPARATIONS FOR MEDICAL, DENTAL, OR TOILET PURPOSES',
     'Medicinal preparations containing organic active ingredients',
     'Heterocyclic compounds',
     'Heterocyclic compounds having oxygen as the only ring hetero atom, e.g. fungichromin',
     'Heterocyclic compounds having oxygen as the only ring hetero atom, e.g. fungichromin having six-membered rings with one oxygen as the only ring hetero atom',
     'Heterocyclic compounds having oxygen as the only ring hetero atom, e.g. fungichromin having six-membered rings with one oxygen as the only ring hetero atom condensed with carbocyclic rings, e.g. cannabinols, methantheline',
     'HUMAN NECESSITIES',
     'MEDICAL OR VETERINARY SCIENCE; HYGIENE',
     'PREPARATIONS FOR MEDICAL, DENTAL, OR TOILET PURPOSES',
     'Medicinal preparations containing organic active ingredients',
     'Hydroxy compounds, e.g. alcohols; Salts thereof, e.g. alcoholates',
     'Phenols',
     'HUMAN NECESSITIES',
     'MEDICAL OR VETERINARY SCIENCE; HYGIENE',
     'PREPARATIONS FOR MEDICAL, DENTAL, OR TOILET PURPOSES',
     'Medicinal preparations containing organic active ingredients',
     'Acids; Anhydrides, halides or salts thereof, e.g. sulfur acids, imidic, hydrazonic, hydroximic acids',
     'Carboxylic acids, e.g. valproic acid',
     'Carboxylic acids, e.g. valproic acid having an amino group',
     'Carboxylic acids, e.g. valproic acid having an amino group the amino and the carboxyl groups being attached to the same acyclic carbon chain, e.g. gamma-aminobutyric acid [GABA], beta-alanine, epsilon-aminocaproic acid, pantothenic acid',
     'Alpha-aminoacids, e.g. alanine, edetic acids [EDTA]']

我能够通过使用删除重复

    set(my_list)

甚至通过(从结果字典中获取键)

    from collections import Counter
    Counter(my_list)

但我不知道如何删除其他值内的值。请帮我解决这个问题!

标签: pythonlist

解决方案


您可以在列表上创建 2 个嵌套循环,并且对于您与另一个比较的每个字符串,您跟踪最长的一个以保存每个字符串的结果。如果您有大量数据,这可能不是最佳解决方案

代码

currect_list=['HUMAN NECESSITIES',
 'MEDICAL OR VETERINARY SCIENCE; HYGIENE',
 'PREPARATIONS FOR MEDICAL, DENTAL, OR TOILET PURPOSES',
 'Medicinal preparations containing organic active ingredients',
 'Heterocyclic compounds',
 'Heterocyclic compounds having oxygen as the only ring hetero atom, e.g. fungichromin',
 'Heterocyclic compounds having oxygen as the only ring hetero atom, e.g. fungichromin having six-membered rings with one oxygen as the only ring hetero atom',
 'Heterocyclic compounds having oxygen as the only ring hetero atom, e.g. fungichromin having six-membered rings with one oxygen as the only ring hetero atom condensed with carbocyclic rings, e.g. cannabinols, methantheline',
 'HUMAN NECESSITIES',
 'MEDICAL OR VETERINARY SCIENCE; HYGIENE',
 'PREPARATIONS FOR MEDICAL, DENTAL, OR TOILET PURPOSES',
 'Medicinal preparations containing organic active ingredients',
 'Hydroxy compounds, e.g. alcohols; Salts thereof, e.g. alcoholates',
 'Phenols',
 'HUMAN NECESSITIES',
 'MEDICAL OR VETERINARY SCIENCE; HYGIENE',
 'PREPARATIONS FOR MEDICAL, DENTAL, OR TOILET PURPOSES',
 'Medicinal preparations containing organic active ingredients',
 'Acids; Anhydrides, halides or salts thereof, e.g. sulfur acids, imidic, hydrazonic, hydroximic acids',
 'Carboxylic acids, e.g. valproic acid',
 'Carboxylic acids, e.g. valproic acid having an amino group',
 'Carboxylic acids, e.g. valproic acid having an amino group the amino and the carboxyl groups being attached to the same acyclic carbon chain, e.g. gamma-aminobutyric acid [GABA], beta-alanine, epsilon-aminocaproic acid, pantothenic acid',
 'Alpha-aminoacids, e.g. alanine, edetic acids [EDTA]']
 
final_resut_list=[]
for i in range(0,len(currect_list)):
 current_str=currect_list[i]
 for j in range (i+1,len(currect_list)):
    if current_str in currect_list[j] and len(currect_list[j])> len(current_str):
        current_str= currect_list[j]
 if current_str not in final_resut_list:
    final_resut_list.append(current_str)
for res in final_resut_list:
    print(res)

最终输出将是

HUMAN NECESSITIES
MEDICAL OR VETERINARY SCIENCE; HYGIENE
PREPARATIONS FOR MEDICAL, DENTAL, OR TOILET PURPOSES
Medicinal preparations containing organic active ingredients
Heterocyclic compounds having oxygen as the only ring hetero atom, e.g. fungichromin having six-membered rings with one oxygen as the only ring hetero atom condensed with carbocyclic rings, e.g. cannabinols, methantheline
Hydroxy compounds, e.g. alcohols; Salts thereof, e.g. alcoholates
Phenols
Acids; Anhydrides, halides or salts thereof, e.g. sulfur acids, imidic, hydrazonic, hydroximic acids
Carboxylic acids, e.g. valproic acid having an amino group the amino and the carboxyl groups being attached to the same acyclic carbon chain, e.g. gamma-aminobutyric acid [GABA], beta-alanine, epsilon-aminocaproic acid, pantothenic acid
Alpha-aminoacids, e.g. alanine, edetic acids [EDTA]

推荐阅读