Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iterating over list of string and combining string values Python

I have a list of strings, like so:

    sixbit = ['000011', '000001', '010100', '100001']

I would like to iterate over this list and generate a new one that looks like this:

    eightbit = ['00001100', '00010101', '00100001']

For the purposes of illustration, len(sixbit) equals 4, because that translates nicely into eightbit. Ideally sixbit could be of any length -- it's fine if eightbit "cuts off" any remaining 1s or 0s.

Here is some code I've tried...sadly, it's way off but maybe you could see what I'm trying to do. The idea is 1) put all digits into one big string 2) loop over each digit and keep count, collecting digits into holder as you iterate 3) when count = 8 append to list 4) continue iterating

def compress_six_bit(ary):
    final_list = []
    holder = ''
    temp_string = ''
    encode_counter = 0

    for i in ary:
        holder = holder + i
        while encode_counter < 8:
            encode_counter = encode_counter + 1
            temp_string = temp_string + i
            final_list.append(temp_string)
        encode_counter = 0
        temp_string = ''

    return final_list
like image 854
hillmandj Avatar asked Dec 01 '25 08:12

hillmandj


1 Answers

>>> chained = itertools.chain.from_iterable(sixbit)
>>> [''.join(bits) for bits in itertools.izip(*[chained]*8)]
['00001100', '00010101', '00100001']

Explanation

chained is just an iterator of all the letters of the original strings. It uses chained function from itertools.

>>> chained = itertools.chain.from_iterable(sixbit)
>>> list(chained)
['0', '0', '0', '0', '1', '1', '0', '0', '0', '0', '0', '1', '0', '1', '0', '1', '0', '0', '1', '0', '0', '0', '0', '1']

[chained]*8 creates list containing the same chained object 8 times.

* just unpacks those 8 elements into izip parameters.

izip just return tuples, the first one of which contains the first letters of each chained iterator in the parameters, the second tuple contains second letters, etc. There are 8 chained objects, so there are 8 letters in each tuple.

Most importantly, the letters are taken from each iterator, but it is in fact 8 instances of the same iterator. And it is consumed by each call. So the first tuple contains the first 8 letters of the chained iterator.

>>> chained = itertools.chain.from_iterable(sixbit)
>>> list(itertools.izip(*[chained]*8))
[('0', '0', '0', '0', '1', '1', '0', '0'), ('0', '0', '0', '1', '0', '1', '0', '1'), ('0', '0', '1', '0', '0', '0', '0', '1')]

At the last step, we join them in list comprehension:

>>> chained = itertools.chain.from_iterable(sixbit)
>>> [''.join(bits) for bits in itertools.izip(*[chained]*8)]
['00001100', '00010101', '00100001']
like image 104
ovgolovin Avatar answered Dec 02 '25 22:12

ovgolovin