473,394 Members | 1,887 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,394 software developers and data experts.

Re: Is there any nice way to unpack a list of unknown size??

srinivasan srinivas wrote:
I want to do something like below:

1. first, second, third, *rest = foo

2. for (a,b,c,*rest) in list_of_lists:
update to Python 3.0 (as others have pointed out), or just do

first, second, third = foo[:3]
rest = foo[3:]

for item in list_of_lists:
a, b, c = item[:3]
rest = item[3:]
...

and move on to more interesting parts of your program.

</F>

Sep 14 '08 #1
1 1460
On 2008-09-14, Fredrik Lundh <fr*****@pythonware.comwrote:
>1. first, second, third, *rest = foo

2. for (a,b,c,*rest) in list_of_lists:

update to Python 3.0 (as others have pointed out), or just do

first, second, third = foo[:3]
rest = foo[3:]
Of course you can do that in one line if you want it to look a
bit more like the original pseudocode:

(a,b,c),rest = foo[:3],foo[3:]

That still requires you to manually count the number of
"non-rest" destination elements on the LHS and type that number
twice on the RHS. If you wanted to elminate a tiny bit of the
redundancy you could define a split() function:

def split(seq,n):
return seq[:n],seq[n:]

(a,b,c),rest = split(foo,3)

--
Grant

Sep 15 '08 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Geoffrey | last post by:
Hope someone can help. I am trying to read data from a file binary file and then unpack the data into python variables. Some of the data is store like this; xbuffer:...
38
by: jrlen balane | last post by:
basically what the code does is transmit data to a hardware and then receive data that the hardware will transmit. import serial import string import time from struct import * ser =...
5
by: grant | last post by:
Hi All, I am pretty new to python and am having a problem intepreting binary data using struct.unpack. I am reading a file containing binary packed data using open with "rb". All the values are...
6
by: g.franzkowiak | last post by:
Hello Everybody, I've read a pipe and store it in a object. My next step was the separation from 4 bytes with obj = string.join(list(dataObject) ==> '\x16 \x00 \x00 \x00' and the converting by...
24
by: markscala | last post by:
Problem: You have a list of unknown length, such as this: list = . You want to extract all and only the X's. You know the X's are all up front and you know that the item after the last X is...
4
by: OhKyu Yoon | last post by:
Hi! I have a really long binary file that I want to read. The way I am doing it now is: for i in xrange(N): # N is about 10,000,000 time = struct.unpack('=HHHH', infile.read(8)) # do...
3
by: echo | last post by:
i have never contacted to python language before this, i want to use the following py file to convert a pgm image to *.coe file, which is initial file of Xilinx memory.And this py file was written by...
0
by: Gary Herron | last post by:
Marlin Rowley wrote: Numpy can do this for you. First, do you really mean the array to contain lists of one string each? If so: kludge here array(, dtype='|S1') array(, ,
17
by: JRough | last post by:
I have used this function to create a string called $headers: function GetHeaders($file_name){ return "<th><a href='".$file_name."&order_by=l_e'>L_E</a></th> <th><a...
2
by: Gary Herron | last post by:
srinivasan srinivas wrote: Python 3.0 has exactly this feature. No current Python 2.x version has it. Gary Herron
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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...

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.