Appending values from one array to another array of unknown dimension - python

I have an array A of dimension (654 X 2). Now within a loop, I have an 'if' statement. If for a value of 'i', the condition is true, I need to append the values of 'i'th row of A into a separate array B. That means the dimension of B is not known to me beforehand. So, how to initialize such array B in python. If this is not the procedure, suggest me alternative ways to execute the same.

You could initialize B as empty array:
B = []
Then when needs, just append it with the value:
B.append( [x,y] )

you do not provide ANY code to start from, please read this to learn how to ask a question How to create a Minimal, Complete, and Verifiable example
from the almost 0 information that you've provided
you should try doing something like this:
B = []
for i in range(n):
if i % 2 == 0: # example of condition
B += [ A[i] ]
print(B)

Related

concat and append elements of array with several condition

Moring guys.
I have a question about how to append array elements with several condition.
assume i've array like this:
A=[[100,Z],[102,A],...,[9901,A]]
and another array like this:
B=[[100,-0.22,0.99],[102,-0.442,0.99],...,[9901,-1.22,4.99]]
The length of array A and B are different.
I want to append both the array elements with condition like this:
if(A[0][0]==B[0][0]):
temp = [B[0][1],B[0][2],A[0][1]]
array_new.append(temp)
I've try to append , and works , but the length of the new array is shorter than the A array.
Is something wrong with my code?
this is my code how I concat it:
for g in range(len(A)):
for h in range(len(B)):
if(B[h][0]==A[g][0]):
temp = [B[h][1],B[h][2],A[g][1]]
array.append(temp)
Thank you and have a nice day.
This matches the elements from A and B that have the same first entry in the list.
Convert B to a dictionary with the first element as the key.
The second elements of A are converted to characters, you can't define A recursive.
A=[[100,'Z'],[102,'A'],[9901,'C']]
B=[[100,-0.22,0.99],[102,-0.442,0.99],[9901,-1.22,4.99]]
B_Dict = { b[0]:b for b in B }
array_new = [ [B_Dict.get(a[0])[1],B_Dict.get(a[0])[2],a[1]] for a in A if B_Dict.get(a[0])]
print (array_new)
EDITED
If you indent correctly, length of array must same as A or shorter than A.
Fixing indentation:
for g in range(len(A)):
for h in range(len(B)):
if(B[h][0]==A[g][0]):
temp = [B[h][1],B[h][2],A[g][1]]
array.append(temp)

How to rightly locate a value at an index in nd array and save it as list in python?

I have a list called L. It has C number of elements.
I have a nd array called X. X has Boolean data (either 0 or 1). It has dimension as (20,C). There are 20 lists with each list having C number of elements
I want to locate each index that has value of 1 in X.Then I want the value at this same index in List L, and finally store this value from L in another nd array .
I write the following code
emptylist=[]
for index,value in np.ndenumerate(X): #this index is a tuple like (0,3)
tuple_to_list=list(i)
if value == 1:
emptylist.append (L[tuple_to_list[1]]) #error
the program does not stop running. Can you guide me to improve this code ?
the last line should be:
empylist.append(L[index[0]])
and I don't see what your tuple_to_list is needed for
A solution using only arrays would be the following:
L = list(np.random.rand(20)) # gives a List of radom values (to be compatible to the question)
La = np.array(L)
X = randint(0,5,(20,101)) # make an array having values from 0...4
emptylist = La[np.where(X==1)[0]] #gives those rows of L where rows of X contain 1
though the name empty is not appropriate anymore.

manipulating two 2D lists - python 2.7

