Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #63892
| Path | csiph.com!usenet.pasdenom.info!dedibox.gegeweb.org!gegeweb.eu!nntpfeed.proxad.net!proxad.net!feeder1-2.proxad.net!usenet-fr.net!nerim.net!novso.com!newsfeed.xs4all.nl!newsfeed1.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.000 |
| X-Spam-Evidence | '*H*': 1.00; '*S*': 0.00; 'operator': 0.03; 'win32': 0.03; 'syntax': 0.04; 'subject:Python': 0.06; 'indices': 0.07; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'way:': 0.09; 'python': 0.11; 'creates': 0.14; 'igor': 0.16; 'index.': 0.16; 'integers,': 0.16; 'itemgetter': 0.16; 'received:80.91.229.3': 0.16; 'received:dip0.t-ipconnect.de': 0.16; 'received:plane.gmane.org': 0.16; 'received:t-ipconnect.de': 0.16; 'sliced': 0.16; 'tuple': 0.16; 'tuple,': 0.16; 'typeerror:': 0.16; 'alpha': 0.16; 'elements': 0.16; 'wrote:': 0.18; 'variable': 0.18; 'bit': 0.19; 'split': 0.19; '>>>': 0.22; 'example': 0.22; 'import': 0.22; 'print': 0.22; 'creating': 0.23; 'header:User- Agent:1': 0.23; 'delta': 0.24; 'skip:" 20': 0.27; 'values': 0.27; 'header:X-Complaints-To:1': 0.27; '"",': 0.31; '>>>>': 0.31; 'perl': 0.31; 'file': 0.32; 'probably': 0.32; '(most': 0.33; 'test': 0.35; 'but': 0.35; 'picking': 0.36; 'doing': 0.36; 'subject:?': 0.36; 'list': 0.37; 'skip:o 20': 0.38; 'to:addr :python-list': 0.38; 'recent': 0.39; 'does': 0.39; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'skip:n 10': 0.64; 'more': 0.64; '2013,': 0.91; 'why?': 0.91 |
| X-Injected-Via-Gmane | http://gmane.org/ |
| To | python-list@python.org |
| From | Peter Otten <__peter__@web.de> |
| Subject | Re: What's correct Python syntax? |
| Date | Tue, 14 Jan 2014 12:33:26 +0100 |
| Organization | None |
| References | <mailman.5437.1389689219.18130.python-list@python.org> <cfd7f2f1-c8cb-4901-a6d7-630674ab20d4@googlegroups.com> <mailman.5439.1389691509.18130.python-list@python.org> <41a71031-be78-475f-bab2-822e35f909b7@googlegroups.com> <CA+FnnTxQ3kq_RWzAuDsaEXxPMmTWtYR_RMT-ShtzBRLKfrJ72w@mail.gmail.com> |
| Mime-Version | 1.0 |
| Content-Type | text/plain; charset="ISO-8859-1" |
| Content-Transfer-Encoding | 7Bit |
| X-Gmane-NNTP-Posting-Host | p508498d1.dip0.t-ipconnect.de |
| User-Agent | KNode/4.7.3 |
| 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 | <https://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 | <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.5451.1389699222.18130.python-list@python.org> (permalink) |
| Lines | 43 |
| NNTP-Posting-Host | 2001:888:2000:d::a6 |
| X-Trace | 1389699222 news.xs4all.nl 2930 [2001:888:2000:d::a6]:49309 |
| X-Complaints-To | abuse@xs4all.nl |
| Xref | csiph.com comp.lang.python:63892 |
Show key headers only | View raw
Igor Korot wrote:
> I am actually looking for a way to get a result from split which is
> sliced the way I want. Like in my example above.
> I mean I can probably make more variable by creating a tuple, but why?
> What is the purpose if I want only couple elements out of split.
> Doing it Perl way does not help:
>
> C:\Documents and Settings\Igor.FORDANWORK\Desktop\winpdb>python
> Python 2.7.5 (default, May 15 2013, 22:43:36) [MSC v.1500 32 bit
> (Intel)] on win32
> Type "help", "copyright", "credits" or "license" for more information.
>>>> test = "I,like,my,chocolate"
>>>> print test.split(',')[2,3]
> Traceback (most recent call last):
> File "<stdin>", line 1, in <module>
> TypeError: list indices must be integers, not tuple
>
> I can do it this way:
>
>>>> testlist = test.split(',')
>>>> print testlist[2]
> my
>
> but it will needlessly creates a list on which I will access by the index.
>
> Why? All I need is couple of values out of n-dimensional list (array).
Python has no dedicated syntax for picking arbitrary items from a list
If you are only concerned about printing use format():
>>> items = ["alpha", "beta", "gamma", "delta"]
>>> print "{1} {3} {0}".format(*items)
beta delta alpha
If you want to work with the values use operator.itemgetter():
>>> from operator import itemgetter
>>> itemgetter(1, 0, -1)(items)
('beta', 'alpha', 'delta')
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
What's correct Python syntax? Igor Korot <ikorot01@gmail.com> - 2014-01-14 00:46 -0800
Re: What's correct Python syntax? Rustom Mody <rustompmody@gmail.com> - 2014-01-14 00:54 -0800
Re: What's correct Python syntax? Igor Korot <ikorot01@gmail.com> - 2014-01-14 01:25 -0800
Re: What's correct Python syntax? Rustom Mody <rustompmody@gmail.com> - 2014-01-14 01:37 -0800
Re: What's correct Python syntax? Igor Korot <ikorot01@gmail.com> - 2014-01-14 02:02 -0800
Re: What's correct Python syntax? Rustom Mody <rustompmody@gmail.com> - 2014-01-14 02:16 -0800
Re: What's correct Python syntax? Igor Korot <ikorot01@gmail.com> - 2014-01-14 02:35 -0800
Re: What's correct Python syntax? Rustom Mody <rustompmody@gmail.com> - 2014-01-14 02:51 -0800
Re: What's correct Python syntax? Roy Smith <roy@panix.com> - 2014-01-14 08:47 -0500
Re: What's correct Python syntax? Peter Otten <__peter__@web.de> - 2014-01-14 12:33 +0100
Re: What's correct Python syntax? Ned Batchelder <ned@nedbatchelder.com> - 2014-01-14 07:19 -0500
Re: What's correct Python syntax? Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2014-01-14 08:58 -0500
Re: What's correct Python syntax? Mark Lawrence <breamoreboy@yahoo.co.uk> - 2014-01-14 09:37 +0000
Fwd: What's correct Python syntax? Igor Korot <ikorot01@gmail.com> - 2014-01-14 02:03 -0800
Re: Fwd: What's correct Python syntax? Larry Hudson <orgnut@yahoo.com> - 2014-01-14 22:00 -0800
Re: What's correct Python syntax? Alister <alister.ware@ntlworld.com> - 2014-01-14 10:59 +0000
Re: What's correct Python syntax? Roy Smith <roy@panix.com> - 2014-01-14 08:34 -0500
csiph-web