Finding maximum value in a dictionary Python [duplicate] - python-3.x

This question already has answers here:
Getting the Max Value from a Dictionary [duplicate]
(3 answers)
Closed 3 years ago.
the input I have given is
BatmanA,14
BatmanB,199
BatmanC,74
BatmanD,15
BatmanE,9
and the output i expect is the highest value and i get something else this is my code below i have tried other methods too pls help thanks.
N = int(input("Enter the number of batsman : "))
d = {}
for i in range(0,N):
batsman = input("enter the batsman values " ).split(',')
d[batsman[0]] = batsman[1]
v = list(d.values())
k = list(d.keys())
print(k[v.index(max(v))])

d[max(d, key=d.get)]
See Getting key with maximum value in dictionary? for more discussion.

Related

Interpret the meaning of one line of Python3 code [duplicate]

This question already has answers here:
is_max = s == s.max() | How should I read this?
(5 answers)
Closed last month.
I am a beginner for Python3. Here I have one question to bother you. In the function below:
def highlight_min(s):
'''
highlight the minimum in a Series red.
'''
is_max = s == s.min()
return ['background-color: red' if v else '' for v in is_max]
What does is_max = s == s.min() mean?
Thank you.
s appears to be an array of some sort. The result of is_max in this operation will be a boolean type array the same size as s. Any element in s that is the same as the minimum of the array s will have the value True, else it will have the value False .
The following line is a loop through is_max that returns a python list. if v queries if an element is True in is_max and assigns the string 'background-color: red' if it is or an empty string if not.
I suspect that the function was copied from the original probably called 'highlight_max' and is_max should really be is_min.

Python 3 : List of odd numbers [duplicate]

This question already has answers here:
Python "for i in" + variable
(3 answers)
Closed 2 years ago.
I'm trying to return a list of odd numbers below 15 by using a python user-defined function
def oddnos(n):
mylist = []
for num in n:
if num % 2 != 0:
mylist.append(num)
return mylist
print(oddnos(15))
But I'm getting this error :
TypeError: 'int' object is not iterable
I didn't understand what exactly this means, please help me find my mistake
Because 15 is an integer, not a list you need to send the list as an input something like range(0,15) which will give all numbers between 0 and 15.
def oddnos(n):
mylist = []
for num in n:
if num % 2 != 0:
mylist.append(num)
return mylist
print(oddnos(range(0,15)))
When you're passing values to the function oddnos, you're not passing a list of values till 15, rather only number 15. So the error tells you, you're passing an int and not a list, hence not iterable.
Try to use range() function directly in the for loop, pass your number limit to the oddnos function.

Count multiple occurrence of all string elements in a list? [duplicate]

This question already has answers here:
How do I count the occurrences of a list item?
(29 answers)
How do I count the occurrence of a certain item in an ndarray?
(32 answers)
Closed 2 years ago.
How can I get a count of all the string elements from the given list?
Var1 = ['2019-11-22', '2019-11-22', '2019-11-20']
Desired Output:
2019-11-22 : count(2)
2019-11-20 : count(1)
That's what a counter is for.
from collections import Counter
Var1 = ['2019-11-22', '2019-11-22', '2019-11-20']
counts = Counter(Var1)
from collections import Counter
var1 = ['2019-11-22', '2019-11-22', '2019-11-20']
for i, c in Counter(var1).items():
print(f"{i}: count({c})")

check if only one of the numbers is odd or even from user input list [duplicate]

This question already has answers here:
How to compare multiple variables to the same value?
(7 answers)
Closed 5 years ago.
I am learning python and struggling with this so please help. out of 4 different user input integers, I want to print 'False' if there's a single odd or an even integer in the list. say if user inputs 1,1,2,2, = true...
but 1,1,1,2, or 1,2,2,2 = false
my attempt was to check if only one in the list divisible by two (or not) to return false.
a = int(input())
b = int(input())
c = int(input())
d = int(input())
if a or b or c or d % 2 == 0:
print ('FALSE')
elif a or b or c or d % 2 != 0:
print('FALSE')
else:
print('TRUE')
please help a guide to clean my mess or understanding .. thank you!
You're effectively testing whether there's an odd number of evens among four numbers. If there's an odd number of evens, then the sum of the four values will be odd. You can therefore check the sum as following:
a = int(input())
b = int(input())
c = int(input())
d = int(input())
if (a + b + c + d) % 2 == 0:
print ('TRUE')
else:
print('FALSE')

How to check condition and choose return [duplicate]

This question already has answers here:
Does Python have a ternary conditional operator?
(31 answers)
Closed 7 years ago.
In C# I would do this if I wanted the word 'Many' to display if the count was 10 or more:
int count = 10;
var result = count < 10 ? count : "Many";
How do you do this in python?
Simply use print function and if-else statement:
>>> count =10
>>> print('many') if count>=10 else ''
many

Resources