Related
Hi i want to classify indexes of same rows in 2D numpy array. Is there any function to do it ?
Something like this :
a= [[1,2,3] , [2,3,4] , [5,6,7] ,[1,2,3] ,[1,2,3] , [2,3,4]]
then f(a) returns same row indexes [[0,3,4],[1,5],[2]]
I would appreciate for your solutions
Here's one to output list of arrays of row indices -
def classify_rows(a):
sidx = np.lexsort(a.T)
b = a[sidx]
m = ~(b[1:] == b[:-1]).all(1)
return np.split(sidx, np.flatnonzero(m)+1)
If you need a list of lists as output -
def classify_rows_list(a):
sidx = np.lexsort(a.T)
b = a[sidx]
m = np.concatenate(( [True], ~(b[1:] == b[:-1]).all(1), [True]))
l = sidx.tolist()
idx = np.flatnonzero(m)
return [l[i:j] for i,j in zip(idx[:-1],idx[1:])]
Sample run -
In [78]: a
Out[78]:
array([[1, 2, 3],
[2, 3, 4],
[5, 6, 7],
[1, 2, 3],
[1, 2, 3],
[2, 3, 4]])
In [79]: classify_rows(a)
Out[79]: [array([0, 3, 4]), array([1, 5]), array([2])]
In [80]: classify_rows_list(a)
Out[80]: [[0, 3, 4], [1, 5], [2]]
Python 2.7.9
Hello I'm experiencing some problems with how python is handling my permutations algorithm.
So I want my function to take the set_list=[1,2,3,4] and return all the permutations of that list.
This was my idea. Move from left to right, starting at the index values 0,1 of set_list, flip the value of set_list[index] with the value of set_list[index+1], with the understanding that when you reach an end point you flip set_list[0] with set_list[-1]. I thought this was gucci.
Solution
Forward
1234
2134
2314
2341
1342
3142
3412
3421
1423
4123
4213
4231
The idea then being to just to a reverse sort of the previous sub-lists to derive all the possible permutations.
Solution Reverse
4321
4312
4132
1432
2431
2413
2143
1243
3241
3214
3124
1324
My python notes
x = [1,2,3,4]
print(x)
j k k j
x[0],x[1] = x[1],x[0]
print(x)
x[1],x[2] = x[2],x[1]
print(x)
x[2],x[3] = x[3],x[2]
print(x)
x[3],x[0] = x[0],x[3]
print(x)
x[0],x[1] = x[1],x[0]
print(x)
x[1],x[2] = x[2],x[1]
print(x)
x[2],x[3] = x[3],x[2]
print(x)
x[3],x[0] = x[0],x[3]
print(x)
x[0],x[1] = x[1],x[0]
print(x)
x[1],x[2] = x[2],x[1]
print(x)
x[2],x[3] = x[3],x[2]
print(x)
x[3],x[0] = x[0],x[3]
print(x)
Code returns:
[1, 2, 3, 4]
[2, 1, 3, 4]
[2, 3, 1, 4]
[2, 3, 4, 1]
[1, 3, 4, 2]
[3, 1, 4, 2]
[3, 4, 1, 2]
[3, 4, 2, 1]
[1, 4, 2, 3]
[4, 1, 2, 3]
[4, 2, 1, 3]
[4, 2, 3, 1]
[1, 2, 3, 4]
So for a lot of reasons recursively using this function doesn't work (I have no idea why):
def set_return(__LIST__,j,k):
__LIST__[j],__LIST__[k]=__LIST__[k],__LIST__[j]
return(__LIST__)
So I decided to try and sudo-bullshit-hack a solution together, and have only successfully succeed in pulling my hair out.
Step 1)
Create lists containing values from vertical lists of j,k based on characteristics of list x shouldn't be that hard right?
x = [1,2,3,4]
set_0 = range(0,len(x))*(len(x)-1)
set_1 = set_0[1:len(set_0)]+[set_0[0]]
Code Returns:
>>> set_0
[0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3]
>>> set_1
[1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3, 0]
>>>
Step 2)
def shift(__LIST__,a,b):
j = int(__LIST__[a])
k = int(__LIST__[b])
__LIST__[a] = k
__LIST__[b] = j
return(__LIST__)
Code Output:
>>>
[1, 2, 3, 4]
[2, 1, 3, 4]
[2, 3, 1, 4]
[2, 3, 4, 1]
[1, 3, 4, 2]
[3, 1, 4, 2]
[3, 4, 1, 2]
[3, 4, 2, 1]
[1, 4, 2, 3]
[4, 1, 2, 3]
[4, 2, 1, 3]
[4, 2, 3, 1]
[1, 2, 3, 4]
>>> set_0
[0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3]
>>> set_1
[1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3, 0]
>>> shift([1, 2, 3, 4],0,1)
[2, 1, 3, 4]
>>> shift([2, 1, 3, 4],1,2)
[2, 3, 1, 4]
>>> shift([2, 3, 1, 4],2,3)
[2, 3, 4, 1]
>>> shift([2, 3, 4, 1],3,0)
[1, 3, 4, 2]
>>>
So then I use this:
chi = [1,2,3,4]
set_0 = range(0,len(chi))*(len(chi)-1)
set_1 = set_0[1:len(set_0)]+[set_0[0]]
to_return=[]
x = [1,2,3,4]
for i in range(len(set_0)):
insert=shift(x,set_0[i],set_1[i])
to_return.append(insert)
x = insert
And get:
[[1, 2, 3, 4],
[1, 2, 3, 4],
[1, 2, 3, 4],
[1, 2, 3, 4],
[1, 2, 3, 4],
[1, 2, 3, 4],
[1, 2, 3, 4],
[1, 2, 3, 4],
[1, 2, 3, 4],
[1, 2, 3, 4],
[1, 2, 3, 4],
[1, 2, 3, 4]]
Are there easier ways...yes. Can I "protect" x or insert or hell maybe even both with list() ...face palm. Posting anyway. Enjoy.
The issue you are having at the end of your code is that all your insert values are references to the same list x, which gets modified in place by the shift function. You only see [1, 2, 3, 4] values in your to_return list because that's the final value of x. If you printed x while the loop was running you'd see the other permutations, but storing a reference in to_return doesn't preserve the value when x is modified later.
Compare with this:
x = [1, 2]
y = [x, x, x, x] # y contains several references to the list x
print(y) # prints [[1, 2], [1, 2], [1, 2], [1, 2]]
x[0] = 0 # modify x in place
print(y) # prints [[0, 2], [0, 2], [0, 2], [0, 2]], all the 1's are gone!
To fix this issue you can copy the list before appending it to to_return. The copy won't change when you modify x again later. (An alternative solution would be to change shift to return a new list without changing the old one, but I'll leave exploring that option up to you.) When you're storing a copy, you don't even need to care about shift's return value, you can just keep using x directly:
for i in range(len(set_0)):
shift(x,set_0[i],set_1[i]) # ignore the return value, which is another reference to x
to_return.append(x[:]) # append a copy of x
The slice x[:] is a compact way of copying the list. You could use list(x) if you wanted to be a bit more explicit (at the cost of a few extra characters). Either version would be easily understood as a copy by experienced Python programmers.
Before I finish this answer, I want to make a few additional suggestions which are somewhat unrelated to the issue you were asking about.
First off, your algorithm doesn't actually find all the permutations of the list you're giving it. For instance, [4, 3, 2, 1] never appears (nor indeed any permutation where 4 appears directly before 3). So even if the code I suggest above "works" in terms of doing what you want it to do, it may not be doing the right thing in a larger sense.
Second is a matter of style. Python lets you name your variables whatever you like, but it's a very bad idea to use some kinds of names. In several of your functions you're using the name __LIST__ which is a really bad name for an argument. For one thing, double-underscore names are reserved for the interpreter. In some future version, Python might store some special value in the name __LIST__ (overwriting whatever you're using it for). Or it might expect that name to hold some specific kind of value, and break in some way if you store something else in it. So don't use double underscores for arbitrary variables!
Even if we ignore the double underscores, __LIST__ is not a great name because it's really long and hard to type. While a name that's more explicit is often good (e.g. number_of_things may be better than n), you have to balance that with being easy enough to read and type. It's probably not wise to use list, since that's the name of the builtin type, but lst is a pretty common abbreviation of it.
When it comes to names, above all, be consistent. It doesn't really matter if you use a and b for throwaway variables, but it's much more confusing when you sometimes use those, and other times use j, and k (without any obvious distinction between their meanings).
def factorial(n):
c=1
for i in range(1,n+1):
c*=i
return(c)
def set_start(LIST):
to_return=[]
for i in range(len(LIST)):
insert=[]
for x in range(len(LIST)):
if LIST[i]!=LIST[x]:
insert.append(LIST[x])
to_return.append(insert)
return(to_return)
def set_builder(NESTED_LIST):
to_return=[]
for i in range(len(NESTED_LIST)):
to_return.append(set_start(NESTED_LIST[i]))
return(to_return)
def set_chain(NESTED_LIST):
to_return=[]
for i in range(len(NESTED_LIST)):
to_return+=NESTED_LIST[i]
return(to_return)
def set_expand(SET):
to_return=[]
for i in range(len(SET)):
to_return+=[SET[i]]*factorial(len(SET)-1)
return(to_return)
def set_rotation(SET):
set_0 = range(0,len(SET)-1,2)
set_1 = range(1,len(SET),2)
to_return=[]
for i in range(len(set_0)):
to_return+=[SET[set_1[i]],SET[set_0[i]]]
return(to_return)
def recursive_chain(SET):
sub_set_lengths=[]
for i in range(len(SET)):
sub_set_lengths.append(len(SET[i]))
sub_set_lengths = sorted(list(set(sub_set_lengths)),reverse=True)
to_return=[]
for i in range(len(sub_set_lengths)):
insert=[]
for x in range(len(SET)):
if sub_set_lengths[i]==len(SET[x]):
insert+=SET[x]
to_return.append(insert)
return(to_return)
def recursive_return(to_return):
to_return = [to_return]
initialize = set_start(to_return[-1])
while len(to_return[-1])!=2:
to_return+=initialize
to_chain = list(set_builder(list(initialize)))
to_pass = list(set_chain(list(to_chain)))
initialize = list(to_pass)
for i in range(len(to_return)):
if len(to_return[i])!=2:
to_return[i]=set_expand(to_return[i])
to_return = recursive_chain(to_return)
to_return+=[set_rotation(to_return[-1])]
return(to_return)
def PERMUTATIONS(SET):
to_return=[]
to_pop = recursive_return(SET)
while to_pop[-1]!=[]:
insert=[]
for i in range(len(SET)):
insert.append(to_pop[i][0])
to_return.append(insert)
for i in range(len(SET)):
to_pop[i].pop(0)
return(to_return)
I've abstracted my problem to something very simple. I'd like the [:-0] to give me [:] but can't see how to do it without using a special case.
I'm trying to get the behavior indicated below:
The first cases are just for completeness.
a = range(6)
print "a[i:] for i in range(3)"
for i in range(3):
print a[i:]
print "a[:i] for i in range(3)"
for i in range(3):
print a[:i]
print "a[-i:] for i in range(3)"
for i in range(3):
print a[-i:]
print "a[:i] for i in [None, -1, -2] DESIRED RESULT"
for i in [None, -1, -2]:
print a[:i]
print "a[:-i] for i in range(3) BUT CAN'T GET IT HERE"
for i in range(3):
print a[:-i]
print "a[:i] for i in [0, -1, -2] OR HERE EITHER"
for i in [0, -1, -2]:
print a[:i]
You can run it in 2.7 or just refer to the results below:
a[i:] for i in range(3)
[0, 1, 2, 3, 4, 5]
[1, 2, 3, 4, 5]
[2, 3, 4, 5]
a[:i] for i in range(3)
[]
[0]
[0, 1]
a[-i:] for i in range(3)
[0, 1, 2, 3, 4, 5]
[5]
[4, 5]
a[:i] for i in [None, -1, -2] DESIRED RESULT
[0, 1, 2, 3, 4, 5]
[0, 1, 2, 3, 4]
[0, 1, 2, 3]
a[:-i] for i in range(3) BUT CAN'T GET IT HERE
[]
[0, 1, 2, 3, 4]
[0, 1, 2, 3]
a[:i] for i in [0, -1, -2] OR HERE EITHER
[]
[0, 1, 2, 3, 4]
[0, 1, 2, 3]
You need to take advantage of Python's coalescing operators:
>>> for i in range(3):
... print a[:-i or None]
...
[0, 1, 2, 3, 4, 5]
[0, 1, 2, 3, 4]
[0, 1, 2, 3]
>>> for i in [0, -1, -2]:
... print a[:i or None]
...
[0, 1, 2, 3, 4, 5]
[0, 1, 2, 3, 4]
[0, 1, 2, 3]
How can I go about splitting a list into 2 separate lists based on every 5 numbers. This is what im trying to get it to look like.
list = [a,a,a,a,a,b,b,b,b,b,c,c,c,c,c,d,d,d,d,d]
newlista = [a,a,a,a,a,c,c,c,c,c]
newlistb = [b,b,b,b,b,d,d,d,d,d]
Ive been looking at itertools, not sure if im on the right path.
You can do this with list comprehension and slices:
In [1]: a, b, c, d, = 1, 2, 3, 4
In [2]: l = [a,a,a,a,a,b,b,b,b,b,c,c,c,c,c,d,d,d,d,d]
In [3]: [l[i:i+5] for i in range(0,15,10)]
Out[3]: [[1, 1, 1, 1, 1], [3, 3, 3, 3, 3]]
In [4]: [l[i:i+5] for i in range(5,20,10)]
Out[4]: [[2, 2, 2, 2, 2], [4, 4, 4, 4, 4]]
I have a big list of around 2000 numbers in the list. This is just an example of what I want.
I have list1=[1,2,3,4] and list2=[1,3,2,5]. I want it so that list1[i] will be used list2[i] times in the new list.
So for this example the new list would be:list3=[1,2,2,2,3,3,4,4,4,4,4]
The new list3 has 1x1, 3x2, 2x3, 5x4.
This isn't pretty and isn't particularly easy to understand, but works:
>>> list1 = [1, 2, 3, 4]
>>> list2 = [1, 3, 2, 5]
>>> import itertools
>>> list3 = list(itertools.chain(*[[list1[i]] * count for i, count in enumerate(list2)]))
>>> list3
[1, 2, 2, 2, 3, 3, 4, 4, 4, 4, 4]
Brief explanation...
You can multiply a list:
>>> [1] * 3
[1, 1, 1]
Using this in the list comprehension will get you a list-of-lists:
>>> [[list1[i]] * count for i, count in enumerate(list2)]
[[1], [2, 2, 2], [3, 3], [4, 4, 4, 4, 4]]
You can then use itertools to flatten the list as above.
list1=[1,2,3,4]
list2=[1,3,2,5]
list3 = []
for a, b in zip(list1, list2):
for i in range(b):
list3.append(a)
list3 == [1, 2, 2, 2, 3, 3, 4, 4, 4, 4, 4]
Another alternative:
list1=[1,2,3,4]
list2=[1,3,2,5]
z=[]
for x,y in zip(list1,list2):
z.extend([x] * y)
print z