Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #43861
| Path | csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!news.stack.nl!newsfeed.xs4all.nl!newsfeed3.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; 'python,': 0.02; '16,': 0.03; 'element': 0.07; '22,': 0.09; 'explanation': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'python': 0.11; '2.7.3': 0.16; '23,': 0.16; '24,': 0.16; '32,': 0.16; '33,': 0.16; 'message-id:@post.gmane.org': 0.16; 'numpy': 0.16; 'obviously,': 0.16; 'received:80.91.229.3': 0.16; 'received:mediaways.net': 0.16; 'received:plane.gmane.org': 0.16; 'received:pool.mediaways.net': 0.16; 'thanks,': 0.17; '>>>': 0.22; 'header:User-Agent:1': 0.23; '31,': 0.24; 'skip:c 70': 0.24; '15,': 0.26; 'this:': 0.26; 'asking': 0.27; 'header:X-Complaints- To:1': 0.27; 'tried': 0.27; 'subject:list': 0.30; '25,': 0.31; 'vertical': 0.31; 'writes:': 0.31; 'alone': 0.33; 'but': 0.35; 'possible': 0.36; 'list.': 0.37; 'expected': 0.38; 'to:addr :python-list': 0.38; 'rather': 0.38; '12,': 0.39; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'simple,': 0.60; 'break': 0.61; 'first': 0.61; 'kind': 0.63; '30,': 0.65; '26,': 0.68 |
| X-Injected-Via-Gmane | http://gmane.org/ |
| To | python-list@python.org |
| From | Wolfgang Maier <wolfgang.maier@biologie.uni-freiburg.de> |
| Subject | Re: Strange behavior for a 2D list |
| Date | Thu, 18 Apr 2013 21:19:00 +0000 (UTC) |
| References | <CAJq7yh4Pc=Ge=AU=XDKQtpy3PLM-SYetBN2u+9V8PmY3B+RV2Q@mail.gmail.com> |
| Mime-Version | 1.0 |
| Content-Type | text/plain; charset=utf-8 |
| Content-Transfer-Encoding | 8bit |
| X-Gmane-NNTP-Posting-Host | sea.gmane.org |
| User-Agent | Loom/3.14 (http://gmane.org/) |
| X-Loom-IP | 77.2.142.54 (Mozilla/5.0 (Windows NT 6.2; WOW64; rv:20.0) Gecko/20100101 Firefox/20.0) |
| 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.797.1366319953.3114.python-list@python.org> (permalink) |
| Lines | 36 |
| NNTP-Posting-Host | 2001:888:2000:d::a6 |
| X-Trace | 1366319953 news.xs4all.nl 2221 [2001:888:2000:d::a6]:49804 |
| X-Complaints-To | abuse@xs4all.nl |
| Xref | csiph.com comp.lang.python:43861 |
Show key headers only | View raw
Robrecht W. Uyttenhove <ruyttenhove <at> gmail.com> writes:
>
> Hello,
> I tried out the following
code:y=[range(0,7),range(7,14),range(14,21),range(21,28),range(28,35)]
> >>> y[[0, 1, 2, 3, 4, 5, 6], [7, 8, 9, 10, 11, 12, 13],
> [14, 15, 16, 17, 18, 19, 20], [21, 22, 23, 24, 25, 26, 27], [28, >
29, 30, 31, 32, 33, 34]]
> >>> y[1:5:2][::3]
> [[7, 8, 9, 10, 11, 12, 13]]
> I expected the 2D list:[[ 7, 10, 13],
> [21, 24, 27]]
>
> Any ideas?
>
> Thanks,
> Rob
> PS: I used Python 2.7.3
>
The explanation is rather simple, just break up your complex slicing into
its parts:
y[1:5:2] => [[7, 8, 9, 10, 11, 12, 13],[21, 22, 23, 24, 25, 26, 27]]
and [::3] is asking for the first,4th,7th,... element from this list.
Obviously, only the first one's existing, so [7, 8, 9, 10, 11, 12, 13]
What you expected is kind of vertical slicing through the rows. I don't
think you can achieve this with slicing alone in standard Python, but it's
possible with numpy arrays.
In Python you will have to combine slicing with a comprehension, like this:
[x[::3] for x in y[1:5:2]]
Best,
Wolfgang
Back to comp.lang.python | Previous | Next | Find similar | Unroll thread
Re: Strange behavior for a 2D list Wolfgang Maier <wolfgang.maier@biologie.uni-freiburg.de> - 2013-04-18 21:19 +0000
csiph-web