'long' object has no attribute '__getitem__' Error in a function, which could substract arrays

General discussion for off-topic subjects.
SKY_SHY
Junior Member
Posts: 5
Joined: Sun Sep 18, 2016 9:51 am

'long' object has no attribute '__getitem__' Error in a function, which could substract arrays

Postby SKY_SHY » Sun Sep 18, 2016 9:55 am

I have a problem with my python-code.

I have an array which is called the group:

Syntax: Select all

import numpy as np   
group = np.array(
[[[0,0,1,0,0],
[0,1,0,1,0],
[0,1,0,1,0],
[0,1,0,1,0],
[0,0,1,0,0]],
[[0,0,1,0,0],
[0,1,1,0,0],
[0,0,1,0,0],
[0,0,1,0,0],
[0,1,1,1,0]],
[[0,1,1,0,0],
[0,1,0,1,0],
[0,0,1,0,0],
[0,1,0,0,0],
[0,1,1,1,0]],
[[0,1,1,1,0],
[0,0,1,0,0],
[0,1,1,1,0],
[0,0,0,1,0],
[0,1,1,1,0]],
[[0,0,1,1,0],
[0,1,0,1,0],
[0,1,1,1,0],
[0,0,0,1,0],
[0,0,0,1,0]],
[[0,1,1,1,0],
[0,1,0,0,0],
[0,1,1,1,0],
[0,0,0,1,0],
[0,1,1,1,0]],
[[0,0,0,1,0],
[0,0,1,0,0],
[0,1,1,1,0],
[0,1,0,1,0],
[0,1,1,1,0]],
[[0,1,1,1,0],
[0,0,0,1,0],
[0,0,1,0,0],
[0,1,0,0,0],
[0,1,0,0,0]],
[[0,1,1,1,0],
[0,1,0,1,0],
[0,0,1,0,0],
[0,1,0,1,0],
[0,1,1,1,0]],
[[0,1,1,1,0],
[0,1,0,1,0],
[0,1,1,1,0],
[0,0,1,1,0],
[0,1,0,1,0]]])
>
dataSetSize = group.shape[0]
>
a1=[
[1 1 0 1 0]
[0 0 0 0 0]
[0 0 1 1 1]
[1 0 1 0 1]
[0 0 0 1 0]]

def tile(your_array, your_database, n):
m=your_database[:]
for i in range(n):
newarr=your_array - m[i]
return newarr
print newarr


I need to substract an array your_array (is the a1) from an element of an array of the arrays is called your_database (is the group). I try to specify an index of your_database (is the group) , but python says that your_database[:] and m[i] has no attribute getitem()

Then i wrote this code:

Syntax: Select all

def tile(your_array, your_database, n):
m=your_database.__getitem__()
for i in range(n):
yy=m.__getitem__(i)
newarr=your_array - yy
return newarr
print newarr

But i get an error : 'long' object has no attribute 'getitem'

This function i have written and try to run my code and a mistake occurs at the line #185 . This number of line in my def tile. Thus i haven't called this function, but my code contains it.

1st variant of def tile in tile m=your_database[:] TypeError: 'long' object has no attribute 'getitem'

2nd variant of def tile in tile m=your_database.getitem() AttributeError: 'long' object has no attribute 'getitem'

These function i write to get an substracted array from: a1 - group[i], where i - an element of the big array group .

As i understand right if i specify n - this is numbers of elements of big array (is the group)

Being passed for i'd get my substracted arrays (5*5)

If you have an idea how to substract element of big array *(Element of big array have same size like as array(5*5))* from an array (5*5)
Last edited by L'In20Cible on Sun Sep 18, 2016 10:11 am, edited 1 time in total.
Reason: [code] → [python]
User avatar
iPlayer
Developer
Posts: 590
Joined: Sat Nov 14, 2015 8:37 am
Location: Moscow
Contact:

Re: 'long' object has no attribute '__getitem__' Error in a function, which could substract arrays

Postby iPlayer » Sun Sep 18, 2016 10:59 am

Hi.

I have no problem executing your code and even calling tile function. Tested on Python 2.7 and Python 3.5.

You shouldn't get any errors from the function if you don't execute it (as long as there're no syntax errors).

As I see it, your first variant of tile is fine, except for a few moments.

1.

Syntax: Select all

m=your_database[:]

As I see it, you don't even have to make a shallow copy ([:]) of your_database, as you never alter it in your function, only read it.
2.

Syntax: Select all

return newarr
print newarr

Firstly, your print statement will never print anything, because it's used after return.
Secondly, your for-loop will only have one iteration, because you return immediately in the first iteration.

What your exception means though, is that you passed some long integer to the function as a second parameter.

My full code:

Syntax: Select all

import numpy as np   


group = np.array([
[
[0,0,1,0,0],
[0,1,0,1,0],
[0,1,0,1,0],
[0,1,0,1,0],
[0,0,1,0,0]
],
[
[0,0,1,0,0],
[0,1,1,0,0],
[0,0,1,0,0],
[0,0,1,0,0],
[0,1,1,1,0]
],
[
[0,1,1,0,0],
[0,1,0,1,0],
[0,0,1,0,0],
[0,1,0,0,0],
[0,1,1,1,0]
],
[
[0,1,1,1,0],
[0,0,1,0,0],
[0,1,1,1,0],
[0,0,0,1,0],
[0,1,1,1,0]
],
[
[0,0,1,1,0],
[0,1,0,1,0],
[0,1,1,1,0],
[0,0,0,1,0],
[0,0,0,1,0]
],
[
[0,1,1,1,0],
[0,1,0,0,0],
[0,1,1,1,0],
[0,0,0,1,0],
[0,1,1,1,0]
],
[
[0,0,0,1,0],
[0,0,1,0,0],
[0,1,1,1,0],
[0,1,0,1,0],
[0,1,1,1,0]
],
[
[0,1,1,1,0],
[0,0,0,1,0],
[0,0,1,0,0],
[0,1,0,0,0],
[0,1,0,0,0]
],
[
[0,1,1,1,0],
[0,1,0,1,0],
[0,0,1,0,0],
[0,1,0,1,0],
[0,1,1,1,0]
],
[
[0,1,1,1,0],
[0,1,0,1,0],
[0,1,1,1,0],
[0,0,1,1,0],
[0,1,0,1,0]
]
])

dataSetSize = group.shape[0]

a1 = [
[1, 1, 0, 1, 0],
[0, 0, 0, 0, 0],
[0, 0, 1, 1, 1],
[1, 0, 1, 0, 1],
[0, 0, 0, 1, 0]
]


def tile(your_array, your_database, n):
for i in range(n):
newarr = your_array - your_database[i]
print(newarr)

tile(a1, group, dataSetSize)

Note how I call the function on the last line.

Also, these are Source.Python scripting forums, you might not get the best help on NumPy here...
Image /id/its_iPlayer
My plugins: Map Cycle • Killstreaker • DeadChat • Infinite Jumping • TripMines • AdPurge • Bot Damage • PLRBots • Entity AntiSpam

Hail, Companion. [...] Hands to yourself, sneak thief. Image

Return to “Whatever”

Who is online

Users browsing this forum: No registered users and 6 guests