Poppy Cooke

Poppy Cooke

1547824910

Iterating through a large array filling it with identically sized, but different, smaller arrays

I have a large array full of zeros simply defined by:

BigArray = np.zeros((100,1000,1000),np.float16)

I then have a 3D volume that I randomly rotate outside of python and each time it is rotated I want to import the file into python and add it to the next bit of the array. I currently have the following code that will do it:

n = 0

while n < 99:
Zaxisangle = randint(0,360)
Yaxisangle = randint(0,360)
Xaxisangle = randint(0,360)

os.system('rotatevol -angles {},{},{} -input {} -output {}'.format(Zaxisangle, Yaxisangle, Xaxisangle,
                                                               MRCfilewithextension, MRCforoutput))
particledata = mrcopen(MRCforoutput)



if n &lt; 10:
    ArtTomo[:, 0:100, (100*(n+1))-100:100*(n+1)] = particledata
    n = n+1
else:
    n = n+1

For the purpose of this example we can simplify it down to the following:

BigArray = np.zeros((100,1000,1000),np.float16)
particledata = np.random.rand(100,100,100)
n = 0
while n < 99:
if n < 10:
ArtTomo[:, 0:100, (100*(n+1))-100:100*(n+1)] = particledata
n = n+1
elif: 10 < n < 20
ArtTomo[:, 100:200, (100*(n+1))-100:100*(n+1)] = particledata
n = n+1
else:
n = n+1

I would then write lots of elif statements for each ‘row’. Because I am iterating through the array with different files I can’t simply fill it with a ‘in range(0,1000,100)’ statement annoyingly.

whilst I can write out all the elif statements I feel as if there must be a more efficient way to write this code I am just not good enough to see it. Could anyone else write this in a nicer way or am I just going to have to write 10 elif statments (i just don’t feel like it is neat code!).

#python #numpy

What is GEEK

Buddha Community

Daisy Rees

1547865748

If I understand correctly, I think what you are writing could just be expressed like:

