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]]
I am new to python and trying to solve this example on pyschool
I need to write a function,
a) that takes a list of numbers
b) removes duplicates from the list
c) returns a sorted set:
In python, example :
>>> genSet([5,4,8,4,9,8])
[4, 5, 8, 9 ]
>>> genSet([3,-2,-1,-1,3,-2,0])
[-2, -1, 0, 3 ]
Removing Duplicates:
>>> t = [1, 2, 3, 1, 2, 5, 6, 7, 8]
>>> t
[1, 2, 3, 1, 2, 5, 6, 7, 8]
>>> list(set(t))
[1, 2, 3, 5, 6, 7, 8]
>>> s = [1, 2, 3]
>>> list(set(t) - set(s))
[8, 5, 6, 7]
Sort the List:
>>> sorted([5, 2, 3, 1, 4])
[1, 2, 3, 4, 5]
Now you could write your custom function, that removes duplicates and sort.
The following code snippet works:
def genSet(clist):
t = list(set(clist))
return sorted(t)
if __name__ == "__main__":
print genSet([5,4,8,4,9,8])
print genSet([3,-2,-1,-1,3,-2,0])
If you want to iterate over multiple list try this:
a = [
[5,4,8,4,9,8],
[3,-2,-1,-1,3,-2,0]
]
for aa in a:
print genSet(aa)
set will automatically remove duplicates and sorted will sort the list.
def genSet(l):
return (sorted(set(l)))
Hello I am working with some lists I have the following list:
a = [1,2,3,4,5,6]
I would like to get the these two lists from a:
b = [2,3,4,5,6]
c = [1,2,3,4,5]
I would like to get the firt one removing the first element of a and the second one removing the last element of a, I tried:
b = a
c = a
b.pop(0)
c.pop(len(a)-1)
print(b)
print(c)
print(a)
However the output is:
[2, 3, 4, 5]
[2, 3, 4, 5]
[2, 3, 4, 5]
that is affecting my fist list, I am not sure about what I am doing, I would like to appreciate support with this.
You should not modify the original list - it's simpler than you think, just slice the input list passing the right indexes. Try this:
a = [1, 2, 3, 4, 5, 6]
b = a[1:]
c = a[:-1]
a
=> [1, 2, 3, 4, 5, 6]
b
=> [2, 3, 4, 5, 6]
c
=> [1, 2, 3, 4, 5]
I think slicing would be ok:
b =a[1:];
c =a[:-1]
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)
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]]