Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #28622 > unrolled thread
| Started by | tinnews@isbd.co.uk |
|---|---|
| First post | 2012-09-06 18:59 +0100 |
| Last post | 2012-09-13 17:48 +0000 |
| Articles | 9 — 7 participants |
Back to article view | Back to comp.lang.python
How to print something only if it exists? tinnews@isbd.co.uk - 2012-09-06 18:59 +0100
Re: How to print something only if it exists? Emile van Sebille <emile@fenx.com> - 2012-09-06 11:19 -0700
Re: How to print something only if it exists? Dave Angel <d@davea.name> - 2012-09-06 14:18 -0400
Re: How to print something only if it exists? tinnews@isbd.co.uk - 2012-09-08 11:02 +0100
Re: How to print something only if it exists? Dave Angel <d@davea.name> - 2012-09-08 07:08 -0400
Re: How to print something only if it exists? Terry Reedy <tjreedy@udel.edu> - 2012-09-06 15:34 -0400
Re: How to print something only if it exists? Hans Mulder <hansmu@xs4all.nl> - 2012-09-07 11:37 +0200
Re: How to print something only if it exists? Roy Smith <roy@panix.com> - 2012-09-07 09:16 -0400
RE: How to print something only if it exists? "Prasad, Ramit" <ramit.prasad@jpmorgan.com> - 2012-09-13 17:48 +0000
| From | tinnews@isbd.co.uk |
|---|---|
| Date | 2012-09-06 18:59 +0100 |
| Subject | How to print something only if it exists? |
| Message-ID | <9s4nh9-8dr.ln1@chris.zbmc.eu> |
I want to print a series of list elements some of which may not exist,
e.g. I have a line:-
print day, fld[1], balance, fld[2]
fld[2] doesn't always exist (fld is the result of a split) so the
print fails when it isn't set.
I know I could simply use an if but ultimately there may be more
elements of fld in the print and the print may well become more
complex (most like will be formatted for example). Thus it would be
good if there was some way to say "print this if it exists".
--
Chris Green
[toc] | [next] | [standalone]
| From | Emile van Sebille <emile@fenx.com> |
|---|---|
| Date | 2012-09-06 11:19 -0700 |
| Message-ID | <mailman.318.1346955387.27098.python-list@python.org> |
| In reply to | #28622 |
On 9/6/2012 10:59 AM tinnews@isbd.co.uk said...
> I want to print a series of list elements some of which may not exist,
> e.g. I have a line:-
>
> print day, fld[1], balance, fld[2]
>
> fld[2] doesn't always exist (fld is the result of a split) so the
> print fails when it isn't set.
>
> I know I could simply use an if but ultimately there may be more
> elements of fld in the print and the print may well become more
> complex (most like will be formatted for example). Thus it would be
> good if there was some way to say "print this if it exists".
>
You may be better off ensuring that fld is an appropriate length. One
way may be tweaking the split to return the right numbers of elements:
>>> T = "1,2,3"
>>> flds = (T+","*5).split(",")[:5]
>>> flds
['1', '2', '3', '', '']
Emile
[toc] | [prev] | [next] | [standalone]
| From | Dave Angel <d@davea.name> |
|---|---|
| Date | 2012-09-06 14:18 -0400 |
| Message-ID | <mailman.319.1346955517.27098.python-list@python.org> |
| In reply to | #28622 |
On 09/06/2012 01:59 PM, tinnews@isbd.co.uk wrote:
> I want to print a series of list elements some of which may not exist,
> e.g. I have a line:-
>
> print day, fld[1], balance, fld[2]
>
> fld[2] doesn't always exist (fld is the result of a split) so the
> print fails when it isn't set.
>
> I know I could simply use an if but ultimately there may be more
> elements of fld in the print and the print may well become more
> complex (most like will be formatted for example). Thus it would be
> good if there was some way to say "print this if it exists".
>
Would you like to define "exists" ? A list is not sparse, so all items
exist if their subscript is less than the length of the list. So all
you need to do is compare 2 to len(fld).
But perhaps there's another approach. Just what DO you want to print if
fld(1) exists, but fld(2) does not? Do you still want to print out day,
fld(1), and balance? Or do you want to skip balance as well?
if you literally want nothing printed for list elements beyond the end,
then I'd add some extra empty-strings to the end of the list.
fld.extend("" * 5)
Now, subscripts 0 through 4 inclusive will work, as specified.
Alternatively, perhaps there's some association between the day and the
fld(1), and the balance and the fld(2). in that case, probably you want
to make a loop out of it, and only print each pair if they're both
available. Something like:
for tag, value in zip((something, day, balance), fld):
print tag, value, ""
--
DaveA
[toc] | [prev] | [next] | [standalone]
| From | tinnews@isbd.co.uk |
|---|---|
| Date | 2012-09-08 11:02 +0100 |
| Message-ID | <6nhrh9-908.ln1@chris.zbmc.eu> |
| In reply to | #28625 |
Dave Angel <d@davea.name> wrote:
> Would you like to define "exists" ? A list is not sparse, so all items
> exist if their subscript is less than the length of the list. So all
> you need to do is compare 2 to len(fld).
>
Yes, a I said a simple len(fld) will tell me if fld[2] 'exists' but it
gets messy if I have to do it in the middle of the print sequence.
> But perhaps there's another approach. Just what DO you want to print if
> fld(1) exists, but fld(2) does not? Do you still want to print out day,
> fld(1), and balance? Or do you want to skip balance as well?
>
Here's a sample of the file whose lines are being split() :-
01 JB 0.00 Start of 2012, Initial balance
02 BB 0.00
13 ZB 0.00
I want to print out everything, it's just that in some cases there's
no descriptive text (the bit that ends up in fld[2]).
> if you literally want nothing printed for list elements beyond the end,
> then I'd add some extra empty-strings to the end of the list.
>
> fld.extend("" * 5)
>
> Now, subscripts 0 through 4 inclusive will work, as specified.
>
That's probably the simplest approach, thank you.
--
Chris Green
[toc] | [prev] | [next] | [standalone]
| From | Dave Angel <d@davea.name> |
|---|---|
| Date | 2012-09-08 07:08 -0400 |
| Message-ID | <mailman.378.1347102512.27098.python-list@python.org> |
| In reply to | #28717 |
On 09/08/2012 06:02 AM, tinnews@isbd.co.uk wrote:
> Dave Angel <d@davea.name> wrote:
>> Would you like to define "exists" ? A list is not sparse, so all items
>> exist if their subscript is less than the length of the list. So all
>> you need to do is compare 2 to len(fld).
>>
> Yes, a I said a simple len(fld) will tell me if fld[2] 'exists' but it
> gets messy if I have to do it in the middle of the print sequence.
>
>
>> But perhaps there's another approach. Just what DO you want to print if
>> fld(1) exists, but fld(2) does not? Do you still want to print out day,
>> fld(1), and balance? Or do you want to skip balance as well?
>>
> Here's a sample of the file whose lines are being split() :-
>
> 01 JB 0.00 Start of 2012, Initial balance
> 02 BB 0.00
> 13 ZB 0.00
>
> I want to print out everything, it's just that in some cases there's
> no descriptive text (the bit that ends up in fld[2]).
>
>
>> if you literally want nothing printed for list elements beyond the end,
>> then I'd add some extra empty-strings to the end of the list.
>>
>> fld.extend("" * 5)
>>
>> Now, subscripts 0 through 4 inclusive will work, as specified.
>>
> That's probably the simplest approach, thank you.
>
If there literally is only one missing field, and at the end, then you
could use fld.append("") instead. Or better, you could do something like
if len(fld) == 2 : fld.append(""")
This may be longer, but at least it's in one place -- the place where
the split is occurring. And it pretty much states that the fld2 is
optional.
You ought to consider what error to report if you encounter a line with
missing fields that ARE required.
--
DaveA
[toc] | [prev] | [next] | [standalone]
| From | Terry Reedy <tjreedy@udel.edu> |
|---|---|
| Date | 2012-09-06 15:34 -0400 |
| Message-ID | <mailman.322.1346960078.27098.python-list@python.org> |
| In reply to | #28622 |
On 9/6/2012 1:59 PM, tinnews@isbd.co.uk wrote: > I want to print a series of list elements some of which may not exist, > e.g. I have a line:- > > print day, fld[1], balance, fld[2] > > fld[2] doesn't always exist (fld is the result of a split) so the > print fails when it isn't set. What fails is the indexing operation. You can prevent that by slicing rather than indexing: fld[1:2], fld[2:3]. These will give '' if there is no fld[1], fld[2]. Whether or not this is sensible depends on what you want the output to look like in these cases. -- Terry Jan Reedy
[toc] | [prev] | [next] | [standalone]
| From | Hans Mulder <hansmu@xs4all.nl> |
|---|---|
| Date | 2012-09-07 11:37 +0200 |
| Message-ID | <5049c059$0$6936$e4fe514c@news2.news.xs4all.nl> |
| In reply to | #28622 |
On 6/09/12 19:59:05, tinnews@isbd.co.uk wrote:
> I want to print a series of list elements some of which may not exist,
> e.g. I have a line:-
>
> print day, fld[1], balance, fld[2]
>
> fld[2] doesn't always exist (fld is the result of a split) so the
> print fails when it isn't set.
How about:
print day, fld[1], balance, fld[2] if len(fld) > 2 else ''
If you really want to avoid the keyword 'if', then you'd have to
do something like:
print day, fld[1], balance, (fld[2:3] or [''])[0]
That may be shorter, but it isn't very readable.
Hope this helps,
-- HansM
[toc] | [prev] | [next] | [standalone]
| From | Roy Smith <roy@panix.com> |
|---|---|
| Date | 2012-09-07 09:16 -0400 |
| Message-ID | <roy-603586.09165507092012@news.panix.com> |
| In reply to | #28622 |
In article <9s4nh9-8dr.ln1@chris.zbmc.eu>, tinnews@isbd.co.uk wrote: > I want to print a series of list elements some of which may not exist, > e.g. I have a line:- > > print day, fld[1], balance, fld[2] > > fld[2] doesn't always exist (fld is the result of a split) so the > print fails when it isn't set. > > I know I could simply use an if but ultimately there may be more > elements of fld in the print and the print may well become more > complex (most like will be formatted for example). Thus it would be > good if there was some way to say "print this if it exists". One possible way is a trick I've used in the past. fld = split(...) + ['']*10 this guarantees that fld has at least 10 elements. If you want to guarantee that fld has *exactly* 10 elements, just take [0:10] of that.
[toc] | [prev] | [next] | [standalone]
| From | "Prasad, Ramit" <ramit.prasad@jpmorgan.com> |
|---|---|
| Date | 2012-09-13 17:48 +0000 |
| Message-ID | <mailman.625.1347559650.27098.python-list@python.org> |
| In reply to | #28622 |
tinnews@isbd.co.uk wrote: > I want to print a series of list elements some of which may not exist, > e.g. I have a line:- > > print day, fld[1], balance, fld[2] > > fld[2] doesn't always exist (fld is the result of a split) so the > print fails when it isn't set. > > I know I could simply use an if but ultimately there may be more > elements of fld in the print and the print may well become more > complex (most like will be formatted for example). Thus it would be > good if there was some way to say "print this if it exists". You can use an inline if-else statement. print day, fld[1], balance, fld[2] if len(fld) >= 3 else '' Ramit -- This email is confidential and subject to important disclaimers and conditions including on offers for the purchase or sale of securities, accuracy and completeness of information, viruses, confidentiality, legal privilege, and legal entity disclaimers, available at http://www.jpmorgan.com/pages/disclosures/email.
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web