hi,

assume data looks like this:
cell Bit
0 1
1 X
2 1
3 0
4 X
5 X
6 X
7 X
8 1
9 X
10 0
11 0
12 X
13 X
14 1
15 1

And i want to group 4 bits into each group such as:
ROW1=01X1
ROW2=XXXX
ROW3=00X1
ROW4=11XX

how to do this?

thanks

Recommended Answers

All 12 Replies

Not so simple, but here is my solution based on simplified grouper as I used in my Sudoku post resently. More complete solution for grouping can be found in manual of itertools, using izip_longest.

def grouper(n, iterable): return zip(*((iter(iterable),) * n))

data = """cell Bit
0 1
1 X
2 1
3 0
4 X
5 X
6 X
7 X
8 1
9 X
10 0
11 0
12 X
13 X
14 1
15 1"""

bits = zip(*(row.split() for row in (data.splitlines()[1:])))
## let's check that we dropped the header and transposed with zip(*) properly
print bits
## bits needed to reverse still and we must reverse the result back
## let's do it by reversed and by taking slice of step -1 slice to demonstrate two ways
bits = list(grouper(4, reversed(bits[1])))[::-1]
print('\n'.join('ROW{index}={line}'.format(index=index+1, line=''.join(line)) for index,line in enumerate(bits)))

wow.. this is really complex. :)
i got this error after executing the code:
>>>
[('0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12', '13', '14', '15'), ('1', 'X', '1', '0', 'X', 'X', 'X', 'X', '1', 'X', '0', '0', 'X', 'X', '1', '1')]
Traceback (most recent call last):
File "C:\Python25\myscript\group\group.py", line 27, in <module>
print('\n'.join('ROW{index}={line}'.format(index=index+1, line=''.join(line)) for index,line in enumerate(bits)))
File "C:\Python25\myscript\group\group.py", line 27, in <genexpr>
print('\n'.join('ROW{index}={line}'.format(index=index+1, line=''.join(line)) for index,line in enumerate(bits)))
AttributeError: 'str' object has no attribute 'format'
>>>

pls advise.

AttributeError: 'str' object has no attribute 'format'
You are using an older version of Python. You can replace line 27 with this: print('\n'.join('ROW%d=%s'%(index+1,''.join(line)) for index,line in enumerate(bits)))

excellent this works!! :)

suppose data is inside a file (eg test.txt). modified the code to be:

def grouper(n, iterable): return zip(*((iter(iterable),) * n))

f=open('test.txt')
for line in f:

    bits = zip(*(row.split() for row in (line.splitlines()[1:])))
    ## let's check that we dropped the header and transposed with zip(*) properly
    print bits
    ## bits needed to reverse still and we must reverse the result back
    ## let's do it by reversed and by taking slice of step -1 slice to demonstrate two ways
    bits = list(grouper(4, reversed(bits[1])))[::-1]
    print('\n'.join('ROW%d=%s'%(index+1,''.join(line)) for index,line in enumerate(bits)))

but getting traceback error:
>>>
[]
Traceback (most recent call last):
File "C:/Python25/myscript/group/group4.py", line 11, in <module>
bits = list(grouper(4, reversed(bits[1])))[::-1]
IndexError: list index out of range
>>>

pls advise. tq

I don't understand why you left the splitlines even you have line ready. Try by replacing line.splitlines() with open('test.txt').

do you mean:
bits = zip(*(row.split() for row in (line.open('test.txt')[1:])))

Like this:

def grouper(n, iterable): return zip(*((iter(iterable),) * n))

f = open('test.txt')
next(f) # drop header
bits = zip(*(row.strip().split() for row in f))
bits = list(grouper(4, reversed(bits[1])))[::-1]
print('\n'.join('ROW%d=%s'%(index+1,''.join(line)) for index,line in enumerate(bits)))

i'm getting next(f) not defined. i'm using Python 2.5 and Win XP.

Traceback (most recent call last):
File "C:\Python25\myscript\group\group4.py", line 19, in <module>
next(f) # drop header
NameError: name 'next' is not defined

So if you wanted to get lines from an open file, what would you do in your version of Python? Hint: look up getlines() Seems to me as if you could do a little better at actually thinking about the program and what the error message is telling you.

Why to use so old Python? Use f.next() syntax. Preferably update to Python 2.7.1.

ha..ha... i guess Python 2.5 is more stable. :)
i guess getline is the equalvalent of next. there is also readline.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.