I want to use a variable as an argument for count = Counter(list).most_common(x). But receive this error:
TypeError: unorderable types: str() >= int()
My code works fine when there is a constant in place for x like: count = Counter(list).most_common(2)
Any help would be appreciated.
Thanks
Related
I have a numpy array as follows:
board = np.arange(9).reshape(3, 3)
I would like to replace a value in the array, for example:
board[1, 2] = 'x'.
But as I understand, I cannot do this as the array is in type int and what I am trying to replace it with is a string. When I run this, I get the error:
ValueError: invalid literal for int() with base 10: 'x'
I tried setting the dtype of the array to str:
board = np.arange(9, dtype=str).reshape(3, 3)
But this gives me the error:
ValueError: no fill-function for data-type.
Thanks
In numpy any non-numeric dtype is of type object.
This will work:
board = np.arange(9, dtype=object).reshape(3, 3)
board[1, 2] = 'x'
Cheers.
I am receiving the TypeError: 'int' object is not callable at last Line "print(max(lenght1)):
Please suggest
l=[int(x) for x in "10,22,9,33,21,50,41,60,80".split(sep = ",")]
print(l)
length1 = [1 for i in range(len(l))]
for i in range(len(l)):
max = l[i]
for j in range(i+1,len(l)):
if l[j]>max:
max=l[j]
length1[i]=length1[i]+1
print(max(length1))
This happens because you used max as a variable name, which shadowed the built-in max function. It's preferable to not use built-in names of functions, types etc. as variable names.
Below is the following code:
a = list(map(int, input().split()))
b = list(map(int, input().split()))
The above code works fine but the below code throws error.Can you please explain?
l = input()
i = int(l.split())
m = input()
j = int(m.split())
The type error is : int() argument must be a string, a bytes-like object or a number, not 'list.
I wanted to figure out how the above part of code works and not below one.
split returns an array of strings, and as you've seen, int can't take that as an argument. map takes a function reference (int in this case) and applies it to each member of an iterable, thus converting all the strings to ints.
I have made a program in Python 3, which tests whether a number is a palindrome. Obviously it is not done, but when I try to run it:
#!/usr/bin/env python
def testforpalin():
i = 101
lop = list(str(i))
print(lop)
len(lop)
if lop[0] == lop[len-1]:
print("hi")
testforpalin()
TypeError: unsupported operand type(s) for -: 'builtin_function_or_method' and 'int'
I get that error. How do I fix this?
in the line
if lop[0] == lop[len-1]:
you have len-1 where len is the function that gives you the length of the string (hence the error - you are trying to do subtraction where one of the values is len which is a "builtin_function_or_method"). you probably mean lop[len(lop)-1] (which would work), but it would be simpler to do:
if lop[0] == lop[-1]:
because [-1] gives you the last element in a string or array (and [-2] gives you next-to-last, etc).
#integers to be input
n = input('Enter \"n\" trials')
x = input('Enter \"x\" number of succeses')
p = input('Enter the probability \"p\" of success on a single trial')
#Probability Distribution function
def probDist(n, x, p):
q = (1-p)**(n-x)
numerator = math.factorial(n);
denominator = math.factorial(x)* math.factorial(n-x);
C = numerator / denominator;
answer = C*p**x*q;
return answer
# Does this have to come after I define the function? Or does this matter in Python
# Also this part just doesn't work.
dist = probDist(n, x, p);
print(dist);
Here's the error that I get after I run and I input all the values.
Traceback (most recent call last):
line 17, in <module>
dist = probDist(n, x, p);
line 9, in probDist
q = (1-p)**(n-x)
TypeError: unsupported operand type(s) for -: 'int' and 'str'
In Python 3.x, input always returns a string, without applying eval the the user input. Python 2.x input does eval, but that's rarely what you want. If you want an integer, use int(input(...)) and if you want a float (not quite the same as a real number, as it has only limited range!), use float(input). (You should propably catch ValueError to handle the cases where the input is not appropiate; if this is for exercise/education, it's propably okay to leave error handling out for now.)
Does this have to come after I define the function?
Yes.
q = (1-p)**(n-x)
TypeError: unsupported operand type(s) for -: 'int' and 'str'
You have two - operations. One of those two is a mixture of int and str data.
Let's go through each operand.
1 - int
p - result of input(), and therefore a string
n - result of input(), and therefore a string
x - result of input(), and therefore a string
You probably want to do something to convert the results of input() to a proper floating-point value. float() works well for this.