Python Forum
Find index of missing number
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Find index of missing number
#1
Find missing number position / index based on the input

Here is the sample data.

def missing_numbers(num_list):
      original_list = [x for x in range(num_list[0], num_list[-1] + 1)]
      num_list = set(num_list)
      return (list(num_list ^ set(original_list)))

missing_numbers([1,2,3,4,6,7,10])
[5, 8, 9]
>>>
it returns 5 ,8,9, but how to say that the it is missing in the index of 4,7,8.

Output 1,2,3,4,5,6,7,8,9,10
Index 0,1,2,3,4,5,6,7,8,9
Reply
#2
You can obviously see pattern here: missing number - 1 = missing numbers index
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#3
Thanks !
What will happen if we have some thing like below.
missing_numbers([1,10,25,35,47,59,63,70])
This is sample data
missing_numbers([1,2,3,4,6,7,10])
Reply
#4
I suggest this:

def missing_numbers(num_list):
      original_list = [x for x in range(num_list[0], num_list[-1] + 1)]
      diff_list = []
      for index, elt in enumerate(original_list):
          if elt not in num_list:
              diff_list.append((elt, index))
      return diff_list
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  find the sum of a series of values that equal a number ancorte 1 493 Oct-30-2023, 05:41 AM
Last Post: Gribouillis
  find random numbers that are = to the first 2 number of a list. Frankduc 23 3,198 Apr-05-2023, 07:36 PM
Last Post: Frankduc
  Find if chain of characters or number Frankduc 4 1,796 Feb-11-2022, 01:55 PM
Last Post: Frankduc
Question Help to find the largest int number in a file directory SalzmannNicholas 1 1,628 Jan-13-2022, 05:22 PM
Last Post: ndc85430
  labels.append(self.classes.index(member.find('name').text)) hobbyist 1 1,912 Dec-15-2021, 01:53 PM
Last Post: deanhystad
  pandas pivot table: How to find count for each group in Index and Column JaneTan 0 3,298 Oct-23-2021, 04:35 AM
Last Post: JaneTan
  Regular expression: cannot find 1st number in a string Pavel_47 2 2,412 Jan-15-2021, 04:39 PM
Last Post: bowlofred
  Find specific subdir, open files and find specific lines that are missing from a file tester_V 8 3,568 Aug-25-2020, 01:52 AM
Last Post: tester_V
  str.find() not returning correct index. DreamingInsanity 10 4,210 Aug-18-2020, 05:41 PM
Last Post: DreamingInsanity
  Find index value in List Martin2998 3 2,771 May-12-2020, 02:17 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020