chandra kanth Chandra
chandra kanth Chandra

Reputation: 149

Need help for Bettter logic to search sequence of strings in a list in python

I have a list containing integers where i need to check the String sequence in the list.

my list=[1,8,9,2,7,8,6,3,4] here i need to check 1,2,3,4 IS THERE IN THE LIST and after 1 ,2 should there in the list and then 3 in the list and 4 should be there after 3.

1,2,3,4 may not be required in sequence but whether 1,2,3,4 is there in the list need to be check and report if any one is missing.

    import os
    import re
    res=[1,8,9,2,7,8,6,3]
    cnt=0
    def check():
        global cnt
        for i in range(len(res)):
            if res[i]==1:
                cnt=1
                print "1"
            if cnt==1 and res[i]==2:
                 cnt=2
                 print "2"
            if cnt==2 and res[i]==3:
                  cnt=3
                  print "3"

             if cnt==3 and res[i]==4:
                  cnt=4
                  print "sequence correct"

        if cnt==0:
           print "1 not found"
           return                
        if cnt==1:
           print "not found"
           return
        if cnt==2:
           print "3 not found"
           return
        if cnt==3:
           print "4 not found"
           return


    c=check()
    print c

Can any one suggest me any better logic to do it more in a simple way.

Upvotes: 0

Views: 48

Answers (3)

Burhan Khalid
Burhan Khalid

Reputation: 174624

I understand that you have a list of numbers, and a string representing a sequence of numbers and your task is to find out if the sequence of numbers appears in the list.

Try this method, which does some smart checking with slicing:

>>> def find_seq(nums, seq, sep=','):
...     seq = map(int, seq.split(sep))
...     if seq[0] in nums:
...         return nums[nums.index(seq[0]):len(seq)+1] == seq
...     return False
...
>>> find_seq([1,2,3,4,5], '2,3')
True
>>> find_seq([1,2,3,4,5], '9')
False

Here is what it does:

  1. It converts your "search string" into a list of numbers (since your haystack is also a list of numbers)

  2. Then it tries to extract a list of the same size from the haystack and compare it with the search string.

There are a few issues with this approach, namely if the nums list has duplicates, then the index check will not match accurately - but I leave that up to you to decide how to optimize.


I need to check whether 2,3,4 present in list and that too out of 2,3,4 the number '2' should come first and after 3 and then '4' and in between these three values there may be any no of integers.

Okay, this is a different problem - how about this:

>>> def find_seq(nums, seq, sep=','):
...     seq = map(int, seq.split(sep))
...     results = [(nums.index(i), i) for i in seq if i in nums]
...     return len(results) == len(seq) and results == sorted(results, key=lambda x: x[0])
...
>>> find_seq([1,2,3,4,5], '2,3,4')
True
>>> find_seq([1,2,3,5,4], '2,3,4')
True
>>> find_seq([1,4,3,5,2], '2,3,4')
False

First, we search for the numbers in the sequence and find their positions in the nums list.

Then, we check for two things:

  1. If all numbers were found (the length of the results should be the same as the length of the sequence we are searching for)

  2. Then, to check if the numbers appear in sequence, we check if the results of our filtering are in ascending (increasing) order by the position. Since lists are 0-indexed, the order of items should be increasing from lowest to highest. If its not in this order, it means that the numbers are there, but not in the sequence we want.

Upvotes: 1

Harsha Biyani
Harsha Biyani

Reputation: 7268

You can try like this :

>>> my_list = [1, 8, 9, 2, 7, 8, 6, 3, 4]
>>> list_to_check = max(my_list)
>>> for i in range(1 , list_to_check+1):
...     if i not in my_list:
...        print str(i)+" not in list"
...        break
...
5 not in list

Upvotes: 0

Often Right
Often Right

Reputation: 425

I'm not entirely sure I understand what you're looking for, but would something like this work:

def check(number, lst):
 #checks if a specified number ('number') is in a list ('lst')
 if number in lst:
  return number
 else:
  return "Unable to find " + str(number)

def runChecks(numbers, lst):
 #runs multiple checks of a list based on a list of numbers ('numbers')
 for i in numbers:
  print check(i, lst)
aList = [1,8,9,2,7,8,6,3,4]
runChecks([1, 2, 3, 4], aList)

Upvotes: 0

Related Questions