Here's a little issue I run into more than I like: I often need to
unpack a sequence that may be too short or too long into a fixed-size
set of items:
a, b, c = seq # when seq = (1, 2, 3, 4, ...) or seq = (1, 2)
What I usually do is something like this:
a, b, c = (list(seq) + [None, None, None])[:3]
but that just feels rather ugly to me -- is there a good Pythonic
idiom for this?
Thx,
Rick 2 1925
On Dec 18, 12:49 pm, ram <rickmorri...@g mail.comwrote:
Here's a little issue I run into more than I like: I often need to
unpack a sequence that may be too short or too long into a fixed-size
set of items:
a, b, c = seq # when seq = (1, 2, 3, 4, ...) or seq = (1, 2)
What I usually do is something like this:
a, b, c = (list(seq) + [None, None, None])[:3]
but that just feels rather ugly to me -- is there a good Pythonic
idiom for this?
In terms of brevity I don't think so; however it can be done more
efficient and general (e.g. for infinite series) using itertools:
from itertools import islice, chain, repeat
def unpack(iterable , n, default=None):
return islice(chain(it erable,repeat(d efault)), n)
a, b, c = unpack(seq, 3)
George
ram <ri**********@g mail.comwrote:
Here's a little issue I run into more than I like: I often need to
unpack a sequence that may be too short or too long into a fixed-size
set of items:
a, b, c = seq # when seq = (1, 2, 3, 4, ...) or seq = (1, 2)
What I usually do is something like this:
a, b, c = (list(seq) + [None, None, None])[:3]
but that just feels rather ugly to me -- is there a good Pythonic
idiom for this?
Pythonic might be to be explicit: i.e. know in advance how long the
sequence actually is.
One drawback I see with your code is that it doesn't give you any way to
specify different defaults for the values. So here's an alternative to
consider: try passing your sequence to a function. This lets you specify
appropriate defaults, and it reads quite cleanly. Of course it also forces
you to extract the code using those variables out into a separate function,
but that may not be a bad thing.
>>def process(a=None, b=None, c=None):
print a, b, c
>>seq = iter('abcd') process(*iter tools.islice(se q,0,3))
a b c
>>seq = iter('ab') process(*iter tools.islice(se q,0,3))
a b None
>>>
This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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: '\x00\x00\xb9\x02\x13EXCLUDE_CREDIT_CARD'
# the above was printed using repr(xbuffer).
# Note that int(0x13) = 19 which is exactly the length of the visible
text
#
|
by: |
last post by:
How to call a function with variable argument list from another function
again with variable argument list?
Example :
double average ( int num, ... );
double AFunct1 ( int num, ... );
double AFunct2 ( int num, ... );
double average ( int num, ... )
|
by: ft_ml |
last post by:
Hello all,
I´m trying to unpack variable length strings.
In an application I receive a stream of bytes which contains a number n (2 bytes) and n strings of variable length. All strings ends whith a null.
I wrote the next piece of code:
sub GetStrings() {
my ($data) = @_;
|
by: tmp123 |
last post by:
Hello,
Thanks for your time.
After review the "struct" documentation, it seems there are no option
to pack/unpack zero terminated strings.
By example, if the packed data contains: byte + zero terminated string
+ zero terminated string + byte, it seems no possible to unpack it
using "struct".
|
by: eliben |
last post by:
Hello,
In a Python program I'm writing I need to dynamically generate
functions and store them in a dict. eval() can't work for me
because a function definition is a statement and not an expression, so
I'm using exec. At the moment I came up with the following to make it
work:
def build_func(args):
code """def foo(...)..."""
| |
by: mh |
last post by:
What's the best Python idiom for this C construct?
while ((x = next()) != END) {
....
}
Now I'm doing
x = next()
while x != END:
|
by: Jean-Paul Calderone |
last post by:
On Tue, 26 Aug 2008 16:42:30 GMT, mh@pixar.com wrote:
Use the magical second parameter to the `iter´ builtin:
for x in iter(next, END):
...
Jean-Paul
|
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 href='".$file_name."&order_by=carnumber'>Carnumber</a></th>
<th><a href='".$file_name."&order_by=location'>Location</a></th>
<th><a href='".$file_name."&order_by=sighting_date_asc'>Sighting
Date</a></th>
<th><a href='".$file_name."&order_by=classification'>Code</a></th>...
|
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 href='".
$file_name."&order_by=carnumber'>Carnumber</a></th>
<th><a href='".
$file_name."&order_by=location'>Location</a></th>
<th><a href='".
$file_name."&order_by=sighting_date_asc'>Sighting
|
by: Trups |
last post by:
HI,
I want to dynamically allocate array variable of type LPWSTR.
Code looks like this...
main() {
LPWSTR *wstr;
int count = Foo (wstr);
for (int i = 0; i < count; i++)
|
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
| |
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
| |
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |