I'm trying to compare sum of first and last element in a list, second to second last, and so on.
The list has even number of numbers.
But when I call the max() function on a list, it gives me an error. Help me
li = [12,31,51,72,93,11,132,151,172,144]
sum1 = []
for i in range(len(li)):
sum1.append(li[i] + li[len(li)-1-i])
print(sum1)
print(max(sum1))
and the error is:
TypeError Traceback (most recent call last)
<ipython-input-34-04d301659610> in <module>
5 sum1.append(li[i] + li[len(li)-1-i])
6 print(sum1)
----> 7 print(max(sum1))
8
TypeError: 'int' object is not callable
it is possible that you have shadowed the built-function, you can use:
print(__builtin__.max(sum1))
in this way you will use the max function that you want
Related
Having issue in the follwing code, it looks like I am facing issue with either list or int operations, so where and how should I do the changes to correct this code:
cube = lambda x:x**3
def fibonacci(n):
fb=[0,1]
for i in range(2,n-1):
fb.append([fibonacci(i-1)+fibonacci(i-2)])
return fb
if __name__ == '__main__':
n = int(input())
print(list(map(cube, fibonacci(n))))
Error:
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-9-761cca8f2b48> in <module>
7 if __name__ == '__main__':
8 n = int(input())
----> 9 print(list(map(cube, fibonacci(n))))
<ipython-input-9-761cca8f2b48> in <lambda>(x)
----> 1 cube = lambda x:x**3
2 def fibonacci(n):
3 fb=[0,1]
4 for i in range(2,n-1):
5 fb.append([fibonacci(i-1)+fibonacci(i-2)])
TypeError: unsupported operand type(s) for ** or pow(): 'list' and 'int'
You should be certain what you want fibonacci() to return. It looks like you want it to return a list since you are passing the result to map(). If so, the recursive calls will also return a list, to this line doesn't really make sense:
fb.append([fibonacci(i-1)+fibonacci(i-2)])
because fibonacci(i-1) and fibonacci(i-2) are also lists, but it looks like you expect an integer here as a result of the addition.
Since you are using a loop in the function and have a seed list, you don't need the recursion. Just index into the list fb:
def fibonacci(n):
fb=[0,1]
for i in range(2,n-1):
fb.append(fb[i-1] + fb[i-2])
return fb
n = 9
fibonacci(n)
# [0, 1, 1, 2, 3, 5, 8, 13]
With that, your cube map() should work.
You might want to replace
fb.append([fibonacci(i-1)+fibonacci(i-2)])
with
fb.append(fibonacci(i-1)+fibonacci(i-2))
Right now, you are mapping a function that takes a scalar as input onto a list containing both numbers like 0 and 1 as well as lists, like [1], for example.
Note: I know python is strongly typed and we wont concatenate 2 different types. but accidentally i have tried,
I got different result,
a = []
>>>a=a+1 #it gives me expected concatenate error
TypeError: can only concatenate list (not "int") to list
>>>a+=1 #but this wasn't
TypeError: 'int' object is not iterable
This is because operator + and operator += is not same magic function implement in list class.
__iadd__() ## Map to += Operator
__add__() ## Map to + Operator
That means you trigger different magic function of list object.
From what I gather while doing experimentation is that.
a = a + 1 is concatenating a list and an integer, but as we know + operator or concatenation requires two lists, hence the error we get is
TypeError: can only concatenate list (not "range") to list, as we can see below
In [43]: [] + 1
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-43-67dee89361ae> in <module>
----> 1 [] + 1
TypeError: can only concatenate list (not "int") to list
In [47]: [] + []
Out[47]: []
In a += 1, we internally call a.extend(1) as per the docs: https://docs.python.org/3/library/stdtypes.html#mutable-sequence-types
s.extend(t) or s += t .
extends s with the contents of t (for the most part the same as s[len(s):len(s)] = t)
Also from the docs at: https://docs.python.org/3/tutorial/datastructures.html
list.extend(iterable)
Extend the list by appending all the items from the iterable. Equivalent to a[len(a):] = iterable.
So 1 is being treated as an iterable, but since int is not an iterable, we get the error TypeError: 'int' object is not iterable as we can see below
In [49]: a = []
In [50]: a.extend(1)
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-50-4671950943e4> in <module>
----> 1 a.extend(1)
TypeError: 'int' object is not iterable
As stated the two operators do not map to the same magic function. And the two have different implementations. This has some interesting consequences. the normal plus (+ or __add__) expects another list to concatenate whereas in-place add (+= or __iadd__) is more allowing and can take any iterator and make it into a list before adding:
a = []
a = a + range(2)
TypeError: can only concatenate list (not "range") to list
a += range(2)
[0, 1]
I don't know why this is though.
def list_gen(a,b,c,d):
print(a,b,c,d)
l=[]
for i in range(a,b):
for j in range(c,d):
l.append(d[i,j])
return l
When I pass the arguments to the function list_gen(0,3,0,3) I am getting below error:
TypeError Traceback (most recent call last)
<ipython-input-51-ac343943d448> in <module>()
----> 1 list_gen(0,3,0,3)
<ipython-input-49-afc3d3a347a9> in list_gen(a, b, c, d)
4 for i in range(a,b):
5 for j in range(c,d):
----> 6 l.append(d[i,j])
7 return l
TypeError: 'int' object is not subscriptable
But this code works without any issues. Can anyone tell what is the error here ?
for i in range(0,3):
for j in range(0,3):
print(d[i,j])
Apparently you have a global variable d containing a dict that you want to access. But you used d as the name of a parameter of the function, so that hides the global name. When you use d[i,j], it tries to index the number 3, which is not possible.
The solution is to use a different variable for the function parameter.
def list_gen(a,b,c,e):
print(a,b,c,e)
l=[]
for i in range(a,b):
for j in range(c,e):
l.append(d[i,j])
return l
You'll run into problems like this less often if you use more verbose, meaningful variable names than d.
The parameter in range has to be integer and since the for j in range(c,d): didn't throw error, it mean d is int and you cannot reference int using d[i,j] in the line l.append(d[i,j]) . You probably have a variable named d whose scope is active during the print statement.
I made a code to make the summary of the numbers of a matrix but i get this error TypeError: 'range' object is not callable and i don't know why
Here is my code:
print ('The summary of the positive and negative numbers of a matrix')
A=[[0 for i in range (col)] for j in range (fil)]
fil=int(input('Number of columns'))
col=int(input('Number of rows'))
auxp=0
auxn=0
for i in range (fil):
for j in range (col):
A[i][j]=int(input('Numbers of the matrix'))
for i in range (fil)(col):
for j in range (fil):
if (A[i][j]>0):
auxp=aup+A[i][j]
else:
if (A[i][j]<0):
auxn=auxn+A[i][j]
print ('The summary of the positive numbers is ',auxp)
print ('The summary of the negative numbers is ',auxn)
TypeError Traceback (most recent call last)
<ipython-input-16-83bec8a1085e> in <module>
9 for j in range (col):
10 A[i][j]=int(input('Numbers of the matrix'))
---> 11 for i in range (fil)(col):
12 for j in range (fil):
13 if (A[i][j]>0):
TypeError: 'range' object is not callable
I think that you should be iterating over the rows and columns of the matrix as your code does in the first for loop, i.e. like this:
for i in range(fil):
for j in range(col):
if A[i][j] > 0:
...
Your code incorrect because it is attempting to call the range object:
>>> range(fil)
range(0, 10)
>>> type(range(fil))
<class 'range'>
>>> range(fil)(col)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'range' object is not callable
So the call to range(fil) creates a new range object, and then Python attempts to call that object as though it were a function, passing col to it as an argument.
Perhaps you should read up on functions and function calls to better understand how functions work in Python.
You probably want to change the line
for i in range (fil)(col):
to just
for i in range (fil):
The way you had it, it was a function call on the range object, and as the error message says, the result of calling range() cannot be used as a callable/function.
I'm python user and I'm evaluate combination in python.
For two integers m,n, nCm=n!/(m!*(n-m)!)
So, I defined factorial and combination function.
factorial function is working, but combination function does not working.
What is the error?
1> Factorial Function
def factorial(a):
f=1
for i in range(1,a+1):
f=f*i
print(f)
2> Combination Function
def Combination(n,m):
fn=factorial(n)
fm=factorial(m)
fnm=factorial(n-m)
ncm=factorial(n)/(factorial(m)*factorial(n-m))
print(ncm)
In factorial function, For example, factorial(4)=24 is working in python.
But, In combination function,
When I typing Combination(4,2),
24
2
2
24
2
2
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-18-daae2b10838c> in <module>()
----> 1 Combination(4,2)
<ipython-input-17-76c6e425ad35> in Combination(n, m)
3 fm=factorial(m)
4 fnm=factorial(n-m)
----> 5 ncm=factorial(n)/(factorial(m)*factorial(n-m))
6 print(ncm)
7
TypeError: unsupported operand type(s) for *: 'NoneType' and 'NoneType'
What's the matter in my coding?
Instead of printing the output of a function, you should return the result.
def factorial(a):
f=1
for i in range(1,a+1):
f=f*i
return f
def combination(n,m):
return = factorial(n)/(factorial(m)*factorial(n-m))
print combination(4,2)
One more remark: after calculating factorial(n), it will be stored in your fn variable. You shouldn't calculate it again, but rather take that value and "recycle" it.
def factorial(a):
f=1
for i in range(1,a+1):
f=f*i
return f
def Combination(n,m):
fn=factorial(n)
fm=factorial(m)
fnm=factorial(n-m)
ncm=fn/(fm*fnm)
print(ncm)
Combination(4,2)
In your code there is missing return statement in factorial function.