I have a list containing integers and want to replace them so that the element which previously contained the highest number now contains a 1, the second highest number set to 2, etc etc.
Example:
[5, 6, 34, 1, 9, 3] should yield [4, 3, 1, 6, 2, 5].
I personally only care about the first 9 highest numbers by I thought there might be a simple algorithm or possibly even a python function to do take care of this task?
Edit: I don't care how duplicates are handled.
A fast way to do this is to first generate a list of tuples of the element and its position:
sort_data = [(x,i) for i,x in enumerate(data)]
next we sort these elements in reverse:
sort_data = sorted(sort_data,reverse=True)
which generates (for your sample input):
>>> sort_data
[(34, 2), (9, 4), (6, 1), (5, 0), (3, 5), (1, 3)]
and nest we need to fill in these elements like:
result = [0]*len(data)
for i,(_,idx) in enumerate(sort_data,1):
result[idx] = i
Or putting it together:
def obtain_rank(data):
sort_data = [(x,i) for i,x in enumerate(data)]
sort_data = sorted(sort_data,reverse=True)
result = [0]*len(data)
for i,(_,idx) in enumerate(sort_data,1):
result[idx] = i
return result
this approach works in O(n log n) with n the number of elements in data.
A more compact algorithm (in the sense that no tuples are constructed for the sorting) is:
def obtain_rank(data):
sort_data = sorted(range(len(data)),key=lambda i:data[i],reverse=True)
result = [0]*len(data)
for i,idx in enumerate(sort_data,1):
result[idx] = i
return result
Another option, you can use rankdata function from scipy, and it provides options to handle duplicates:
from scipy.stats import rankdata
lst = [5, 6, 34, 1, 9, 3]
rankdata(list(map(lambda x: -x, lst)), method='ordinal')
# array([4, 3, 1, 6, 2, 5])
Assuimg you do not have any duplicates, the following list comprehension will do:
lst = [5, 6, 34, 1, 9, 3]
tmp_sorted = sorted(lst, reverse=True) # kudos to #Wondercricket
res = [tmp_sorted.index(x) + 1 for x in lst] # [4, 3, 1, 6, 2, 5]
To understand how it works, you can break it up into pieces like so:
lst = [5, 6, 34, 1, 9, 3]
# let's see what the sorted returns
print(sorted(lst, reverse=True)) # [34, 9, 6, 5, 3, 1]
# biggest to smallest. that is handy.
# Since it returns a list, i can index it. Let's try with 6
print(sorted(lst, reverse=True).index(6)) # 2
# oh, python is 0-index, let's add 1
print(sorted(lst, reverse=True).index(6) + 1) # 3
# that's more like it. now the same for all elements of original list
for x in lst:
print(sorted(lst, reverse=True).index(x) + 1) # 4, 3, 1, 6, 2, 5
# too verbose and not a list yet..
res = [sorted(lst, reverse=True).index(x) + 1 for x in lst]
# but now we are sorting in every iteration... let's store the sorted one instead
tmp_sorted = sorted(lst, reverse=True)
res = [tmp_sorted.index(x) + 1 for x in lst]
Using numpy.argsort:
numpy.argsort returns the indices that would sort an array.
>>> xs = [5, 6, 34, 1, 9, 3]
>>> import numpy as np
>>> np.argsort(np.argsort(-np.array(xs))) + 1
array([4, 3, 1, 6, 2, 5])
A short, log-linear solution using pure Python, and no look-up tables.
The idea: store the positions in a list of pairs, then sort the list to reorder the positions.
enum1 = lambda seq: enumerate(seq, start=1) # We want 1-based positions
def replaceWithRank(xs):
# pos = position in the original list, rank = position in the top-down sorted list.
vp = sorted([(value, pos) for (pos, value) in enum1(xs)], reverse=True)
pr = sorted([(pos, rank) for (rank, (_, pos)) in enum1(vp)])
return [rank for (_, rank) in pr]
assert replaceWithRank([5, 6, 34, 1, 9, 3]) == [4, 3, 1, 6, 2, 5]
Related
Trying to figure out how to reverse multiple ascending sequences in a list.
For instance: input = [1,2,2,3] to output = [2,1,3,2].
I have used mylist.reverse() but of course it reverses to [3,2,2,1]. Not sure which approach to take?
Example in detail:
So lets say [5, 7, 10, 2, 7, 8, 1, 3] is the input - the output should be [10,7,5,8,7,2,3,1]. In this example the first 3 elements 5,7,10 are in ascending order, 2,7,8 is likewise in ascending order and 1,3 also in ascending order. The function should be able to recognize this pattern and reverse each sequence and return a new list.
All you need is to find all non-descreasing subsequences and reverse them:
In [47]: l = [5, 7, 10, 2, 7, 8, 1, 3]
In [48]: res = []
In [49]: start_idx = 0
In [50]: for idx in range(max(len(l) - 1, 0)):
...: if l[idx] >= l[idx - 1]:
...: continue
...: step = l[start_idx:idx]
...: step.reverse()
...: res.extend(step)
...: start_idx = idx
...:
In [51]: step = l[start_idx:]
In [52]: step.reverse()
In [53]: res.extend(step)
In [54]: print(res)
[10, 7, 5, 8, 7, 2, 3, 1]
For increasing subsequences you need to change if l[idx] >= l[idx - 1] to if l[idx] > l[idx - 1]
Walk the list making a bigger and bigger window from x to y positions. When you find a place where the next number is not ascending, or reach the end, reverse-slice the window you just covered and add it to the end of an output list:
data = [5, 7, 10, 2, 7, 8, 1, 3]
output = []
x = None
for y in range(len(data)):
if y == len(data) - 1 or data[y] >= data[y+1]:
output.extend(data[y:x:-1])
x = y
print(output)
There is probably a more elegant way to do this, but one approach would be to use itertools.zip_longest along with enumerate to iterate over sequential element pairs in your list and keep track of each index where the sequence is no longer ascending or the list is exhausted in order to slice, reverse, and extend your output list with the sliced items.
from itertools import zip_longest
d = [5, 7, 10, 2, 7, 8, 1, 3]
results = []
stop = None
for i, (a, b) in enumerate(zip_longest(d, d[1:])):
if not b or b <= a:
results.extend(d[i:stop:-1])
stop = i
print(results)
# [10, 7, 5, 8, 7, 2, 3, 1]
data = [5, 7, 10, 2, 7, 8, 1, 3,2]
def func(data):
result =[]
temp =[]
data.append(data[-1])
for i in range(1,len(data)):
if data[i]>=data[i-1]:
temp.append(data[i-1])
else:
temp.append(data[i-1])
temp.reverse()
result.extend(temp)
temp=[]
if len(temp)!=0:
temp.reverse()
result.extend(temp)
temp.clear()
return result
print(func(data))
# output [10, 7, 5, 8, 7, 2, 3, 1, 2]
You could define a general handy method which returns slices of an array based on condition (predicate).
def slice_when(predicate, iterable):
i, x, size = 0, 0, len(iterable)
while i < size-1:
if predicate(iterable[i], iterable[i+1]):
yield iterable[x:i+1]
x = i + 1
i += 1
yield iterable[x:size]
Now, the slice has to be made when the next element is smaller then the previous, for example:
array = [5, 7, 10, 2, 7, 8, 1, 3]
slices = slice_when(lambda x,y: x > y, array)
print(list(slices))
#=> [[5, 7, 10], [2, 7, 8], [1, 3]]
So you can use it as simple as:
res = []
for e in slice_when(lambda x,y: x > y, array):
res.extend(e[::-1] )
res #=> [10, 7, 5, 8, 7, 2, 3, 1]
Suppose I have some numpy array (all elements are unique) that I want to sort in descending order. I need to find out which positions elements of initial array will take in sorted array.
Example.
In1: [1, 2, 3] # Input
Out1: [2, 1, 0] # Expected output
In2: [1, -2, 2] # Input
Out2: [1, 2, 0] # Expected output
I tried this one:
def find_positions(A):
A = np.array(A)
A_sorted = np.sort(A)[::-1]
return np.argwhere(A[:, None] == A_sorted[None, :])[:, 1]
But it doesn't work when the input array is very large (len > 100000). What I did wrong and how can I resolve it?
Approach #1
We could use double argsort -
np.argsort(a)[::-1].argsort() # a is input array/list
Approach #2
We could use one argsort and then array-assignment -
# https://stackoverflow.com/a/41242285/ #Andras Deak
def argsort_unique(idx):
n = idx.size
sidx = np.empty(n,dtype=int)
sidx[idx] = np.arange(n)
return sidx
out = argsort_unique(np.argsort(a)[::-1])
Take a look at numpy.argsort(...) function:
Returns the indices that would sort an array.
Perform an indirect sort along the given axis using the algorithm specified by the kind keyword. It returns an array of indices of the same shape as a that index data along the given axis in sorted order.
Here is the reference from the documentation, and the following is a simple example:
import numpy
arr = numpy.random.rand(100000)
indexes = numpy.argsort(arr)
the indexes array will contain all the indexes in the order in which the array arr would be sorted
I face the same problem for plain lists, and would like to avoid using numpy. So I propose a possible solution that should also work for an np.array, and which avoids reversal of the result:
def argsort(A, key=None, reverse=False):
"Indirect sort of list or array A: return indices of elements in order."
keyfunc = (lambda i: A[i]) if key is None else lambda i: key(A[i])
return sorted(range(len(A)), keyfunc, reverse=reverse)
Example of use:
>>> L = [3,1,4,1,5,9,2,6]
>>> argsort( L )
[1, 3, 6, 0, 2, 4, 7, 5]
>>> [L[i]for i in _]
[1, 1, 2, 3, 4, 5, 6, 9]
>>> argsort( L, key=lambda x:(x%2,x) ) # even elements first
[6, 2, 7, 1, 3, 0, 4, 5]
>>> [L[i]for i in _]
[2, 4, 6, 1, 1, 3, 5, 9]
>>> argsort( L, key=lambda x:(x%2,x), reverse = True)
[5, 4, 0, 1, 3, 7, 2, 6]
>>> [L[i]for i in _]
[9, 5, 3, 1, 1, 6, 4, 2]
Feedback would be welcome! (Efficiency compared to previously proposed solutions? Suggestions for improvements?)
Here I have a list
a = [1, 2, 1, 4, 5, 7, 8, 4, 6]
Now I want a following output but without for loop.
Remove all the duplicate from the list.
[2, 5, 7, 8, 6]
output list contain only single occurrence number
Given: a = [1, 2, 1, 4, 5, 7, 8, 4, 6]
One liner:
b = [x for x in a if a.count(x) == 1]
You can use a Counter and a conditional list comprehension or filter in order to maintain the original order:
from collections import Counter
c = Counter(a)
clean_a = filter(lambda x: c[x] == 1, a) # avoids 'for' ;-)
# clean_a = list(filter(lambda x: c[x] == 1, a)) # Python3, if you need a list
# clean_a = [x for x in a if c[a] == 1] # would be my choice
This is a very simple and inefficient implementation.
We use a while loop to access every element of a. In the loop we check if the current element appears only once in the list. If yes, we add it to a new list.
a = [1, 2, 1, 4, 5, 7, 8, 4, 6]
index = 0
result = []
while index < len(a):
if a.count(a[index]) == 1:
result.append(a[index])
index += 1
print(result)
def cleaner(LIST, pos):
if len(LIST)>pos:
if LIST[pos] in LIST[pos+1:]:
LIST.pop(pos)
# OR
# LIST.remove(LIST[pos])
cleaner(LIST, pos)
else:
pos+=1
cleaner(LIST, pos)
return LIST
LIST = [1, 2, 1, 4, 5, 7, 8, 4, 6]
print(cleaner(LIST, 0))
i want to write a function that takes in a list of numbers (positive integers) and returns a list of sorted numbers such that odd numbers come first and even numbers come last
For example:
my_sort([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]) => [1, 3, 5, 7, 9, 2, 4, 6, 8, 10]
my_sort([1, 2]) => [1, 2]
my_sort([2, 1]) => [1, 2]
my_sort([3, 3, 4]) => [3, 3, 4]
my_sort([90, 45, 66]) => [45, 66, 90]'''
This is my code
def my_sort(numbers):
a = [n for n in numbers if n % 2 != 0]
b = [n for n in numbers if n % 2 == 0]
new_num = b + a
for m in numbers:
if a and b:
return new_num
else:
return "Invalid sorted output"
Which fails all the test. I'm new to programming and python. So I'ld appreciate if anyone could help me with this.
And here is the unittest
import unittest
class MySortTestCases(unittest.TestCase):
def setUp(self):
self.result1 = my_sort([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
self.result2 = my_sort([1, 2])
self.result3 = my_sort([2, 1])
self.result4 = my_sort([3, 3, 4])
self.result5 = my_sort([90, 45, 66])
def test_output_1(self):
self.assertEqual(self.result1, [1, 3, 5, 7, 9, 2, 4, 6, 8, 10],
msg='Invalid sorted output')
def test_output_2(self):
self.assertEqual(self.result2, [1, 2], msg='Invalid sorted
output')
def test_output_3(self):
self.assertEqual(self.result3, [1, 2], msg='Invalid sorted
output')
def test_output_4(self):
self.assertEqual(self.result4, [3, 3, 4], msg='Invalid sorted
output')
def test_output_5(self):
self.assertEqual(self.result5, [45, 66, 90], msg='Invalid
sorted output')
You could so it by splitting the list into odd and even and then sorting both and concatenating the two lists.
def my_sort(numbers):
odd = [n for n in numbers if n % 2 != 0]
even = [n for n in numbers if n % 2 == 0]
return sorted(odd) + sorted(even)
See that this
>>> my_sort([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
[1, 3, 5, 7, 9, 2, 4, 6, 8, 10]
But using a key function avoids constructing the split lists:
>>> numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
>>> numbers.sort(key=lambda v: (v%2==0, v))
>>> numbers
[1, 3, 5, 7, 9, 2, 4, 6, 8, 10]
This sorts the list using a key function that returns a tuple of (0, v) if v is odd and (1, v) if even. This causes the odd numbers to appear before the even numbers in an ascending ordered sort.
It can be turned into a function:
def my_sort(numbers):
return sorted(numbers, key=lambda v: (v%2==0, v))
So, I went ahead and grabbed your code and did just a few tests by myself to see the actual output. Your code doesn't actually sort the list currently. It only puts the even numbers first and the odd numbers second. Which by the sound of it isn't what you want either.
>>> my_sort([4, 5, 7, 1, 2, 6, 3])
[4, 2, 6, 5, 7, 1, 3]
>>> my_sort([1, 2])
[2, 1]
These were the outputs I got by running your code using the Python interpreter. As you can see the even numbers are first, unsorted, followed by the odd numbers, also unsorted. This just has to do with the way you made the new_num list. You have new_num = b + a, but you created b by looking for all the even numbers b = [n for n in numbers if n % 2 == 0] and created a by looking for all the odd numbers a = [n for n in numbers if n % 2 != 0]. The % returns the remainder. So, if a number is divisible by 2 it returns 0 and this means that it is even. So, you can either flip the assignment of a and b or you can flip when you add them together so a is first and b is second.
As for the individual chunks not being sorted. Python has a built in sorted function that you can call on list sorted(my_list) that returns a sorted version of that list. So, if you just run that on your a and b when you're adding them together to create your new_num list then the numbers for each should be sorted just fine.
Your if statements at the end inside your for loop are also not working properly. Giving an already sorted list just returns the list given. Your code here:
for m in numbers:
if a and b:
return new_num
else:
return "Invalid sorted output"
This is looping through the original list given and returns the new_num if a and b exists. Since a and b will always exist this will always return new_num and never the "Invalid sorted output" statement. You need to make sure you're checking to see if the numbers list is already sorted with the expected output of your function. My suggestion would be check to see if new_num is the same as numbers.
Just use sorted twice. First to sort the list, and again to sort it by odd then even using the key parameter.
x = [3,5,4,1,6,8,10,2,9,7]
sorted(sorted(x), key=lambda x: (x+1)%2)
# returns:
# [1, 3, 5, 7, 9, 2, 4, 6, 8, 10]
This should work... It makes sense if you are joining Andela.
def my_sort(theNumbers):
#pass
evenNumbers=[]
oddNumbers=[]
#CHECK IF EVEN OR ODD THEN STORE THEN SEPARATE
for i in theNumbers:
if i%2==0:
evenNumbers.append(i)
else:
oddNumbers.append(i)
#print (evenNumbers)
#SORT DEM LISTS
evenNumbers.sort()
oddNumbers.sort()
#print (evenNumbers)
#join the two
oddNumbers+=evenNumbers
print (oddNumbers)
#my_sort([11111, 1, 11, 979749, 1111, 1111])
This code will sort even and odd numbers without creating a temporary list.
def segregateEvenOdd(arr, index=0, iterations=0):
if iterations == len(arr):
return arr
if arr[index]%2 == 0:
arr.append(arr[index])
arr.pop(index)
return segregateEvenOdd(arr, index, iterations+1 )
if arr[index]%2 != 0:
return segregateEvenOdd(arr,index+1,iterations+1)
arr = [ 2, 3, 9, 45, 2, 5, 10, 47 ]
output = segregateEvenOdd(arr)
print(output)
# Output
# [3, 9, 45, 5, 47, 2, 2, 10]
This code should work:
def sort_integers(list_of_integers):
odd_numbers = [n for n in list_of_integers if n%2!=0]
odd_numbers = sorted(odd_numbers, reverse = True)
print(odd_numbers)
even_numbers = [x for x in list_of_integers if x%2 == 0]
even_numbers = sorted(even_numbers, reverse = True)
print(even_numbers)
new_sorted_list = even_numbers + odd_numbers
print(new_sorted_list)
Let's say I have such a python list:
l = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
by using random.shuffle,
>>> import random
>>> random.shuffle(l)
>>> l
[5, 3, 2, 0, 8, 7, 9, 6, 4, 1]
I am having the above list.
How can I get the previous index values list of each item in the shuffled list?
You could pair each item with its index using enumerate, then shuffle that.
>>> import random
>>> l = [4, 8, 15, 16, 23, 42]
>>> x = list(enumerate(l))
>>> random.shuffle(x)
>>> indices, l = zip(*x)
>>> l
(4, 8, 15, 23, 42, 16)
>>> indices
(0, 1, 2, 4, 5, 3)
One advantage of this approach is that it works regardless of whether l contains duplicates.
If your values are unique, just use the list.index method. For example, you can do this:
import random
l = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
start_l = l[:]
random.shuffle(l)
for elem in l:
print(elem, '->', start_l.index(elem))
Of course, in your example this is trivial - each element is already it's initial index.
# gives the same result as above.
l = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
random.shuffle(l)
for elem in l:
print(elem, '->', elem)
In fact, the best method depends strongly on what you want to do. If you have other data, it might be simplest to just shuffle indices, not data. This avoids any problems of duplication etc. Basically you get a permutation list, where each element is the index the position is shifted to. For example, [2, 1, 0] is the permutation for reversing a list.
l = list(random.randint(0, 10) for _ in range(10))
l_idx = list(range(len(l))) # list of indices in l
random.shuffle(l_idx)
for new_idx, old_idx in enumerate(l_idx):
print(l[old_idx], '#', old_idx, '->', new_idx)
A more intuitive alternative to the other answers:
Shuffle a range of indices, and use that to get a shuffled list of the original values.
To keep track of everything using a dictionary, one can do this:
Use enumerate in your dictionary comprehension to have index and value in your iteration, and then assign value as key, and index as value.
import random
l = [5, 3, 2, 0, 8, 7, 9, 6, 4, 1]
d = {v: i for i, v in enumerate(l)}
print(d) # current state
random.shuffle(l)
The advantage here is that you get O(1) lookup for retrieving your index for whatever value you are looking up.
However, if your list will contain duplicates, this answer from Kevin should be referred to.
Create a copy of the original list and shuffle the copy:
>>> import random
>>> l = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> l_copy = list(l) # <-- Creating copy of the list
>>> random.shuffle(l_copy) # <-- Shuffling the copy
>>> l_copy # <-- Shuffled copy
[8, 7, 1, 3, 6, 5, 9, 2, 0, 4]
>>> l # <-- original list
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>>