473,399 Members | 3,603 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

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
Expand|Select|Wrap|Line Numbers
  1.  
  2. look_in = raw_input ("Enter the search file to look in ")
  3. search = raw_input ("Enter your search item ").split()
  4.  
  5. txt = open(look_in)
  6. search2 = search
  7. line = txt.readline()
  8. while line:
  9.     line = txt.readline()
  10.  
  11.     for item in search2:
  12.         c = line.count(item)
  13.  
  14.     if c > 0: print "(",c,")", "Of your search was found"
  15.     else: print item, "was not found"
  16.  
Feb 23 '10 #1
4 1467
bvdet
2,851 Expert Mod 2GB
You should iterate on the file object instead of using a while loop. Example:
Expand|Select|Wrap|Line Numbers
  1. f = open(file_name)
  2. for line in f:
  3.     #do stuff
  4. 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:
Expand|Select|Wrap|Line Numbers
  1. >>> lineList
  2. ['3', '6', '9', '10', '11', '14', '10']
  3. >>> searchList = ['3', '11', '15']
  4. >>> [item in lineList for item in searchList]
  5. [True, True, False]
  6. >>> 
Then you can test for membership of False in the resulting list as in
Expand|Select|Wrap|Line Numbers
  1. for line in fileObj:
  2.     lineList = line.strip().split()
  3.     if False not in [item in lineList for item in searchList]:
  4.         print line
Feb 23 '10 #2
Thanks I will try and us for.
Feb 24 '10 #3
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?
Feb 24 '10 #4
bvdet
2,851 Expert Mod 2GB
You are only limited by your system resources - it should handle several hundred lines easily.
Feb 24 '10 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: muser | last post by:
Can anyone run this program through their compiler or if they can see a logical error please point it out. I have my tutor working on it at the moment but I would rather a less ambigious response...
6
by: hoover_richard | last post by:
I am a newbie to C++ and I need help with a simple program I am trying to write. My program is designed to print all of the odd integers contained in an array and output the sum of the odd...
27
by: Mark A. Nicolosi | last post by:
I've been trying to learn C for quite a while. But I've had trouble with the lack of good quality online text (some of it's alright). But I finally bought a book on C, Practical C. I like it...
10
by: Vince | last post by:
The following is code for the classic tortoise and hare C assignment. I am posting this for educational purposes only. Please do not plagiarize as comp sci instructors regularly read newsgroups....
7
by: Hulo | last post by:
In a C program I am required to enter three numbers (integers) e.g. 256 7 5 on execution of the program. C:\> 256 7 5 There should be spaces between the three numbers and on pressing "enter",...
9
by: ehabaziz2001 | last post by:
I am facing that error message with no idea WHY the reason ? "Abnormal program termination" E:\programs\c_lang\iti01\tc201\ch06\ownarr01o01 Enter a number : 25 More numbers (y/n)? y...
2
Banfa
by: Banfa | last post by:
Posted by Banfa The previous tutorial discussed what programming is, what we are trying to achieve, the answer being a list of instructions constituting a valid program. Now we will discuss how...
0
by: DesperateStdnt | last post by:
Basically, the program needs to take a data set of X (i.e. 80) students and assign them them into Y (i.e. 10) groups. Then assign Y (i.e. 10) number of faculty members, one to each group. But,...
0
amitpatel66
by: amitpatel66 | last post by:
There is always a requirement that in Oracle Applications, the Concurrent Program need to be execute programatically based on certain conditions/validations: Concurrent programs can be executed...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.