1

I'm having some problems figuring out a solution to this problem.

I want to read from a file on a per line basis and analyze whether that line has one of two characters (1 or 0). I then need to sum up the value of the line and also find the index value (location) of each of the "1" character instances.

so for example:

 1001

would result in:

line 1=(count:2, pos:[0,3])

I tried a lot of variations of something like this:

r=urllib.urlopen(remote-resouce)
list=[]
for line in lines:
    for m in re.finditer(r'1',line):
        list.append((m.start()))

I'm having two issues:

1) I thought that the best solution would be to iterate through each line and then use a regex finditer function. My issue here is that I keep failing to write a for loop that works. Despite my best efforts, I keep returning the results as one long list, rather than a multidimensional array of dictionaries.

Is this approach the right one? If so, how do I write the correct for loop?

If not, what else should I try?

3
  • 2
    You could show us your code... Commented Mar 1, 2011 at 17:40
  • What do you mean by "whether that line has one of two characters 0 and 1"? A line that contains only ones and zeroes? Commented Mar 1, 2011 at 17:52
  • In other words, a line contains only 1s and 0s. I want to identify how many 1s are in each line and where within each line they are located. unutbu has the right answer, but it's not working for some reason Commented Mar 1, 2011 at 18:16

3 Answers 3

4

Perhaps do it without regex:

import urllib
url='http://stackoverflow.com/questions/5158168/python-regex-question/5158341'
f=urllib.urlopen(url)
for linenum,line in enumerate(f):
    print(line)
    locations=[pos for pos,char in enumerate(line) if char=='1']
    print('line {n}=(count:{c}, pos:{l})'.format(
        n=linenum,
        c=len(locations),
        l=locations
        ))
Sign up to request clarification or add additional context in comments.

5 Comments

Hi unutbu, this all makes sense. Unfortunately, when i run it, nothing prints. The only change that I made was with respect to the first line. I'm pulling in a webpage, so I used urllib.urlopen. Otherwise I copied word for word
the issue seems to occur around line two, with enumerate f. When I run a regular for loop against the resource, it prints correctly. However, when I run enumerate against it, it prints a blank line.
@Brendan: I've modified the code to use urllib.urlopen. I put in an extra print statement for debugging purposes. If you still see no lines printed, please post the code and url you are using.
thanks unutbu, this was definitely correct. thanks for the great help!
You must remove the last part '#5158341' otherwise you get <HTML><HEAD><TITLE>Bad Request</TITLE>
1

Using regexes here is probably a bad idea. You can see if a 1 or 0 is in a line of text with '0' in line or '1' in line, and you can get the count with line.count('1').

Finding all of the locations of 1s does require iterating through the string, I believe.

Comments

0

Unubtu's code works fine. I tested it on a sample file which also has all 0's for a particular line. Here is the complete code -

#! /usr/bin/python
  2 
  3 # Write a program to read a text file which has 1's and 0's on each line
  4 # For each line count the number of 1's and their position and print it
  5 
  6 import sys
  7 
  8 def countones(infile):
  9   f = open(infile,'r')
 10   for linenum, line in enumerate(f):
 11     locations = [pos for pos,char in enumerate(line) if char == '1']
 12     print('line {n}=(count:{c}, pos:{l})'.format(n=linenum,c=len(locations),l=    locations))
 13 
 14 
 15 def main():
 16   infile = './countones.txt'
 17   countones(infile)
 18 
 19 # Standard boilerplate to call the main() function to begin the program
 20 if __name__ == '__main__':
 21   main()


Input file -

1001
110001
111111
00001
010101
00000

Result -

line 0=(count:2, pos:[0, 3])
line 1=(count:3, pos:[0, 1, 5])
line 2=(count:6, pos:[0, 1, 2, 3, 4, 5])
line 3=(count:1, pos:[4])
line 4=(count:3, pos:[1, 3, 5])
line 5=(count:0, pos:[])

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.