chwi
chwi

Reputation: 2802

Joining elements in a list to a new list in Python

I saw this question but it didn't help me much with my problem.

I have a list of strings, where the 12 first items are 20 bytes long (40 characters) and the 13'th is 19 bytes (38 characters), and this goes on for the rest of the list which is an arbitrary size (I am decoding an audio record which does not have a given size).

for packet in adpcm_packets[0:15]:
    print packet

output:

0000000FFF77F7FFF2A1412F4B8058A891982070
F1885C08883C7A880018980B7A50E24AA8004E4C
5D38103F3C910810009941B91088F7D509008980
009980809F7C28008880B7B935A88A0208118C3B
C251881F4C00B1408088A1398007F1980259C800
A108892278E879B383F04B0B50188191F1888189
A9608080980808080E7E40080989080018A860D3
F4C800001C7A100810C90B8302118980FC508923
B888D782BD8283B802902902A7CB589008802080
90097F3A8A061C81880B7B40A9249A008E022E6D
20B22D923E9690A80121AB97E4A8800013D5E5B8
490992228F02A14AC00A419990971B2888009988
8A2698289A70C00B039A3A74D2098AE7B18828
3F1C5201D3F1A000811081190988808080808008
0808800880080808800080008881880919880A10

Now, I want a new list where each element contains 13 packets, i.e 12*40 + 1*38 = 518 characters (259 bytes).

blocks.append(adpcm_packets[0])
    for packet in adpcm_packets[1:13]:
        blocks[0] += packet

and then I am stuck for how to do this with the rest of the list. How can I achieve this?

Upvotes: 2

Views: 176

Answers (4)

ssm
ssm

Reputation: 5373

The easiest thing is to use recursion, which is relatively easy to understand ...

def f(l):
    if l==[]: return []
    else: return [ ''.join(l[:13])  ] +  f( l[13:])  

Pythons way of handling lists allows you to not have to worry about the l[13:] and l[:13] part when len(l) < 13.

Note that this is potentially tail-recursive. Unfortunately, I am not sure if this is going to be truly tail-recursive in a Python interpreter, but most modern compilers are able to automatically do that for you. In Python you will have to invoke your own iterator method, which is kind of an overkill for a simple piece of code like this.

Upvotes: 0

Magnus Buvarp
Magnus Buvarp

Reputation: 976

Not completely sure I understand your question, but is it something like this you're looking for?

blocks = ["".join(adpcm_packets[i:i+13]) for i in range(0, len(adpcm_packets), 13)]

Upvotes: 2

timgeb
timgeb

Reputation: 78650

As far as I understand your question, you have list of strings and you want a new list of strings where each string is of a specified chunk length. A simple approach would be to join the list into a single string and then hack it up again. Demo:

>>> lst = ['hello', 'world', 'how', 'are', 'you?']
>>> mystr = ''.join(lst)
>>> chunk = 5
>>> newlst = [mystr[i:i+chunk] for i in range(0,len(mystr),chunk)]
>>> newlst
['hello', 'world', 'howar', 'eyou?']

Change your chunk to 518 and you should be good to go. Please leave a comment if I misunderstood your specifications.

Upvotes: 1

Daniel Roseman
Daniel Roseman

Reputation: 599490

The reason the linked question didn't help you is that your problem is not in any way to do with "joining" a list, but with partitioning a list. You just want to divide your list into separate list of 13 elements each. This question has some good solutions.

Upvotes: 0

Related Questions