+ 1

How to in python?

If result = [name,scores] How can I print the particular name for e specific score?

19th Dec 2021, 3:36 PM
Nisha
13 Respostas
+ 10
print([k for k,v in result.items() if v== specific_score]) If result is a dictionary with key = name and value = score)
19th Dec 2021, 3:52 PM
Oma Falk
Oma Falk - avatar
+ 3
Nisha That's list not dictionary result = [1, 2] #list result = {1, 2} # dictionary result = (1, 2) # tuple
19th Dec 2021, 3:43 PM
AĶ¢J
AĶ¢J - avatar
+ 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])
20th Dec 2021, 7:00 AM
Oma Falk
Oma Falk - avatar
+ 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])
19th Dec 2021, 3:47 PM
David B
+ 1
.items is not a method for lists, maybe show us your code so we can help you better?
19th Dec 2021, 4:41 PM
David B
+ 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])
19th Dec 2021, 5:01 PM
Nisha
+ 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]))
19th Dec 2021, 6:49 PM
David B
+ 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())])
20th Dec 2021, 9:22 PM
Oma Falk
Oma Falk - avatar
+ 1
How to learn
21st Dec 2021, 2:35 AM
Million Facts
0
Oma Falk result.items() showing error for list
19th Dec 2021, 4:35 PM
Nisha
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
19th Dec 2021, 6:43 PM
Lisa
Lisa - avatar
0
ā­Nakul Pasi S/0 Krishna Lal Pasiā­ Don't copy other users answers
20th Dec 2021, 2:05 PM
Lisa
Lisa - avatar
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
20th Dec 2021, 3:58 PM
William Okuro