Maws
Maws

Reputation: 243

How to Find all elements in an array that are greater than all elements

Expected out is {5:2, 10:1, 20:0}

Logical Code is below

myd = {}
#iterate through the list
for each in list_:
  #check the element not exist in dict
  if each not in myd:
    # check the greater than condition
    if each < other numbers:
      # if already exists
       myd[each] += 1
    else:
       myd[each] = 1

Upvotes: 0

Views: 367

Answers (6)

Kavindu Nilshan
Kavindu Nilshan

Reputation: 811

    # any list
    num = [10,5,20]
    num.sort()
    result = dict(zip(num,range(len(num)-1,-1,-1)))
    print(result)

This code can solve your problem. If only your list does not contain any duplicates. you can use the below code to solve your question if there are any duplicates or not.

    # any list
    num = [10, 5, 5, 1, 20]
    # removing duplicates
    arr = list(set(num))
    arr.sort()
    # count greater values for each number in list "arr"
    count = list(map(lambda x:len(list(filter(lambda y:x<y,num))),arr))
    result = dict(zip(arr,count))
    print(result)

Upvotes: 1

Patrick Artner
Patrick Artner

Reputation: 51643

And yet another one using a sorted list and enumeration - with the twist of using only the unique numbers to get to the correct outputs as

[1,1,1,1,5,5,5,5,5,5] # still has only 1 value smaller then 5 

so if we use the set of the list we got correct values.

from random import choices, seed

#     numbers = [1,5,5,10]
#     numbers = list(set(numbers))
seed(42)

stop_at = 100
k = choices(range(stop_at ), k = stop_at) # random perm numbers
k = sorted(set(k)) # only uniques

k.sort() # O(n*log(n))

lk = len(k)
d = {key:lk-idx-1 for idx,key in enumerate(k)}

print(d)

Output:

{0: 64, 2: 63, 3: 62, 4: 61, 6: 60, 7: 59, 8: 58, 9: 57, 10: 56, 
 15: 55, 16: 54, 17: 53, 19: 52, 20: 51, 21: 50, 22: 49, 23: 48, 
 24: 47, 26: 46, 27: 45, 28: 44, 31: 43, 33: 42, 34: 41, 36: 40, 
 37: 39, 38: 38, 39: 37, 42: 36, 45: 35, 50: 34, 52: 33, 53: 32, 
 54: 31, 55: 30, 56: 29, 57: 28, 58: 27, 60: 26, 61: 25, 62: 24, 
 63: 23, 64: 22, 65: 21, 67: 20, 68: 19, 69: 18, 70: 17, 72: 16, 
 73: 15, 77: 14, 79: 13, 80: 12, 82: 11, 84: 10, 86: 9, 87: 8, 
 89: 7, 91: 6, 93: 5, 94: 4, 95: 3, 97: 2, 98: 1, 99: 0}

Upvotes: 0

trincot
trincot

Reputation: 350272

You can get the sorted version of the list, iterate it and set the corresponding dictionary element to the reversed index (counting from the end) of the iterated value:

# Example input (including a duplicate)
lst = [1, 5, 5, 10]

last = len(lst) - 1
d = {}
for i, val in enumerate(sorted(lst)):
    d[val] = last - i

print(d)  # {1: 3, 5: 1, 10: 0}

Upvotes: 2

ThePyGuy
ThePyGuy

Reputation: 18416

Just use comprehension, and pass Boolean generator expression to sum function

>>> {v:sum(v<i for i in l) for v in l}
{10: 1, 5: 2, 20: 0}

You need to sort the resulting dictionary based on count later:

>>> dict(sorted(result.items(), reverse=True, key=lambda x: x[1]))
{5: 2, 10: 1, 20: 0}

You can also sort the list then iterate it using enumerate and then assign the number of elements remaining to be read.

l.sort()
result = {}
n = len(l)-1
for idx,v in enumerate(l):
    result[v] = n-idx
    
result
{5: 2, 10: 1, 20: 0}

PS: The second approach might fail if there are repeated numbers in the list (I have not tested it for duplicates).

Upvotes: 2

JuR
JuR

Reputation: 123

1st solution: by counting

list = np.array([5,10,20])
result = {}
for elmt in list:
   s = np.sum(list>elmt)
   result[elmt]=s

2nde solution: by sorting the list (only if there is no duplicate)

list = [5,10,20]
list_sorted = list.sort(reverse=True)
result={elmt:i for i, elmt in enumerate(list_sorted)}

Upvotes: 1

Deepak Tatyaji Ahire
Deepak Tatyaji Ahire

Reputation: 5309

Use the following algorithm:

1. Sort the list.
2. For index from 1 to N:
    greatest_index_of_current_element = findGreatestIndex[index]
    print(list[index], N - greatest_index_of_current_element)
    index = greatest_index_of_current_element + 1

Time Complexity: O (N * log(N))

Upvotes: 0

Related Questions