1

I have a list with small and big numbers like this:

[234, 454, 123444, 123, 234, 122234, 234, 354, 654, 123231, 234, 342, 1231231]

The "big numbers" are at least 10 times bigger than the smaller numbers. In total, there are 4 "big numbers" in the list: 123444, 122234, 123231 and 1231231.

I want to find the indices of the 3rd and 4th "big numbers":

values: 234 454 123444 123 234 122234 234 354 654 123231 234 342 1231231 indices: 0 1 2 3 4 5 6 7 8 9 10 11 12

As you can see, the 3rd "big number" has the index 9, and the 4th "big number" has the index 12. Therefore the output should be 9 and 12.

Aran-Fey
  • 39,665
  • 11
  • 104
  • 149
Natiya
  • 463
  • 2
  • 9
  • 25

5 Answers5

4

Sorting is no good here since if a larger value long appears before a smaller value long, then the order is destroyed, as it does in this example(123444 > 123231). Instead using enumerate find items that are greater that 10x of the value of other items and append their index to a new list in the order of their appearance and then grab the 3rd and 4th items of the new list containing indexes

longs = []
j = min(l)

for idx, item in enumerate(l):
    if item/10 > j:
        longs.append(idx)

print(*longs[2:])

Or list comprehension

longs = [idx for idx, item in enumerate(l) if item/10 > j]
9 12
vash_the_stampede
  • 4,590
  • 1
  • 8
  • 20
-2

One way to achieve this would be to find the inflection point in the values in the list. This can be done by using a threshold factor for the same.

For example:

def inflection_index(a, threshold=10):
    a.sort()

    current = 0
    for next in range(1, len(a)):
        if a[next] >= threshold * a[current]:
            return i
        current += 1

    return None

More sophisticated methods include finding where the concavity of a curve changes according to the derivative. You can find out more about that here, here and here

Vedang Waradpande
  • 660
  • 1
  • 6
  • 8
-2

You could achieve it by sorting the list and then using reverse slice of index to find the index of largest values:

num = [234, 454, 123444, 123, 234, 122234, 234, 354, 654, 123231, 234, 342, 1231231]

temp = sorted(num)

for i in temp[::-1][:2]:
    print("Index of", i, "is:", num.index(i))

Result:

Index of 1231231 is: 12
Index of 123444 is: 2
Yash Ghorpade
  • 607
  • 1
  • 7
  • 16
-2
list = [234, 454, 123444, 123, 234, 122234, 234, 354, 654, 123231, 234, 342, 1231231]
print (list)
list.sort()
print (list)
my_len = len(list)
print (my_len)
print ("The longest ones are at the end")
print (list[my_len-1])
print (list[my_len-2])
# output
# [234, 454, 123444, 123, 234, 122234, 234, 354, 654, 123231, 234, 342, 1231231]
# [123, 234, 234, 234, 234, 342, 354, 454, 654, 122234, 123231, 123444, 1231231]
# 13
# The longest ones are at the end
# 1231231
# 123444

# Ok how about this 

list = [234, 454, 123444, 123, 234, 122234, 234, 354, 654, 123231, 234, 342, 1231231]
print (list)
my_new_list = []
for idx, val in enumerate(list):
    print(idx, val)
    my_new_list.append((idx,val))
    print(my_new_list)
# output
#[234, 454, 123444, 123, 234, 122234, 234, 354, 654, 123231, 234, 
#342, 1231231]
#0 234
#1 454
#2 123444
#3 123
#4 234
#5 122234
#6 234
#7 354
#8 654
#9 123231
#10 234
#11 342
#12 1231231
#[(0, 234), (1, 454), (2, 123444), (3, 123), (4, 234), (5, 122234), 
#(6, 234), (7, 354), (8, 654), (9, 123231), (10, 234), (11, 342), 
#(12, 1231231)]
Patrick B
  • 1
  • 2
-3

You can sort the elements first. Then for each of the largest elements find its index in the original list. Your should look like this:

original_list = [...]
sorted_list = original_list.sort()
1st_largest_index = original_list.index(sorted_list[-1])
2nd_largest_index = original_list.index(sorted_list[-2])
eiram_mahera
  • 950
  • 9
  • 25