473,385 Members | 1,474 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,385 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 1459
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
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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...
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...

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.