BigArray = np.zeros((100,1000,1000),np.float16)
particledata = np.random.rand(100,100,100)
n = 0
while n < 99:
    start = 100 * (n // 10)
    ArtTomo[:, start:start+100, (100*(n+1))-100:100*(n+1)] = particledata
    n = n+1

Poppy Cooke

Poppy Cooke

1547824910

Iterating through a large array filling it with identically sized, but different, smaller arrays

I have a large array full of zeros simply defined by:

BigArray = np.zeros((100,1000,1000),np.float16)

I then have a 3D volume that I randomly rotate outside of python and each time it is rotated I want to import the file into python and add it to the next bit of the array. I currently have the following code that will do it:

n = 0

while n < 99:
Zaxisangle = randint(0,360)
Yaxisangle = randint(0,360)
Xaxisangle = randint(0,360)

os.system('rotatevol -angles {},{},{} -input {} -output {}'.format(Zaxisangle, Yaxisangle, Xaxisangle,
                                                               MRCfilewithextension, MRCforoutput))
particledata = mrcopen(MRCforoutput)



if n &lt; 10:
    ArtTomo[:, 0:100, (100*(n+1))-100:100*(n+1)] = particledata
    n = n+1
else:
    n = n+1

For the purpose of this example we can simplify it down to the following:

BigArray = np.zeros((100,1000,1000),np.float16)
particledata = np.random.rand(100,100,100)
n = 0
while n < 99:
if n < 10:
ArtTomo[:, 0:100, (100*(n+1))-100:100*(n+1)] = particledata
n = n+1
elif: 10 < n < 20
ArtTomo[:, 100:200, (100*(n+1))-100:100*(n+1)] = particledata
n = n+1
else:
n = n+1

I would then write lots of elif statements for each ‘row’. Because I am iterating through the array with different files I can’t simply fill it with a ‘in range(0,1000,100)’ statement annoyingly.

whilst I can write out all the elif statements I feel as if there must be a more efficient way to write this code I am just not good enough to see it. Could anyone else write this in a nicer way or am I just going to have to write 10 elif statments (i just don’t feel like it is neat code!).

#python #numpy

Difference between Array and Map

An array is a collection of items stored at contiguous memory locations. The idea is to store multiple items of the same type together. This makes it easier to calculate the position of each element by simply adding an offset to a base value, i.e., the memory location of the first element of the array (generally denoted by the name of the array).

The diagrammatic representation of the Array is given below:

Program 1:

Below is an illustration of a 1D array:

  • C++

// C++ program to illustrate 1D array

#include <bits/stdc++.h>

**using** **namespace** std;

// Driver Code

**int** main()

{

// Given array

**int** arr[] = { 6, 10, 5, 0 };

// Print the array elements

**for** (``**int** i = 0; i < 4; i++) {

cout << arr[i] << " "``;

}

**return** 0;

}

Output:

6 10 5 0

Program 2:

Below is an illustration of a 2D array:

// C++ program to illustrate 1D array

#include <bits/stdc++.h>

**using** **namespace** std;

// Driver Code

**int** main()

{

// A 2D array with 3 rows and

// 2 columns

**int** x[3][2] = { { 0, 1 }, { 2, 3 }, { 4, 5 } };

// Print each array element value

// Traverse row

**for** (``**int** i = 0; i < 3; i++) {

// Traverse column

**for** (``**int** j = 0; j < 2; j++) {

// Print element

cout << "Element at x[" << i

<< "][" << j

<< "]: "``;

cout << x[i][j] << endl;

}

}

**return** 0;

}

Output:

Element at x[0][0]: 0
Element at x[0][1]: 1
Element at x[1][0]: 2
Element at x[1][1]: 3
Element at x[2][0]: 4
Element at x[2][1]: 5

Map:

map is an associative container that stores elements in a mapped fashion. Each element has a key value and a mapped value. No two mapped values can have equal key values.

The diagrammatic representation of Map is given below:

Program 1:

Below is an illustration of a map:

// C++ program to illustrate Map

#include <bits/stdc++.h>

**using** **namespace** std;

// Driver Code

**int** main()

{

// Empty map container

map<``**int**``, **int**``> gquiz1;

// Insert elements in Map

gquiz1.insert(pair<``**int**``, **int**``>(1, 40));

gquiz1.insert(pair<``**int**``, **int**``>(2, 30));

gquiz1.insert(pair<``**int**``, **int**``>(3, 60));

// Iterator to iterate Map

map<``**int**``, **int**``>::iterator itr;

cout << "\nThe map gquiz1 is : \n"``;

cout << "\tKEY\tELEMENT\n"``;

// Print map gquiz1

**for** (itr = gquiz1.begin();

itr != gquiz1.end(); ++itr) {

cout << '\t' << itr->first

<< '\t' << itr->second

<< '\n'``;

}

**return** 0;

}

Output:

The map gquiz1 is : 
    KEY    ELEMENT
    1    40
    2    30
    3    60

Program 2:

Below is an illustration of an unordered map:

// C++ program to illustrate Map

#include <bits/stdc++.h>

**using** **namespace** std;

// Driver Code

**int** main()

{

// Declaring umap of <string, int>

// type key will be of string and

// mapped value will be of doubl

unordered_map<string, **int**``> umap;

// Insert values by using [] operator

umap[``"GeeksforGeeks"``] = 10;

umap[``"Practice"``] = 20;

umap[``"Contribute"``] = 30;

// Traversing an unordered map

// and print the key-value pairs

**for** (``**auto** x : umap)

cout << x.first << " "

<< x.second << endl;

**return** 0;

}

Output:

Contribute 30
GeeksforGeeks 10
Practice 20

Program 3:

Below is an illustration of a multimap:

// C++ program to illustrate Multimap

#include <bits/stdc++.h>

**using** **namespace** std;

// Driver Code

**int** main()

{

// Empty multimap container

multimap<``**int**``, **int**``> gquiz1;

// Insert elements

gquiz1.insert(pair<``**int**``, **int**``>(1, 40));

gquiz1.insert(pair<``**int**``, **int**``>(2, 30));

// Iterator

multimap<``**int**``, **int**``>::iterator itr;

cout << "\nThe multimap gquiz1 is : \n"``;

cout << "\tKEY\tELEMENT\n"``;

// Print multimap gquiz1

**for** (itr = gquiz1.begin();

itr != gquiz1.end(); ++itr) {

cout << '\t' << itr->first

<< '\t' << itr->second

<< '\n'``;

}

**return** 0;

}

Output:

The multimap gquiz1 is : 
    KEY    ELEMENT
    1    40
    2    30

Program 4:

Below is an illustration of an unordered multimap:

// C++ program to illustrate

// unordered multimap

#include <bits/stdc++.h>

**using** **namespace** std;

// Driver Code

**int** main()

{

// Empty initialization

unordered_multimap<string, **int**``> umm1;

// Initialization by intializer list

unordered_multimap<string, **int**``> umm2(

{ { "apple"``, 1 },

{ "ball"``, 2 },

{ "apple"``, 10 },

{ "cat"``, 7 },

{ "dog"``, 9 },

{ "cat"``, 6 },

{ "apple"``, 1 } });

// Traversing an unordered_multimap

// and print the elements stored

**for** (``**auto** x : umm2) {

cout << x.first << " "

<< x.second << endl;

}

**return** 0;

}

Output:

dog 9
cat 6
cat 7
ball 2
apple 1
apple 10
apple 1

#arrays #data structures #difference between #cpp-array #cpp-map #cpp-multimap #cpp-unordered_map #cpp-unordered_multimap

Joseph  Murray

Joseph Murray

1623911281

How to Print an Array in Java

Introduction

Printing an array is a quick way to give us visibility on the values of the contents inside. Sometimes the array values are the desired output of the program.

In this article, we’ll take a look at how to print an array in Java using four different ways.

While the “best way” depends on what your program needs to do, we begin with the simplest method for printing and then show more verbose ways to do it.

#java #array #how to print an array in java #array in java #print an array in java #print

What are Iterables and Iterators in Python

While writing an article about Generators, I realized that though iteration, iterable and iterator are so commonly used in programming; there is a certain degree of confusion around them. It’s crucial to understand the underlying concept for a better implementation. This is going to be a short and quick article to help us determine what is an iterable and what is an iterator.


Iteration

  • In layman’s language it is ‘repeating steps’.
  • Iteration in programming is a repetition of a block of code for a certain number of times.
  • This can be achieved by using loops.

Iterable

  • Iterable is an object which can be looped over or iterated over.
  • e.g. List, Tuple, Set, Dictionary, File.
  • In simpler words, iterable is a container which has data or values and we perform iteration over it to get elements one by one. (Can traverse through these values one by one)
  • Iterable has an in-built dunder method iter. A simpler way to determine whether an object is iterable is to check if it supports iter. How? Using dir( ), it returns the list of attributes and methods supported by an object.

Iterator

  • Iterator is an iterable object with a state so it remembers where it is during iteration.
  • e.g. Generator
  • Iterator performs the iteration to access the elements of the iterable one by one. As it maintains the internal state of elements, iterator knows how to get the next value.
  • Iterators can only move forward using _next _. It cannot go back or cannot be reset.
  • Iterator supports in-built dunder methods _iter _and next.

#iteration #python #data-science #programming #iterators

Coy  Roberts

Coy Roberts

1600864380

Javascript ES6 Iterators: Complete Guide on Iterables and Iterators

Javascript iterators is a new concept of ES6 in which it processes each of the items in the collection, which is a pervasive operation. Iterators are a new way to _loop _over any collection in JavaScript.

They were introduced in ES6 and have become popular since they are widely JavaScript provides several ways of iterating over the collection, from simple for loops to map() and filter() Iterators and  Generators bring the concept of iteration directly into the core language and provide a mechanism for customizing the behavior of loops.

Iterators in Javascript

An iterator is a new concept of ES6. An Iterator is an object that lets us iterate through an Array, an Object, a String, or even custom Objects.

The Iterator allows us to effectively loop over a collection of objects like an arraystringobjects, or other data structures.

The most common Iterator in Javascript is the Array iterator, which returns each value in the associated array in sequence.

#javascript #javascript es6 #iterators #iterables