python - 从列表中删除所有可重复的值以及其他值中的值,Python
问题描述
我的列表包含句子。有些句子可以重复,有些句子在其他句子中。例如:
- '杂环化合物'
- '具有氧的杂环化合物'
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)
但我不知道如何删除其他值内的值。请帮我解决这个问题!
解决方案
您可以在列表上创建 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]
推荐阅读
- ruby-on-rails - 如何让 Sendgrid Inbound Parse Webhook 在生产中的 rails 应用程序中工作?
- python - 将每个实例掩码合并回原始图像 Python
- node.js - 如何在构建期间创建带有转储的 postgreSQL 数据库以在 gitlab-CI 中启动单元测试?
- ios - 如何使用带有 react-native-camera 的默认 iOS 相机控件
- swift - 路径中的 SwiftUI UIImage 不可见
- scala - Doobie for Scala / Play 框架中的事务
- loops - TestCafe 循环遍历元素并检查 Set 中是否存在类
- javascript - 为什么当我更新商品数量时,循环遍历购物车商品并返回商品数量的 Vuex getter 不更新?
- python - 使用布尔数组的逐项总和作为包含的“或”
- git - GIT:获取未合并文件的哈希