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


Groups > comp.lang.python > #101300

Re: insert many numbers to a list, a second method.

Path csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail
From Peter Otten <__peter__@web.de>
Newsgroups comp.lang.python
Subject Re: insert many numbers to a list, a second method.
Date Wed, 06 Jan 2016 14:56:20 +0100
Organization None
Lines 29
Message-ID <mailman.29.1452088595.2305.python-list@python.org> (permalink)
References <20160105133632.3C11C400C7@webmail.sinamail.sina.com.cn>
Mime-Version 1.0
Content-Type text/plain; charset="UTF-8"
Content-Transfer-Encoding 8Bit
X-Trace news.uni-berlin.de MS1B2F5a8m1XIfZ2TynVRAF/4vIPBlNde1jqdar/CXOg==
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.001
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'assignment': 0.07; '[0,': 0.09; 'assigning': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'similar,': 0.09; '[2,': 0.16; 'list1': 0.16; 'received:80.91.229.3': 0.16; 'received:dip0.t-ipconnect.de': 0.16; 'received:io': 0.16; 'received:plane.gmane.org': 0.16; 'received:psf.io': 0.16; 'received:t-ipconnect.de': 0.16; 'wrote:': 0.16; '>>>': 0.20; 'sorry,': 0.22; 'code.': 0.23; 'insert': 0.23; 'header:User- Agent:1': 0.26; "doesn't": 0.26; 'example': 0.26; 'subject:list': 0.26; 'header:X-Complaints-To:1': 0.26; 'sense': 0.26; 'right.': 0.27; 'subject:numbers': 0.29; 'position.': 0.30; 'another': 0.32; 'list': 0.34; 'list:': 0.35; 'to:addr:python-list': 0.36; 'subject:: ': 0.37; 'received:org': 0.37; 'to:addr:python.org': 0.40; 'received:de': 0.40; 'your': 0.60; 'sample': 0.63; 'homework': 0.84; 'too:': 0.84
X-Injected-Via-Gmane http://gmane.org/
X-Gmane-NNTP-Posting-Host p57bd8deb.dip0.t-ipconnect.de
User-Agent KNode/4.13.3
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.20+
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>
Xref csiph.com comp.lang.python:101300

Show key headers only | View raw


飛飛 wrote:

> l = list(range(0,12))
> numbers = [5,3,2,7]         #insert numbers at 5th position.
> list1 = list(range(5,9))
> list2 = list(range(0,5))
> list2.extend(numbers)     #
> for i in list1:
>     l.insert(i,list2[i])
> print(l)------>           l =  [0, 1, 2, 3, 4, 5, 3, 2, 7, 5, 6, 7, 8, 9,
> 10, 11]

Sorry, I cannot make sense of your sample code. If this is homework and your 
assignment is to find another way to insert items into a list have a look at 
slices. Example to get items three to five *out* of the list:

>>> items
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> items[2:5]
[2, 3, 4]

Size zero is legal, too:

>>> items[7:7]
[]

Assigning is similar, and the size of the slice on the left doesn't have to 
be the same as that of the list on the right. 

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


Thread

Re: insert many numbers to a list,  a second method. Peter Otten <__peter__@web.de> - 2016-01-06 14:56 +0100

csiph-web