+ 1
How to in python?
If result = [name,scores] How can I print the particular name for e specific score?
13 odpowiedzi
+ 10
print([k for k,v in result.items() if v== specific_score])
If result is a dictionary with key = name and value = score)
+ 3
Nisha
That's list not dictionary
result = [1, 2] #list
result = {1, 2} # dictionary
result = (1, 2) # tuple
+ 3
Nisha
results = {"tom":10,"anton":14,"bertha":12}
sp_value=int(input())
print([k for k,v in results.items() if v==sp_value])
+ 2
maybe you are trying to access a dictionary the wrong way?
dic = {"John":"30","Jake":"39"}
name = 'John'
if name in dic:
print(name + ", " + dic[name])
+ 1
.items is not a method for lists, maybe show us your code so we can help you better?
+ 1
David B
Here the input is name and score, then i found the second smallest number but i could not understand how to call the name for that number.
n = int(input())
result = []
all_marks = []
all_name =[]
for i in range (n):
name = input()
scores = float(input())
result.append([name,scores])
all_marks.append (scores)
all_name.append (name)
all_marks.sort()
for i in range(0,n):
if(all_marks [0+i]!=all_marks[0]):
min = all_marks[0+i]
print (all_marks[0+i])
if(all_marks [0+i+1]!= all_marks [0+i]):
break
print([i for i,v in result.items() if v==min])
+ 1
Remember that in order to define an empty dictionary you must use curly braces {} and to add an item you would use the statement
my_dic['new key'] = 'new value'
so after you used sort on your list, to find the second minimum you would just access the second item in your list (index 1), because that's where it would be.
After that it would be easy to acces your dictionary key:value pair.
So I tried to follow what you were doing and made this:
n = int(input('N:'))
result = {}
all_marks = []
all_name = []
for i in range(n):
name = input('Name: ')
scores = float(input('Score:'))
result[name] = scores
all_marks.append(scores)
all_name.append(name) # This list will be useless and out of order after sorting your marks
all_marks.sort() # I moved this sort out of your loop so you are not sorting everytime you add an item, just 1 time at the end
key = all_name[1]
print('Second lowest score is {} with {}' .format(key, result[key]))
+ 1
n = int(input())
result = {}
for i in range (n):
name = input()
scores = float(input())
result[name]=scores
print([i for i,v in result.items() if v==min(result.values())])
+ 1
How to learn
0
Oma Falk result.items() showing error for list
0
It would be easiest if you used a dictionary to store your data. Then you can use min() on .values() and iterate through the dictionary to output the corresponding names
0
⭐Nakul Pasi S/0 Krishna Lal Pasi⭐ Don't copy other users answers
0
results = ["Bob",90,"William",80,"Willy",70,"kelly",60,"Keith",50]
scores = int(input("Enter Score: "))
i=len(results)
while range(i):
if scores in results:
print(f"{results[results.index(scores)-1]} Scored: {scores}")
break