Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #28717
| Path | csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!news.musoftware.de!wum.musoftware.de!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail |
|---|---|
| From | tinnews@isbd.co.uk |
| Newsgroups | comp.lang.python |
| Subject | Re: How to print something only if it exists? |
| Date | Sat, 8 Sep 2012 11:02:46 +0100 |
| Lines | 34 |
| Message-ID | <6nhrh9-908.ln1@chris.zbmc.eu> (permalink) |
| References | <9s4nh9-8dr.ln1@chris.zbmc.eu> <mailman.319.1346955517.27098.python-list@python.org> |
| X-Trace | individual.net wE4Xbeu+pR8NCniNhPuSpABcCgv8GmQ64SPzBErfitzwyICl+hxbxe4a4AVtD6lMk= |
| X-Orig-Path | not-for-mail |
| Cancel-Lock | sha1:NFR4KNwyHgiodSounp3ZVStxImU= |
| User-Agent | tin/2.0.0-20110823 ("Ardenistiel") (UNIX) (Linux/3.2.0-30-generic (x86_64)) |
| Xref | csiph.com comp.lang.python:28717 |
Show key headers only | View raw
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
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
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
csiph-web