python dict和函数不工作

时间:2017-03-25 11:47:40

标签: python dictionary

以下是我的文本文件的前20行,我有50K行。

    template<typename Key, typename Info>
    void Sequence<Key,Info>::operator=(const Sequence &x)
    {
        element *ptr=head;
        while(ptr)//deallocating memory
        {
            head=ptr->next;
            delete[] ptr;
            ptr=head;
        }
        x.element *ptr2;// <- it doesn't work - how can I make a ptr for structure of class x here?
// rest of code 
.
.
.
//



    }

我这样读,

prov_type|prov_type_desc
0|FAMILY PRACTICE/CLINIC
1|FAMILY PRACTICE
2|ALLERGIST
3|DERMATOLOGIST
4|INTERNIST
5|NEUROLOGIST
6|NEUROSURGEON
7|OB/GYN
8|OPTHAMOLOGIST
9|ORTHOPEDIST
10|OTOLARYNGOLOGIST
11|PATHOLOGIST
12|PEDIATRICIAN
13|PLASTIC SURGEON
14|COLON AND RECTAL SURGERY
15|PSYCHIATRIST
16|RADIOLOGIST
17|SURGEON
18|THORACIC SURGEON
19|UROLOGIST
20|ANESTHESIOLOGIST

我写了一本字典以匹配专业,这是字典。

ovations = pd.read_csv("Ovations.txt",sep='|',dtype=object)
ovations.rename(columns={'prov_type_desc':'specialty'},inplace=True)

我写了一个这样的函数来获取密钥的值,

options = {'FAMILYPRACTICESELF-REFFERAL' : 'FAMILY PRACTICE',
'FAMILYPRACTICESPECIALIST' : 'FAMILY PRACTICE',
'FAMILYPRACTICE/CLINIC' : 'FAMILY PRACTICE',
'GENERALPRACTICE' : 'FAMILY PRACTICE',
'ALLERGY' : 'ALLERGIST',
'ALLERGYANDIMMUNOLOGY' : 'ALLERGIST',
'ALLERGY&IMMUNOLOGY' : 'ALLERGIST',
'ALLERGY/IMMUNOLOGY' : 'ALLERGIST',
'CARDIOLOGY' : 'CARDIOLOGIST',
'CARDIOLOGYGROUP' : 'CARDIOLOGIST',
'CARDIOVASCULARDISEASE' : 'CARDIOLOGIST',
'COLON&RECTALSURGERY' : 'COLON AND RECTAL SURGERY',
'COLON/RECTALSURGERY' : 'COLON AND RECTAL SURGERY',
'COLORECTALSURGERY' : 'COLON AND RECTAL SURGERY',
'DERMATOLOGYGROUP' : 'DERMATOLOGIST',
'DERMATOLOGY' : 'DERMATOLOGIST',
'ENDOCRINOLOGY,DIABETES,ANDMETABOLISM' : 'ENDOCRINOLOGIST',
'ENDOCRINOLOGY' : 'ENDOCRINOLOGIST',
'ENDODONDIST' : 'ENDODONTICS',
'GASTROENTEROLOGY' : 'GASTROENTEROLOGIST',
'GASTROENTEROLOGYGROUP' : 'GASTROENTEROLOGIST',
'GENETICCOUNSELOR' : 'GENETIC TESTING/COUNSELING CENTER',
'GENETICS,CLINICAL(MD)' : 'GENETIC TESTING/COUNSELING CENTER',
'GENETICS,CLINICALMOLECULAR' : 'GENETIC TESTING/COUNSELING CENTER',
'HEMATOLOGYONCOLOGY' : 'HEMATOLOGY/ONCOLOGY',
'HEMATOLOGIST' : 'HEMATOLOGY/ONCOLOGY',
'HEMATOLOGY' : 'HEMATOLOGY/ONCOLOGY',
'HEMATOLOGYGROUP' : 'HEMATOLOGY/ONCOLOGY',
'HEMATOLOGY-ONCOLOGY' : 'HEMATOLOGY/ONCOLOGY',
'HEMATOLOGY-ONCOLOGYGROUP' : 'HEMATOLOGY/ONCOLOGY',
'HOSPICE&PALLATIVEMED' : 'HOSPICE',
'HOSPITALOP/LAB/XRAY' : 'HOSPITAL',
'HOSPITALIST' : 'HOSPITAL',
'INFECTIOUSDISEASEMEDICINE' : 'INFECTIOUS DISEASE',
'INTERNALMED' : 'INTERNAL MEDICINE',
'INTERNALMEDICINESPECIALIST' : 'INTERNAL MEDICINE',
'INTERNIST' : 'INTERNAL MEDICINE',
'INFECTIOUSDISEASESEPCIALIST' : 'INFECTIOUS DISEASE',
'NEPHROLOGY' : 'NEPHROLOGIST',
'NEUROLOGY' : 'NEUROLOGIST',
'OBSTETRICS' : 'OBSTETRICS AND GYNECOLOGY',
'OBSTETRICS&GYNECOLOGY' : 'OBSTETRICS AND GYNECOLOGY',
'OBSTETRICS/GYNECOLOGY' : 'OBSTETRICS AND GYNECOLOGY',
'OB/GYNGROUP' : 'OBSTETRICS AND GYNECOLOGY',
'OBSTETRICSGYNECOLOGY' : 'OBSTETRICS AND GYNECOLOGY',
'OBGYNECOLOGISTSPECIALTY' : 'OBSTETRICS AND GYNECOLOGY',
'OB/GYN' : 'OBSTETRICS AND GYNECOLOGY',
'OB/GYNSELFREFCAP' : 'OBSTETRICS AND GYNECOLOGY',
'GYNECOLOGY' : 'OBSTETRICS AND GYNECOLOGY',
'ONCOLOGY' : 'ONCOLOGIST',
'GYNECOLOGICONCOLOGY' : 'ONCOLOGIST',
'GYNECOLOGICALONCOLOGY' : 'ONCOLOGIST',
'GYNECOLOGICAL/ONCOLOGY' : 'ONCOLOGIST',
'OPHTHALMOLOGY' : 'OPTHAMOLOGIST',
'OTOLARYNGOLOGY' : 'OTOLARYNGOLOGIST',
'OTOLARYNGOLOGY(ENT)' : 'OTOLARYNGOLOGIST',
'PATHOLOGY' : 'PATHOLOGIST',
'PATHOLOGYSERVICES' : 'PATHOLOGIST',
'PATHOLOGY,ANATOMIC' : 'PATHOLOGIST',
'CYTOPATHOLOGY' : 'PATHOLOGIST',
'PATHOLOGY,ANATOMICAL&CLINICAL' : 'PATHOLOGIST',
'PATHOLOGY,BLOOD BANKING/TRANSFUSIONMED' : 'PATHOLOGIST',
'PATHOLOGY,CLINICAL' : 'PATHOLOGIST',
'PATHOLOGY,CYTOPATHOLOGY' : 'PATHOLOGIST',
'PATHOLOGY,DERMATOPATHOLOGY' : 'PATHOLOGIST',
'PATHOLOGY,HEMATOLOGY' : 'PATHOLOGIST',
'PATHOLOGY,IMMUNOPATHOLOGY' : 'PATHOLOGIST',
'PATHOLOGY,NEUROPATHOLOGY' : 'PATHOLOGIST',
'DERMATOLOGY-DERMATOPATHOLOGY' : 'PATHOLOGIST',
'DERMATOPATHOLOGY' : 'PATHOLOGIST',
'PEDIATRICMEDICINE' : 'PEDIATRICIAN',
'PEDIATRSELFREFCAP' : 'PEDIATRICIAN',
'PEDIATRICSPECIALTYIALIST' : 'PEDIATRICIAN',
'PEDIATRICS' : 'PEDIATRICIAN',
'PEDIATRICSSPECIALTYIALIST' : 'PEDIATRICIAN',
'PLASTICANDRECONSTRUCTIVESURGERY' : 'PLASTIC SURGEON',
'PLASTICSURGERY' : 'PLASTIC SURGEON',
'PLASTICSURGERYWITHINTHEHEAD&NECK' : 'PLASTIC SURGEON',
'PSYCHIATRY' : 'PSYCHIATRIST'}

它没有按预期工作,这可能是什么问题?

以下是我如何获得,它应该返回非匹配键的其他键def key_in_dic(p): return next((options[x] for x in p if x in options), 'Other') ovations['specialty_adj'] = key_in_dic(list(ovations['specialty'])) ,但事实并非如此。

enter image description here 谢谢。

2 个答案:

答案 0 :(得分:1)

使用def key_in_dict(p): return (options.get(x, default='Other') for x in p) 方法指定未找到密钥时的默认值。

Rx.Observable.from([1, 2, 3, 4, 5])
  .zip(Rx.Observable.timer(0, 2000), x => x)
  .subscribe(x => console.log(x));

答案 1 :(得分:1)

正如Barmar所说,您可以使用get字典方法。我认为以下内容可以为您提供所需内容:

ovations["specialty_adj"] = ovations["specialty"].apply(lambda x: options.get(x, "Other"))