简体   繁体   中英

How to remove some values from a python dictionary

I have this python dictionary myDict :

{'Age': {0: '39'}, 'DailyRate': {0: '903'}, 'DistanceFromHome': {0: '2'}, 'EnvironmentSatisfaction': {0: '1'}, 'HourlyRate': {0: '41'}, 'JobInvolvement': {0: '4'}, 'JobLevel': {0: '3'}, 'JobSatisfaction': {0: '3'}, 'MonthlyIncome': {0: '7880'}, 'MonthlyRate': {0: '2560'}, 'NumCompaniesWorked': {0: '0'}, 'PercentSalaryHike': {0: '18'}, 'RelationshipSatisfaction': {0: '4'}, 'StandardHours': {0: '80'}, 'TotalWorkingYears': {0: '9'}, 'TrainingTimesLastYear': {0: '3'}, 'YearsAtCompany': {0: '8'}, 'YearsInCurrentRole': {0: '7'}, 'YearsSinceLastPromotion': {0: '0'}, 'YearsWithCurrManager': {0: '7'}, 'MaritalStatus_': {0: '2'}, 'JobRole_': {0: '7'}, 'Gender_': {0: '1'}, 'EducationField_': {0: '1'}, 'Department_': {0: '2'}, 'BusinessTravel_': {0: '2'}, 'OverTime_': {0: '1'}, 'Over18_': {0: '1'}}

As you can see, if i get a one from above sample as below,

{'Age': {0: '39'}}

There is an additional 0 in front of the value 39 . And this zero presents in every key-value pair.

How can I get rid of this 0 , so it looks like this:

{'Age': '39'}

I tried this method, but it removes the whole key instead of the 0 :

map(myDict.pop, ['Age',''])

Can someone please help me?

You can use dictionary comprehension to solve this issue. Try doing:

new_dict = {key: value[0] for key, value in old_dict.items()}

Here, you iterate through each key, value pair in the dictiory and assign the key of the new dictionary to the key of the old dictionary. But the value becomes the 0th key value of the dictionary inside the dictionary.

For an example, the key starts at 'Age' , so the first key of the new dictionary is 'Age' . The value however is {0: '39'}[0] which is '39' . So the first element of the dictionary is 'Age': '39'

you can read it by the following code:

dict= {'Age': {0: '39'}, 'DailyRate': {0: '903'}, 'DistanceFromHome': {0: '2'}, 'EnvironmentSatisfaction': {0: '1'}, 'HourlyRate': {0: '41'}, 'JobInvolvement': {0: '4'}, 'JobLevel': {0: '3'}, 'JobSatisfaction': {0: '3'}, 'MonthlyIncome': {0: '7880'}, 'MonthlyRate': {0: '2560'}, 'NumCompaniesWorked': {0: '0'}, 'PercentSalaryHike': {0: '18'}, 'RelationshipSatisfaction': {0: '4'}, 'StandardHours': {0: '80'}, 'TotalWorkingYears': {0: '9'}, 'TrainingTimesLastYear': {0: '3'}, 'YearsAtCompany': {0: '8'}, 'YearsInCurrentRole': {0: '7'}, 'YearsSinceLastPromotion': {0: '0'}, 'YearsWithCurrManager': {0: '7'}, 'MaritalStatus_': {0: '2'}, 'JobRole_': {0: '7'}, 'Gender_': {0: '1'}, 'EducationField_': {0: '1'}, 'Department_': {0: '2'}, 'BusinessTravel_': {0: '2'}, 'OverTime_': {0: '1'}, 'Over18_': {0: '1'}}
print(dict['Age'][0])

to convert just do:

age = dict['Age'][0]
del(dict['Age'])
dict.update({"Age":age})

you will get the following dic as result:

{'DailyRate': {0: '903'}, 'DistanceFromHome': {0: '2'}, 'EnvironmentSatisfaction': {0: '1'}, 'HourlyRate': {0: '41'}, 'JobInvolvement': {0: '4'}, 'JobLevel': {0: '3'}, 'JobSatisfaction': {0: '3'}, 'MonthlyIncome': {0: '7880'}, 'MonthlyRate': {0: '2560'}, 'NumCompaniesWorked': {0: '0'}, 'PercentSalaryHike': {0: '18'}, 'RelationshipSatisfaction': {0: '4'}, 'StandardHours': {0: '80'}, 'TotalWorkingYears': {0: '9'}, 'TrainingTimesLastYear': {0: '3'}, 'YearsAtCompany': {0: '8'}, 'YearsInCurrentRole': {0: '7'}, 'YearsSinceLastPromotion': {0: '0'}, 'YearsWithCurrManager': {0: '7'}, 'MaritalStatus_': {0: '2'}, 'JobRole_': {0: '7'}, 'Gender_': {0: '1'}, 'EducationField_': {0: '1'}, 'Department_': {0: '2'}, 'BusinessTravel_': {0: '2'}, 'OverTime_': {0: '1'}, 'Over18_': {0: '1'}, 'Age': '39'}

Maybe try the following code:

keys = myDict.keys()
valuesWithZero = myDict.values()

valuesNoZero = []
for item in valuesWithZero:
    value_iterator = iter(item.values()) #to make dict_values obj iterable
    first_value = next(value_iterator) #obtaining first value
    valuesNoZero.append(first_value) #adding to new list
    

newDict = dict(zip(keys, valuesNoZero))  #combining keys arr and values arr
print(newDict)

# should output: {'Age': '39', 'DailyRate': '903', 'DistanceFromHome': '2', 'EnvironmentSatisfaction': '1', 'HourlyRate': '41', 'JobInvolvement': '4', 'JobLevel': '3', 'JobSatisfaction': '3', 'MonthlyIncome': '7880', 'MonthlyRate': '2560', 'NumCompaniesWorked': '0', 'PercentSalaryHike': '18', 'RelationshipSatisfaction': '4', 'StandardHours': '80', 'TotalWorkingYears': '9', 'TrainingTimesLastYear': '3', 'YearsAtCompany': '8', 'YearsInCurrentRole': '7', 'YearsSinceLastPromotion': '0', 'YearsWithCurrManager': '7', 'MaritalStatus_': '2', 'JobRole_': '7', 'Gender_': '1', 'EducationField_': '1', 'Department_': '2', 'BusinessTravel_': '2', 'OverTime_': '1', 'Over18_': '1'}

Same process as the accepted answer but uses some of Python's functional features.

import operator
zero = operator.itemgetter(0)

newdict = dict(zip(myDict,map(zero, myDict.values())))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM