make list in list dynamic in python, problem? - arrays

i want to make a list by combining the results of n. but when the x values are different, it creates a new list. However, if the x value is the same, it will be in 1 list
My code is like this
muy = [[1,2,3],[4,5,6],[7,8,9]]
# will = []
for x in muy:
for y in muy:
if x != y:
print(x, " ", y)
m = np.subtract(x, y)
n = sum(m)
print(m)
print(n)
the result is like this
[1, 2, 3] [4, 5, 6]
[-3 -3 -3]
-9
[1, 2, 3] [7, 8, 9]
[-6 -6 -6]
-18
[4, 5, 6] [1, 2, 3]
[3 3 3]
9
[4, 5, 6] [7, 8, 9]
[-3 -3 -3]
-9
[7, 8, 9] [1, 2, 3]
[6 6 6]
18
[7, 8, 9] [4, 5, 6]
[3 3 3]
9
the result what i want is like this:
[[-9,-18][9,-9][18,9]]
what should i do?

muy = [[1,2,3],[4,5,6],[7,8,9]]
will = []
for x in muy:
temp = []
for y in muy:
if x != y:
m = np.subtract(x, y)
n = sum(m)
temp.append(n)
will.append(temp)
print(will)

Related

How to eliminate the rows from an array which have common elements at different index position in python?

I have a 2D array of form
[[ 1 6]
[ 2 7]
[ 5 6]
[ 6 1]
[ 6 5]
[ 7 2]]
I want the output of the form
[[ 1 6]
[ 2 7]
[ 5 6]]
How I can get this output?
Here's a solution using plain Python, but I'm sure there's a more idiomatic way with NumPy.
import numpy as np
a = np.array([
[1, 6],
[2, 7],
[5, 6],
[6, 1],
[6, 5],
[7, 2]])
seen = set()
out = []
for row in a:
elements = frozenset(row)
if elements not in seen:
seen.add(elements)
out.append(row)
print(np.array(out))
Output:
[[1 6]
[2 7]
[5 6]]

subtracting every nth array with every nth array

I have an array with the shape (10000,6). For example:
a = np.array([[5, 5, 5, 5, 5, 5][10, 10, 10, 10, 10][15, 15, 15, 15, 15]...])
I want to take every 25th array and subtract its element values from the next 25 elements until a new subtraction array in selected. so for example if the first array is:
[10, 10, 10, 10, 10]
then these values should be subtracted on the array itself and the next 25 arrays until for example a new subtraction array like this is selected:
[2, 2, 2, 2, 2]
then the array itself and the following 25 elements should be subtracted that arrays values.
This means that after the operation every 25th array will be:
[0, 0, 0, 0, 0]
because it has been subtracted by itself.
Here's what I would do:
import numpy as np
arr = np.random.randint(0, 10, (9, 3))
group_size = 3
# select vectors you want ot subtract and copy them {group_size} times
selected = arr[::group_size].repeat(3, axis = 0)
# subtract selected vectors from all vectors in the group
sub_arr = arr-selected
output:
arr =
[[9 6 3]
[8 3 3]
[2 0 4]
[0 3 9]
[3 9 9]
[0 8 6]
[4 0 0]
[6 1 9]
[2 6 4]]
selected =
[[9 6 3]
[9 6 3]
[9 6 3]
[0 3 9]
[0 3 9]
[0 3 9]
[4 0 0]
[4 0 0]
[4 0 0]]
sub_arr =
[[ 0 0 0]
[-1 -3 0]
[-7 -6 1]
[ 0 0 0]
[ 3 6 0]
[ 0 5 -3]
[ 0 0 0]
[ 2 1 9]
[-2 6 4]]
You can reshape your array so that each chunk has the right number of lines, and then simply subtract the first line
import numpy as np
a = np.arange(10000)[:, None] * np.ones(6)
a = a.reshape(-1, 25, 6)
a -= a[:, 0, :][:, None, :]
a = a.reshape(-1, 6)

Remove elements from an array into another array (Ruby)

I'm new here and new to coding and I can use some help with a problem I'm trying to solve.
I'm trying to remove all integers that are less than 5 from array
a = [1, 2, 3, 4, 5, 6] and put them into a new array b = [], and then print out the b array.
I've done many Google searches but I can't find anything that helps.
I'm starting to think this is not possible.
Please help!
Thanks
a = [1, 2, 3, 4, 5, 6]
b, a = a.partition { |i| i < 5 }
#=> [[1, 2, 3, 4], [5, 6]]
b #=> [1, 2, 3, 4]
a #=> [5, 6]
See Enumerable#partition.
a = [1, 2, 3, 4, 5, 6]
b = a.select { |i| i < 5 } # [1, 2, 3, 4]
a = a - b # [5, 6]
To actually remove elements from a while putting them in an existing array b, you could use reject!:
a = [1, 2, 3, 4, 5, 6]
b = []
a.reject! { |i| b << i if i < 5 }
a #=> [5, 6]
b #=> [1, 2, 3, 4]
If i < 5 evaluates to true, b << i puts that element in b and returns a truthy result which causes reject! to remove it from a.
Likewise, if i < 5 evaluates to false, b << i is skipped, the block returns a falsy result and that element remains in a.

Getting a single set of elements using loops

Whenever I append an element into a new list in a loop, i get a number of lists that repeats the elements. How do i get a single list with all elements without repeating them?
n, r = [5, 2]
ans = 0
l = [0]
while n-1>0:
ans = ans+r
for i in range(ans):
if ans not in l:
l.append(ans)
print(l)
n-=1
#I was hoping to get this answer:
[0,2,4,6,8]
#Instead, I got this:
[0, 2]
[0, 2]
[0, 2, 4]
[0, 2, 4]
[0, 2, 4]
[0, 2, 4]
[0, 2, 4, 6]
[0, 2, 4, 6]
[0, 2, 4, 6]
[0, 2, 4, 6]
[0, 2, 4, 6]
[0, 2, 4, 6]
[0, 2, 4, 6, 8]
[0, 2, 4, 6, 8]
[0, 2, 4, 6, 8]
[0, 2, 4, 6, 8]
[0, 2, 4, 6, 8]
[0, 2, 4, 6, 8]
[0, 2, 4, 6, 8]
[0, 2, 4, 6, 8]
Use print outside of loop:
n, r = [5, 2]
ans = 0
l = [0]
while n-1>0:
ans = ans+r
for i in range(ans):
if ans not in l:
l.append(ans)
n-=1
print l

How to have multidimensional array with different length in Julia

I need to make a sequence of an array with different length by reading a dataset. I need to call each of them in a loop so probably I need some sort of indexing in order to call them. For example, how can I create the following sequence:
P[1]=[1 2 3 4]
P[2]=[1 4]
P[3]=[8 9 0 0 5 6]
.
.
.
Here it is:
julia> P = Vector{Vector{Int64}}([[1,2,3,4],[1,4],[8,9,0,0,5,6]])
3-element Array{Array{Int64,1},1}:
[1, 2, 3, 4]
[1, 4]
[8, 9, 0, 0, 5, 6]
julia> P[1]
4-element Array{Int64,1}:
1
2
3
4
julia> P[2]
2-element Array{Int64,1}:
1
4
julia> P[3]
6-element Array{Int64,1}:
8
9
0
0
5
6
If you want to add a new element use push!():
julia> push!(P,[7,8,9])
4-element Array{Array{Int64,1},1}:
[1, 2, 3, 4]
[1, 4]
[8, 9, 0, 0, 5, 6]
[7, 8, 9]

Resources