Last one python programming problem. I coded this question but the system says\"
ID: 666651 • Letter: L
Question
Last one python programming problem. I coded this question but the system says"Make sure you removed all debugging print statements." The following is my code so far.
def uniqueValues(aDict):
result = {} #to hold without duplicates data
duplicates = {} # to hold duplicates
for key,value in aDict.items(): #iterating dictionary
if value not in result.values():
result[key] = value #storinf coorect values
else:
duplicates[key] = value #storing duplicate values
result = { k : v for k,v in result.iteritems() if v not in duplicates.values() } #removing even dupliacate traces
print result #printing data
PROBLEM 4
(10 points possible)
You are given a dictionary aDict that maps integer keys to integer values. Write a Python function that returns a list of keys in aDict that map to dictionary values that appear exactly once in aDict.
This function takes in a dictionary and returns a list.
Return the list of keys in increasing order.
If aDict does not contain any values appearing exactly once, return an empty list.
If aDict is empty, return an empty list.
Hint: You may want to create another dictionary as an intermediate data structure.
For example:
If aDict = {1: 1, 3: 2, 6: 0, 7: 0, 8: 4, 10: 0} then your function should return [1, 3, 8]
If aDict = {1: 1, 2: 1, 3: 1} then your function should return []
Paste your entire function, including the definition, in the box below. Do not leave any debugging print statements.
Explanation / Answer
def uniqueValues(aDict):
'''
aDict: a dictionary
returns: a sorted list of keys that map to unique aDict values, empty list if none
'''
# Your code here
tmp = {}
result = []
for value in aDict.values():
if(value in tmp.keys()): tmp[value] += 1
else: tmp[value] = 1
for key in aDict.keys():
if(tmp[aDict[key]] == 1): result.append(key)
return sorted(result)
4.
def uniqueValues(aDict):
'''
aDict: a dictionary
returns: a sorted list of keys that map to unique aDict values, empty list if none
'''
dict = {} # dictionary for unique values and occurence
keyL = aDict.keys()
valueL = aDict.values()
newL = [] # list for unique values
if len(set(valueL)) == len(valueL):
keyL.sort()
return keyL
else:
for key in aDict.keys():
dict[aDict[key]] = dict.get(aDict[key], 0) + 1
print dict
for key, value in dict.items():
if value == 1:
newL.append(key)
for key, value in aDict.items():
if value not in newL:
keyL.remove(key)
keyL.sort()
return keyL
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.