By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,222 Members | 2,354 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,222 IT Pros & Developers. It's quick & easy.

problem about list indexing

P: n/a
Hi, there

a = range(100)

if I want to use No 7, 11, 56,90 in a, then the only way I do is [a[7],
a[11], a[56], a[90]].
Is there any other way?

Thanks in advance.

Nov 26 '06 #1
Share this Question
Share on Google+
7 Replies


P: n/a
hollowspook wrote:
Hi, there

a = range(100)

if I want to use No 7, 11, 56,90 in a, then the only way I do is [a[7],
a[11], a[56], a[90]].
Is there any other way?
I presume a = range(100) is just an indication that a is a list -- the
literal answer to your question as asked is simply [7, 11, 56, 90]

In the general case that a is *any* list (or anything else that
supports the index protocol, e.g. a string, a tuple, an array.array
instance, or some instance of a so-written class), you can use a "list
comprehension". Example:

| >>a = 'qwertyuiopasdfghjklzxcvbnm'
| >>[a[x] for x in [25, 0, 13, 1]]
| ['m', 'q', 'f', 'w']

Do try and find "list comprehension" in the manual and in your book or
tutorial. It's useful for much more than the above.

HTH,
John

Nov 26 '06 #2

P: n/a
On Sun, 26 Nov 2006 00:25:13 -0800, hollowspook wrote:
Hi, there

a = range(100)

if I want to use No 7, 11, 56,90 in a, then the only way I do is [a[7],
a[11], a[56], a[90]].
Is there any other way?
a = [7, 11, 56, 90]

Are those numbers supposed to be in some sort of series? They aren't an
arithmetic series:

(11 - 7) = 4
(56 - 11) = 45 # not a constant difference

nor are they a geometric series:

(11/7) = 1.57
(56/11) = 5.09 # not a constant ratio

They don't look like some form of a Fibonacci series:

7+11 != 56
11+56 != 90

If they're just "random" numbers, plucked out of thin air, then you
probably can't calculate them and you'll need to just create them in a
list a = [7, 11, 56, 90].

--
Steven.

Nov 26 '06 #3

P: n/a
Thanks, John

how about indexing 1-7, 10
[range(1:8),10] will generate [[1, 2, 3, 4, 5, 6, 7], 10], instead of
[1, 2, 3, 4, 5, 6, 7, 10]
"John Machin д
"
hollowspook wrote:
Hi, there

a = range(100)

if I want to use No 7, 11, 56,90 in a, then the only way I do is [a[7],
a[11], a[56], a[90]].
Is there any other way?

I presume a = range(100) is just an indication that a is a list -- the
literal answer to your question as asked is simply [7, 11, 56, 90]

In the general case that a is *any* list (or anything else that
supports the index protocol, e.g. a string, a tuple, an array.array
instance, or some instance of a so-written class), you can use a "list
comprehension". Example:

| >>a = 'qwertyuiopasdfghjklzxcvbnm'
| >>[a[x] for x in [25, 0, 13, 1]]
| ['m', 'q', 'f', 'w']

Do try and find "list comprehension" in the manual and in your book or
tutorial. It's useful for much more than the above.

HTH,
John
Nov 26 '06 #4

P: n/a
hollowspook:
how about indexing 1-7, 10
[range(1:8),10] will generate [[1, 2, 3, 4, 5, 6, 7], 10], instead of
[1, 2, 3, 4, 5, 6, 7, 10]
(Note that range(1:8) is a syntax error).

You can join and extend lists as you like:
>>range(1, 8) + [10]
[1, 2, 3, 4, 5, 6, 7, 10]

See also the list.append and list.extend methods too.

Bye,
bearophile

Nov 26 '06 #5

P: n/a
Thanks, bearophile.
range(1, 8) + [10] is great!

"be************@lycos.com д
"
hollowspook:
how about indexing 1-7, 10
[range(1:8),10] will generate [[1, 2, 3, 4, 5, 6, 7], 10], instead of
[1, 2, 3, 4, 5, 6, 7, 10]

(Note that range(1:8) is a syntax error).

You can join and extend lists as you like:
>range(1, 8) + [10]
[1, 2, 3, 4, 5, 6, 7, 10]

See also the list.append and list.extend methods too.

Bye,
bearophile
Nov 26 '06 #6

P: n/a
ZeD
hollowspook wrote:
how about indexing 1-7, 10
[range(1:8),10] will generate [[1, 2, 3, 4, 5, 6, 7], 10], instead of
[1, 2, 3, 4, 5, 6, 7, 10]
>>range(1,8)+[10]
[1, 2, 3, 4, 5, 6, 7, 10]

--
Under construction
Nov 26 '06 #7

P: n/a
Steven D'Aprano wrote:
On Sun, 26 Nov 2006 00:25:13 -0800, hollowspook wrote:
Hi, there

a = range(100)

if I want to use No 7, 11, 56,90 in a, then the only way I do is [a[7],
a[11], a[56], a[90]].
Is there any other way?

a = [7, 11, 56, 90]

Are those numbers supposed to be in some sort of series? They aren't an
arithmetic series:

(11 - 7) = 4
(56 - 11) = 45 # not a constant difference

nor are they a geometric series:

(11/7) = 1.57
(56/11) = 5.09 # not a constant ratio

They don't look like some form of a Fibonacci series:

7+11 != 56
11+56 != 90

If they're just "random" numbers, plucked out of thin air, then you
probably can't calculate them and you'll need to just create them in a
list a = [7, 11, 56, 90].
Actually, it's a cubic polynomial ;-)

| >>def f(x):
| ... return (
| ... 7
| ... + 4 * x
| ... + 41 * x * (x - 1) // 2
| ... - 52 * x * (x - 1) * (x - 2) // 6
| ... )
| ...
| >>[f(x) for x in range(4)]
| [7, 11, 56, 90]

HTH,
John

Nov 26 '06 #8

This discussion thread is closed

Replies have been disabled for this discussion.