How can a program check if several numbers are in the same line?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • newtopython
    New Member
    • Feb 2010
    • 3

    #1

    How can a program check if several numbers are in the same line?

    using Python 2.6.1 on windows vista.

    I would like this search to check if all the numbers or words are in the line and add to the count if all are in the line. But if any one is not, go to the next line and do not add to count.
    It looks like the and would be perfect for this although I have a varying user input so I don’t know how I would use it if I don’t know how many are entered ahead of time or if I can use it with split.

    The program is only returning the last number entered now and seem to not take the others into consideration.
    Thanks for the help.

    Here is an example of what I am trying to do.

    Enter the search file to look in feb_time.txt
    Enter your search item 5 7 11

    sample of what would be in look_in file
    2 5 7 10 11 17 this would add to count
    5 12 7 17 11 22 this would add to count
    3 6 9 10 11 14 10 this would not add to count

    2 of your search was found

    This is my code
    Code:
     
    look_in = raw_input ("Enter the search file to look in ")
    search = raw_input ("Enter your search item ").split()
    
    txt = open(look_in)
    search2 = search
    line = txt.readline()
    while line:
        line = txt.readline()
    
        for item in search2:
            c = line.count(item)
        
        if c > 0: print "(",c,")", "Of your search was found"
        else: print item, "was not found"
  • bvdet
    Recognized Expert Specialist
    • Oct 2006
    • 2851

    #2
    You should iterate on the file object instead of using a while loop. Example:
    Code:
    f = open(file_name)
    for line in f:
        #do stuff
    f.close()
    I would set it up to use the in operator. You should split each line of the file and check to see if each search item is in the list created from the line. Example:
    Code:
    >>> lineList
    ['3', '6', '9', '10', '11', '14', '10']
    >>> searchList = ['3', '11', '15']
    >>> [item in lineList for item in searchList]
    [True, True, False]
    >>>
    Then you can test for membership of False in the resulting list as in
    Code:
    for line in fileObj:
        lineList = line.strip().split()
        if False not in [item in lineList for item in searchList]:
            print line

    Comment

    • newtopython
      New Member
      • Feb 2010
      • 3

      #3
      Thanks I will try and us for.

      Comment

      • newtopython
        New Member
        • Feb 2010
        • 3

        #4
        Thanks for the reply it is helpful.

        The file I am running through can have a couple hundred lines with numbers or times in it, should I still use the lineList with a file that large?

        Comment

        • bvdet
          Recognized Expert Specialist
          • Oct 2006
          • 2851

          #5
          You are only limited by your system resources - it should handle several hundred lines easily.

          Comment

          Working...