Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > de.comp.lang.python > #4647
| Path | csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail |
|---|---|
| From | Mike Müller <mmueller@python-academy.de> |
| Newsgroups | de.comp.lang.python |
| Subject | Re: [Python-de] Wie sieht Python fuer mich aus? |
| Date | Tue, 10 Jan 2017 00:17:54 +0100 |
| Organization | Python Academy GmbH & Co. KG |
| Lines | 54 |
| Message-ID | <mailman.417.1484003879.2395.python-de@python.org> (permalink) |
| References | <Python-20170108184230@ram.dialup.fu-berlin.de> <0a1a079d-99e2-f353-9334-ad2b20344449@sschwarzer.net> <44f41e4c-945c-c35e-7d1e-370199c7cf8a@python-academy.de> |
| Reply-To | mmueller@python-academy.de |
| Mime-Version | 1.0 |
| Content-Type | text/plain; charset=windows-1252 |
| Content-Transfer-Encoding | 8bit |
| X-Trace | news.uni-berlin.de ySQ7/rdOg0WRwcFhcC7BBAOBE+68kVZDkIpy+KlXC+xA== |
| Return-Path | <mmueller@python-academy.de> |
| X-Original-To | python-de@python.org |
| Delivered-To | python-de@mail.python.org |
| User-Agent | Mozilla/5.0 (Macintosh; Intel Mac OS X 10.11; rv:45.0) Gecko/20100101 Thunderbird/45.6.0 |
| In-Reply-To | <0a1a079d-99e2-f353-9334-ad2b20344449@sschwarzer.net> |
| X-Authenticated-Sender | mmueller@python-academy.de |
| X-Virus-Scanned | Clear (ClamAV 0.99.2/22865/Mon Jan 9 21:06:59 2017) |
| X-BeenThere | python-de@python.org |
| X-Mailman-Version | 2.1.23 |
| Precedence | list |
| List-Id | Die Deutsche Python Mailingliste <python-de.python.org> |
| List-Unsubscribe | <https://mail.python.org/mailman/options/python-de>, <mailto:python-de-request@python.org?subject=unsubscribe> |
| List-Archive | <http://mail.python.org/pipermail/python-de/> |
| List-Post | <mailto:python-de@python.org> |
| List-Help | <mailto:python-de-request@python.org?subject=help> |
| List-Subscribe | <https://mail.python.org/mailman/listinfo/python-de>, <mailto:python-de-request@python.org?subject=subscribe> |
| X-Mailman-Original-Message-ID | <44f41e4c-945c-c35e-7d1e-370199c7cf8a@python-academy.de> |
| X-Mailman-Original-References | <Python-20170108184230@ram.dialup.fu-berlin.de> <0a1a079d-99e2-f353-9334-ad2b20344449@sschwarzer.net> |
| Xref | csiph.com de.comp.lang.python:4647 |
Show key headers only | View raw
Am 09.01.17 um 06:23 schrieb Stefan Schwarzer:
>
>> »range(2,7)« ergibt einen Bereich (einen Iterator) und
>> »range(2)« bedeutet »range(0,2)«.
>
> In Python 2 ergibt `range` eine Liste, in Python 3 einen
> Iterator.
Hier geht es etwas genauer. ;)
`range` in Python 3 gibt keinen Iterator sonder ein `range`-Objekt
zurück:
>>> type(range(10))
range
Wichtige Unterschiede:
* `range` unterstützt kein `next`:
>>> next(range(10))
TypeError: 'range' object is not an iterator
* `range`-Objekte lassen sich indizieren
>>> r = range(10)
>>> r[0]
0
Iteratoren nicht:
>>> i = iter('abc')
>>> i[0]
TypeError: 'str_iterator' object is not subscriptable
* `range`-Objekte lassen sich immer wieder in Listen (oder
andere Sequenzen) umwandeln:
>>> r = range(10)
>>> list(r)
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> list(r)
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
Itertoren sind dagegen "einmalig":
>>> i = iter('abc')
>>> list(i)
['a', 'b', 'c']
>>> list(i)
[]
Bei einer (typischen) Nutzung in einer `for`-Schleife:
>>> for x in range(10):
gibt es allerdings keine Unterschiede.
Mike
Back to de.comp.lang.python | Previous | Next | Find similar
Re: [Python-de] Wie sieht Python fuer mich aus? Mike Müller <mmueller@python-academy.de> - 2017-01-10 00:17 +0100
csiph-web