How to Take an Array, Divide It by Two and Create Two Lists

How can i take an array, divide it by two and create two lists?

To get a part of an array, you can use array_slice:

$input = array("a", "b", "c", "d", "e");

$len = (int) count($input);

$firsthalf = array_slice($input, 0, $len / 2);
$secondhalf = array_slice($input, $len / 2);

Split list into smaller lists (split in half)

A = [1,2,3,4,5,6]
B = A[:len(A)//2]
C = A[len(A)//2:]

If you want a function:

def split_list(a_list):
half = len(a_list)//2
return a_list[:half], a_list[half:]

A = [1,2,3,4,5,6]
B, C = split_list(A)

Split array into two arrays

var arr2 = ['a', 'b', 'c', 'd', 'e', 'f'];
arr = arr2.splice(0, arr2.indexOf('c'));

To remove 'c' from arr2:

arr2.splice(0,1);

arr contains the first two elements and arr2 contains the last three.

This makes some assumptions (like arr2 will always contain the 'point' at first assignment), so add some correctness checking for border cases as necessary.

How to split/divide an array into 2 using php?

Use array_chunk:

$pieces = array_chunk($array, ceil(count($array) / 2));

If you want them in separate variables (instead of a multi-dimensional array), use list:

list($array1, $array2) = array_chunk($array, ceil(count($array) / 2));

PHP: How to split array into 2 parts?

This is a one liner:

$halved = array_chunk($books, ceil(count($books)/2));

Then $halved[0] will contain the first half of the array. It will always be 1 element larger in the event that the array contains an odd number of elements. Of course, $halved[1] will contain the 2nd half of the array.

Here's a working example

How to split an array into two arrays, one for even and one for odd in C

Un-initialized variables can contain anything, it is better to start of by initializing.

This

int array100[100];
int arraypar[100];
int arrayimpar[100];

Should be:

int array100[100] = {0};
int arraypar[100] {0};
int arrayimpar[100] = {0};

And since C uses 0 base array indexing, you are probably seeing a run time error; Dereference of out-of-bounds pointer (or similar) if the code shown in your post is the actual code you are using.

This:

for(i=1;i<=100;i++){
^ ^^
printf("%i ", arraypar[i]);
}

Should be this:

for(i=0;i<100;i++){
^ ^
printf("%i ", arraypar[i]);
}

Same for the next for loop.

Also, a suggestion to help you troubleshoot. I would start off by initializing array100 with a known set of sequential values to easily test whether the values are being split properly:

int array100[100] = {1,2,6,4,5,6,7,8,9,10,
11,12,16,14,15,16,17,18,19,
20,21,22,26,24,25,26,27,28,29,
60,61,62,66,64,65,66,67,68,69,
40,41,42,46,44,45,46,47,48,49,
50,51,52,55,54,55,56,57,58,59,
60,61,62,66,64,65,66,67,68,69,
70,71,72,77,74,75,77,77,78,79,
80,81,82,88,84,85,88,87,88,89,
90,91,92,99,94,95,99,97,98,99,100};

Now run your program with the rand() section commented out to see what you get. If there are problems, they will be easier to see then looking at a collection of randomly generated numbers.

Dividing each corresponding element of two lists in Python

A readable, clean and safe way of doing this would be:

with open('average2mm.txt','r') as average, \
open('difference2mm.txt','r') as difference:

for avg, diff in zip(average, difference):
avg, diff = avg.strip(), diff.strip()
if avg and diff:
ratio = float(avg) / float(diff)
print(ratio)

If you want to store it in a list:

result = []
with open('average2mm.txt','r') as average, \
open('difference2mm.txt','r') as difference:

for avg, diff in zip(average, difference):
avg, diff = avg.strip(), diff.strip()
if avg and diff:
ratio = float(avg) / float(diff)
result.append(ratio)

If you want to write to another file, you can either iterate over result list and write to file, or:

with open('average2mm.txt','r') as average, \
open('difference2mm.txt','r') as difference, \
open('ratio.txt', 'w') as ratiofile:

for avg, diff in zip(average, difference):
avg, diff = avg.strip(), diff.strip()
if avg and diff:
ratio = float(avg) / float(diff)
print(ratio, file=ratiofile)
# Or,
# ratiofile.write(f'{ratio}\n')


Related Topics



Leave a reply



Submit