有效的字符串预处理列表

时间:2017-10-21 16:13:34

标签: python string

我有一个字符串列表。示例字符串,

mesh = "Adrenergic beta-Antagonists/*therapeutic use, Adult, Aged, Aged/*effects, Antihypertensive Agents/*therapeutic use, Blood Glucose/*drug effects, Celiprolol/*therapeutic use, Female, Glucose Tolerance Test, Humans, Hypertension/*drug therapy, Male, Middle Aged, Prospective Studies"

对于字符串中的每个术语(where),术语用逗号分隔,我想删除' /'之后的所有文本。如果没有反斜杠,则什么也不做。

例如,我希望结果字符串是这样的,

 mesh = "Adrenergic beta-Antagonists, Adult, Aged, Aged, Antihypertensive Agents, Blood Glucose, Celiprolol, Female, Glucose Tolerance Test, Humans, Hypertension, Male, Middle Aged, Prospective Studies"

然后我想删除字符串中的任何重复值(例如,Aged)。期望的字符串,

mesh = "Adrenergic beta-Antagonists, Adult, Aged, Antihypertensive Agents, Blood Glucose, Celiprolol, Female, Glucose Tolerance Test, Humans, Hypertension, Male, Middle Aged, Prospective Studies"

我已经编写了适用于一个字符串的代码,但我正在寻找一种更有效的方法来为字符串列表执行此操作:

import string
mesh = "Adrenergic beta-Antagonists/*therapeutic use, Adult, Aged, Aged/*effects, Antihypertensive Agents/*therapeutic use, Blood Glucose/*drug effects, Celiprolol/*therapeutic use, Female, Glucose Tolerance Test, Humans, Hypertension/*drug therapy, Male, Middle Aged, Prospective Studies"
newMesh = []
for each in mesh.split(","):
    newMesh.append(each.split('/', 1)[0].lstrip(' '))
newMesh = list(set(newMesh))
meshString = ",".join(newMesh)
print(meshString)

注意:字符串中术语的顺序无关紧要。

2 个答案:

答案 0 :(得分:4)

您可以使用mesh = "Adrenergic beta-Antagonists/*therapeutic use, Adult, Aged, Aged/*effects, Antihypertensive Agents/*therapeutic use, Blood Glucose/*drug effects, Celiprolol/*therapeutic use, Female, Glucose Tolerance Test, Humans, Hypertension/*drug therapy, Male, Middle Aged, Prospective Studies" import re s = re.sub("\/\*[\w\s]+", '', mesh) final_string = [] for i in re.split(",", s): if i not in final_string: final_string.append(i) new_final_string = ', '.join(final_string) print(new_final_string)

'Adrenergic beta-Antagonists,  Adult,  Aged,  Antihypertensive Agents,  Blood Glucose,  Celiprolol,  Female,  Glucose Tolerance Test,  Humans,  Hypertension,  Male,  Middle Aged,  Prospective Studies'

输出:

 add_filter( 'gettext', 'customizing_product_variation_message', 10, 3 );
 function customizing_product_variation_message( $translated_text, 
 $untranslated_text, $domain )
 {
     if ($untranslated_text == 'Sorry, this product is unavailable. Please choose a different combination.') {
    $translated_text = __( '-type anything you want here, or leave a space- ', $domain );
}
return $translated_text;
}

答案 1 :(得分:0)

使用set函数和import re mesh = "Adrenergic beta-Antagonists/*therapeutic use, Adult, Aged, Aged/*effects, Antihypertensive Agents/*therapeutic use, Blood Glucose/*drug effects, Celiprolol/*therapeutic use, Female, Glucose Tolerance Test, Humans, Hypertension/*drug therapy, Male, Middle Aged, Prospective Studies" word_set = set() result = [] for w in re.sub(r'/[^,]+', '', mesh).split(','): w = w.strip() if w not in word_set: result.append(w) word_set.add(w) result = ', '.join(result) print(result) 对象(用于更快的项目搜索):

Adrenergic beta-Antagonists, Adult, Aged, Antihypertensive Agents, Blood Glucose, Celiprolol, Female, Glucose Tolerance Test, Humans, Hypertension, Male, Middle Aged, Prospective Studies

输出:

    RootScope (AppModule)
       /    \
    LazyA  LazyB
     /        \
  LazyC      LazyC