if i have these two lists...
a = [[1,y,y],[2,x,x],[3,x,x],[4,y,y]
b = [[1,x,x],[4,x,x]
And i want to combine them such that a new list (c) is formed that contains each row of (a) if the first value of (a) is equal with the first value of (b)
c = [[1,y,y],[4,y,y]]
this is what i have tried so far...
for i in xrange(0,1):
for j in xrange(0,3):
if b[i][0] == a[j][0]:
c[i:] = a[[j:]
overwriting values in the c array isnt a problem due to the type of data in (a).
Im just really stuck on this, any help on whether im on the right track or if i should try something else would be greatly appreciated!
Alternatively, you can consider using sets and list comprehensions.
a = [[1,'y','y'],[2,'x','x'],[3,'x','x'],[4,'y','y']]
b = [[1,'x','x'],[4,'x','x']]
b0s = set(l[0] for l in b)
c = [l for l in a if l[0] in b0s]
print(c)
Try this:
c.append(a[j])
inside your IF statement.

Optimize finding pairs of arrays which can be compared

Definition: Array A(a1,a2,...,an) is >= than B(b1,b2,...bn) if they are equal sized and a_i>=b_i for every i from 1 to n.
For example:
[1,2,3] >= [1,2,0]
[1,2,0] not comparable with [1,0,2]
[1,0,2] >= [1,0,0]
I have a list which consists of a big number of such arrays (approx. 10000, but can be bigger). Arrays' elements are positive integers. I need to remove all arrays from this list that are bigger than at least one of other arrays. In other words: if there exists such B that A >= B then remove A.
Here is my current O(n^2) approach which is extremely slow. I simply compare every array with all other arrays and remove it if it's bigger. Are there any ways to speed it up.
import numpy as np
import time
import random
def filter_minimal(lst):
n = len(lst)
to_delete = set()
for i in xrange(n-1):
if i in to_delete:
continue
for j in xrange(i+1,n):
if j in to_delete: continue
if all(lst[i]>=lst[j]):
to_delete.add(i)
break
elif all(lst[i]<=lst[j]):
to_delete.add(j)
return [lst[i] for i in xrange(len(lst)) if i not in to_delete]
def test(number_of_arrays,size):
x = map(np.array,[[random.randrange(0,10) for _ in xrange(size)] for i in xrange(number_of_arrays)])
return filter_minimal(x)
a = time.time()
result = test(400,10)
print time.time()-a
print len(result)
P.S. I've noticed that using numpy.all instead of builtin python all slows the program dramatically. What can be the reason?
Might not be exactly what you are asking for, but this should get you started.
import numpy as np
import time
import random
def compare(x,y):
#Reshape x to a higher dimensional array
compare_array=x.reshape(-1,1,x.shape[-1])
#You can now compare every x with every y element wise simultaneously
mask=(y>=compare_array)
#Create a mask that first ensures that all elements of y are greater then x and
#then ensure that this is the case at least once.
mask=np.any(np.all(mask,axis=-1),axis=-1)
#Places this mask on x
return x[mask]
def test(number_of_arrays,size,maxval):
#Create arrays of size (number_of_arrays,size) with maximum value maxval.
x = np.random.randint(maxval, size=(number_of_arrays,size))
y= np.random.randint(maxval, size=(number_of_arrays,size))
return compare(x,y)
print test(50,10,20)
First of all we need to carefully check the objective. Is it true that we delete any array that is > ANY of the other arrays, even the deleted ones? For example, if A > B and C > A and B=C, then do we need to delete only A or both A and C? If we only need to delete INCOMPATIBLE arrays, then it is a much harder problem. This is a very difficult problem because different partitions of the set of arrays may be compatible, so you have the problem of finding the largest valid partition.
Assuming the easy problem, a better way to define the problem is that you want to KEEP all arrays which have at least one element < the corresponding element in ALL the other arrays. (In the hard problem, it is the corresponding element in the other KEPT arrays. We will not consider this.)
Stage 1
To solve this problem what you do is arrange the arrays in columns and then sort each row while maintaining the key to the array and the mapping of each array-row to position (POSITION lists). For example, you might end up with a result in stage 1 like this:
row 1: B C D A E
row 2: C A E B D
row 3: E D B C A
Meaning that for the first element (row 1) array B has a value >= C, C >= D, etc.
Now, sort and iterate the last column of this matrix ({E D A} in the example). For each item, check if the element is less than the previous element in its row. For example, in row 1, you would check if E < A. If this is true you return immediately and keep the result. For example, if E_row1 < A_row1 then you can keep array E. Only if the values in the row are equal do you need to do a stage 2 test (see below).
In the example shown you would keep E, D, A (as long as they passed the test above).
Stage 2
This leaves B and C. Sort the POSITION list for each. For example, this will tell you that the row with B's mininum position is row 2. Now do a direct comparison between B and every array below it in the mininum row, here row 2. Here there is only one such array, D. Do a direct comparison between B and D. This shows that B < D in row 3, therefore B is compatible with D. If the item is compatible with every array below its minimum position keep it. We keep B.
Now we do the same thing for C. In C's case we need only do one direct comparison, with A. C dominates A so we do not keep C.
Note that in addition to testing items that did not appear in the last column we need to test items that had equality in Stage 1. For example, imagine D=A=E in row 1. In this case we would have to do direct comparisons for every equality involving the array in the last column. So, in this case we direct compare E to A and E to D. This shows that E dominates D, so E is not kept.
The final result is we keep A, B, and D. C and E are discarded.
The overall performance of this algorithm is n2*log n in Stage 1 + { n lower bound, n * log n - upper bound } in Stage 2. So, maximum running time is n2*log n + nlogn and minimum running time is n2logn + n. Note that the running time of your algorithm is n-cubed n3. Since you compare each matrix (n*n) and each comparison is n element comparisons = n*n*n.
In general, this will be much faster than the brute force approach. Most of the time will be spent sorting the original matrix, a more or less unavoidable task. Note that you could potentially improve my algorithm by using priority queues instead of sorting, but the resulting algorithm would be much more complicated.

How do I recursively reduce an element of one/multiple lists contained in a list with python?

I need a piece of code that can reduce an element of one or multiple lists contained in a list. This is a sorted list. The reduction must continue until a certain parameter is greater than zero. I see two possible scenarios and I can not figure out a solution that applies to both. I am having a difficulty formulating a logically sound statement for the problem, will try to provide more details if needed. Thanks!
#Scenario 1
parameter = 5
data = [[1.99,1],[1.98,1],[1.97,2],[1.96,6]]
#reduction code
data = [[1.99,0],[1.98,0],[1.97,0],[1.96,5]]
#Scenario 2
parameter = 0.5
data = [[1.99,1],[1.98,1],[1.97,2],[1.96,6]]
#reduction code
data = [[1.99,0.5],[1.98,1],[1.97,2],[1.96,6]]
the flow of the program is this...
take 2nd element of 1st list reduce it by 1 parameter becomes 5 - 1, continue, take 2nd elemnt of 2nd list reduce it by 1, parameter becomes 3, continue until parameter is 0 (first scenario)
I don't understand why the second scenario has the [1.99, 0.5] pair show up...., but
data_reduced = []
for pair in data:
if pair[1] < parameter:
pair[1] = 0
else :
pair[1] = parameter
data_reduced.append(pair)
Scenario-1 and Scenario-2 both are different cases. I can not find any common logic to built formula for it.
For the first scenario:
parameter = 3
data = [[1.99,1],[1.98,1],[1.97,2],[1.96,6]]
# copy of data list
data_reduced = data[:]
for n,l in enumerate(data_reduced):
if parameter <= 0:
break
else:
l[1] = l[1] - 1
parameter -= 1
print data_reduced
The data list has only 4 elements and you did not describe what should happen if the list is shorter than the parameter counter like in your example (paramater = 5, but only 4 elements in the list), so I reduced the parameter to 3.
The second scenario is completely different, because only the second element of the first list is reduced (and not by 1).

Categories

Resources