Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #28624

Re: How to print something only if it exists?

Path csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!news.mixmin.net!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <python-python-list@m.gmane.org>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.003
X-Spam-Evidence '*H*': 0.99; '*S*': 0.00; "'',": 0.07; 'exist,': 0.07; 'subject:How': 0.09; 'formatted': 0.09; 'length.': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; '10:59': 0.16; 'example).': 0.16; 'fld': 0.16; 'received:173.11': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'subject:exists': 0.16; '>>>': 0.18; 'appropriate': 0.20; 'elements': 0.23; 'split': 0.23; 'thus': 0.24; 'header:In-Reply- To:1': 0.25; 'header:User-Agent:1': 0.26; '(most': 0.27; 'set.': 0.27; "doesn't": 0.28; 'header:X-Complaints-To:1': 0.28; 'skip:( 20': 0.28; 'e.g.': 0.30; 'could': 0.32; 'print': 0.32; 'to:addr :python-list': 0.33; 'list': 0.35; 'exist': 0.35; 'subject:?': 0.35; 'there': 0.35; 'received:org': 0.36; 'but': 0.36; 'subject:: ': 0.38; 'some': 0.38; 'to:addr:python.org': 0.39; 'header:Received:5': 0.40; 'day,': 0.60; 'series': 0.63; 'more': 0.63; 'become': 0.65; "'2',": 0.84; "'3',": 0.84; 'balance,': 0.84; 'elements:': 0.84
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From Emile van Sebille <emile@fenx.com>
Subject Re: How to print something only if it exists?
Date Thu, 06 Sep 2012 11:19:09 -0700
References <9s4nh9-8dr.ln1@chris.zbmc.eu>
Mime-Version 1.0
Content-Type text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding 7bit
X-Gmane-NNTP-Posting-Host 173-11-108-137-sfba.hfc.comcastbusiness.net
User-Agent Mozilla/5.0 (Windows NT 5.1; rv:14.0) Gecko/20120713 Thunderbird/14.0
In-Reply-To <9s4nh9-8dr.ln1@chris.zbmc.eu>
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.318.1346955387.27098.python-list@python.org> (permalink)
Lines 25
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1346955387 news.xs4all.nl 6846 [2001:888:2000:d::a6]:39668
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:28624

Show key headers only | View raw


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

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


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