Skip to content

Exercise N62 - There is an unnecessary code, Exercise N80 - IndexError #215

Closed
@Artur-Arstamyan

Description

@Artur-Arstamyan

Exercise 62 - Considering two arrays with shape (1,3) and (3,1), how to compute their sum using an iterator?
Code

a = np.random.randint(0, 10, (1, 3))
b = np.random.randint(0, 10, (3, 1))
it = np.nditer([a,b,None])

for x,y,z in it: 
    z = x + y

print(it.operands[2])

Unnecessary part

for x,y,z in it: 
    z = x + y

After it = np.nditer([a,b,None]) it.operands[2] already has that value.

Exercise N80
Current code that gives error

r = [slice(start,stop) for start,stop in zip(R_start,R_stop)]
z = [slice(start,stop) for start,stop in zip(Z_start,Z_stop)]
R[r] = Z[z]

Output

IndexError: only integers, slices (`:`), ellipsis (`...`), numpy.newaxis (`None`) and integer or boolean arrays are valid indices

Solution - Use tuples

r = tuple(slice(start,stop) for start,stop in zip(R_start,R_stop))
z = tuple(slice(start,stop) for start,stop in zip(Z_start,Z_stop))

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions