How to use Numpy Tile function To get this result - python-2.7

I want to broadcast the array in a give fashion.I now tile() function in numpy can be used for broadcasting i tried it but not able to generate the required output.
input=[ [1,2],
[3,4],
[4,5] ] #shape(3X2)
numpy.tile(input,----)
out put= [ [ [1,2],
[1,2]
],
[ [3,4],
[3,4]
],
[ [4,5],
[4,5],
]
] #shape(3,2,2)

One approach with np.repeat -
np.repeat(a,2,axis=0).reshape((a.shape) + (2,))
Another one with np.repeat -
np.repeat(a[:,None],2,axis=1) # Or use np.newaxis in place of None
With np.tile -
np.tile(a,2).reshape((a.shape) + (2,))

Another option is to stack input with itself and transpose
np.stack([input] * 2).transpose(1, 0, 2)
array([[[1, 2],
[1, 2]],
[[3, 4],
[3, 4]],
[[4, 5],
[4, 5]]])

Related

Zipp lists in python by iterating through a list of lists

I have two lists of lists as follows. To merge them, I usually do the following:
>>>from itertools import imap, ilist
>>>a = [1,2,3]
>>>b = [4,5,6]
>>> c = list(imap(list,izip(a,b)))
>>> c
[[1, 4]], [2, 5], [3, 6]]
However, now I have a list of list as follows:
[[1,2,3],
[4,5,6],
[7,8,9],
]
How do I iterate through each list and pass it to the izip function to obtain the following output:
[[1,4,7],[2,5,8],[3,6,9]]
Answer for edited Question:
>>> input_list=[[1,2,3],
[4,5,6],
[7,8,9],
]
Using map and zip:
>>> map(list,zip(*input_list))
[[1, 4, 7], [2, 5, 8], [3, 6, 9]]
Using imap and izip:
>>> list(imap(list,list(izip(*input_list))))
[[1, 4, 7], [2, 5, 8], [3, 6, 9]]
Answer for previous question:
By using list comprehension and two for loops:
input_list =[[[1],[2],[3]],
[[4],[5],[6]],
[[7],[8],[9]],
]
out_list = [[] for i in range(len(input_list))]
for each_row in input_list:
for i in range(len(each_row)):
out_list[i].extend(each_row[i])
print out_list
Output:
[[1, 4, 7], [2, 5, 8], [3, 6, 9]]
I think u need something like this:
input_list =[[1,2,3],
[4,5,6],
[7,8,9],
]
result = []
for i in range(len(input_list)):
temp = []
for list in input_list:
temp.append(list[i])
result.append(temp)
print result
result will be:
[[1, 4, 7], [2, 5, 8], [3, 6, 9]]

Extracting from a Nested list Using Indexing and slicing

here is my current list: [0, [], [1,2,3,4], [[5],[6,7]], [8,9,10]]
want to extract from the list and nested items using indexing and slicing, this is what I want to extract: [0, 2, 3, [5 ,6], 8, 10]
code so far:
list = [0, [], [1,2,3,4], [[5],[6,7]], [8,9,10]]
new_list = list[0], list[2], list[3], list[4]
print("new list is", new_list)
outputs this: new list is (0, [1, 2, 3, 4], [[5], [6, 7]], [8, 9, 10]), need to extrac tthe nexted items and format the list like this: [0, 2, 3, [5 ,6], 8, 10]
L = [0, [], [1,2,3,4], [[5],[6,7]], [8,9,10]]
new_list = [L[0], L[2][1], L[2][2], [L[3][0][0], L[3][1][0]], L[-1][-3], L[-1][-1]]
print("new list is", new_list)
print()

partition an 1D array into different sized chunks sequentially with numpy

I would like to partition an 1D array into 2 and 3 sized chunks sequentially
like so:
[1,2,3,4,5,6,7,8,9,10]
[[1,2],
[3,4,5],
[6,7],
[8,9,10]]
solution without numpy:
z=list(range(1,11))
offset=0
r=[]
p=[2,3]
for i in p*(len(z)//sum(p)):
r.append(z[offset:offset+i])
offset+=i
print(r)
yields:
[[1, 2], [3, 4, 5], [6, 7], [8, 9, 10]]

Restructuring a list of lists in python

I am trying to combine several elements from a list of lists in Python3. I have a dataset simplified as:
example_list = [
[1, +, 3, "MNP1", 2480],
[2, +, 2, "MNP1", 2480],
[3, +, 1, "MNP2", 3200],
[4, -, 4, "MNP3", 2480],
[5, -, 2, "MNP4", 4700]
]
What I need is to generate a new list of lists of four elements containing:
1st element: each different value of the last element of the example. If they have different symbol (+ or -) have to be considered separately (ex:2480, 3200, 2480, 4700)
2nd element: each symbol
3rd element: number of times the last element appears (2, 1, 1, 1)
4th element: the sum of third elements only if the symbol and the last element are the same (3+2, 1, 4, 2)
The expected output is:
output_list = [
[2480, +, 2, 5],
[3200, +, 1, 1],
[2480, -, 1, 4],
[4700, -, 1, 2]
]
I have been trying several ways to do it using dictionaries with the last element as key but I am not able to make the function consider the symbol.
Any suggestion?
Thank you in advance!
You can use a dictionary to group the entries by their second and last column. If order is important, use an OrderedDict, otherwise a regular dictionary might work as well.
example_list = [
[1, '+', 3, "MNP1", 2480],
[2, '+', 2, "MNP1", 2480],
[3, '+', 1, "MNP2", 3200],
[4, '-', 4, "MNP3", 2480],
[5, '-', 2, "MNP4", 4700]
]
output_dict = collections.OrderedDict()
for _, op, arg2, _, num in example_list:
key = num, op
output_dict.setdefault(key, [0, 0])
output_dict[key][0] += 1
output_dict[key][1] += arg2
Once you have that, you can turn it into a list:
>>> output_list = [list(key) + value for key, value in output_dict.iteritems()]
>>> print(output_list)
[['+', 2480, 2, 5], ['+', 3200, 1, 1], ['-', 2480, 1, 4], ['-', 4700, 1, 2]]

Prolog - dividing a list in N parts

I'm trying to write a predicate that divides a list into N parts.
This is what I have so far.
partition(1, List, List).
partition(N, List, [X,Y|Rest]):-
chop(List, X, Y),
member(NextToChop, [X,Y]), %Choose one of the new parts to chop further.
NewN is N-1,
partition(NewN, NextToChop, Rest).
chop(List, _, _):-
length(List, Length),
Length < 2, %You can't chop something that doesn't have at least 2 elements
fail,!.
chop(List, Deel1, Deel2):-
append(Deel1, Deel2, List),
Deel1 \= [],
Deel2 \= [].
The idea is to keep chopping parts of the list into two other parts until I have N pieces.
I have mediocre results with this approach:
?- partition(2, [1,2,3,4], List).
List = [[1], [2, 3, 4], 1] ;
List = [[1], [2, 3, 4], 2, 3, 4] ;
List = [[1, 2], [3, 4], 1, 2] ;
List = [[1, 2], [3, 4], 3, 4] ;
List = [[1, 2, 3], [4], 1, 2, 3] ;
List = [[1, 2, 3], [4], 4] ;
false.
So I get what I want, but I get it two times and there are some other things attached.
When dividing into 3 parts things get worse:
?- partition(3, [1,2,3,4], List).
List = [[1], [2, 3, 4], [2], [3, 4], 2] ;
List = [[1], [2, 3, 4], [2], [3, 4], 3, 4] ;
List = [[1], [2, 3, 4], [2, 3], [4], 2, 3] ;
List = [[1], [2, 3, 4], [2, 3], [4], 4] ;
List = [[1, 2], [3, 4], [1], [2], 1] ;
List = [[1, 2], [3, 4], [1], [2], 2] ;
List = [[1, 2], [3, 4], [3], [4], 3] ;
List = [[1, 2], [3, 4], [3], [4], 4] ;
List = [[1, 2, 3], [4], [1], [2, 3], 1] ;
List = [[1, 2, 3], [4], [1], [2, 3], 2, 3] ;
List = [[1, 2, 3], [4], [1, 2], [3], 1, 2] ;
List = [[1, 2, 3], [4], [1, 2], [3], 3] ;
false.
Another idea is using prefix but I don't know how that would really work. To use that I should be able to let Prolog know that it needs to take a prefix that's not too short and not too long either, so I don't take a prefix that's too long so there's nothing left for a next recursion step.
Can anyone point me in the right direction?
Little clarification: the predicate should return all posibilities of dividing the list in N parts (not including empty lists).
When describing relations that involve lists, DCGs are often very useful. Consider:
list_n_parts(List, N, Parts) :-
length(Parts, N),
phrase(parts(Parts), List).
parts([]) --> [].
parts([Part|Parts]) --> part(Part), parts(Parts).
part([P|Ps]) --> [P], list(Ps).
list([]) --> [].
list([L|Ls]) --> [L], list(Ls).
Sample query:
?- list_n_parts([1,2,3,4], 2, Ps).
Ps = [[1], [2, 3, 4]] ;
Ps = [[1, 2], [3, 4]] ;
Ps = [[1, 2, 3], [4]] ;
false.
Here is the basic way I'd use to implement that (using append/2 and length/2) :
list_n_parts(List, Parts, Result) :-
length(Result, Parts),
append(Result, List).
Now, that doesn't totally complies to your expectations : it allows for [].
One idea to fix that is to use a maplist call to format the Resulting list beforehand :
list_n_parts(List, Parts, Result) :-
length(Result, Parts),
using copy_term/2, the maplist/2 call looks like :
maplist(copy_term([_|_]), Result),
using functor/3 (credits to #false), it would look like :
maplist(functor('.', 2), Result),
using lambda.pl you could write :
maplist(\[_|_]^true, Result),
since the '\' already performs a term copy (thanks #false).
The only thing left is the append/2 call:
append(Result, List).
Another idea would be to use forall/2 filtering (maybe simpler to get, but worse in complexity) :
list_n_parts(List, Parts, Result) :-
length(Result, Parts),
append(Result, List),
forall(member(X, Result), X \= []).
etc...