473,386 Members | 1,720 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

problem about list indexing

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
7 1130
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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

21
by: Hilde Roth | last post by:
This may have been asked before but I can't find it. If I have a rectangular list of lists, say, l = ,,], is there a handy syntax for retrieving the ith item of every sublist? I know about for i...
12
by: Steven Bethard | last post by:
So I need to do something like: for i in range(len(l)): for j in range(i+1, len(l)): # do something with (l, l) where I get all pairs of items in a list (where I'm thinking of pairs as sets,...
11
by: wASP | last post by:
Hi, I've got a pair of int properties in a class. The properties in question are indexing values - but that's not relevant to my problem - or it's just symptomatic ... sort of. They are...
65
by: Steven Watanabe | last post by:
I know that the standard idioms for clearing a list are: (1) mylist = (2) del mylist I guess I'm not in the "slicing frame of mind", as someone put it, but can someone explain what the...
5
by: micklee74 | last post by:
hi i have a list with contents like this alist = how can i "convert" this list into a dictionary such that dictionary = { '>QWER':'askfhs' , '>REWR' : 'sfsdf' , '>FGDG', 'sdfsdgffdgfdg' }
3
by: shark | last post by:
Hi all. i am facing a deadlock problem .i have included the -t1204 and -T3605 trace flags and have got the following o/p pu tin sqls server logs. 2006-06-01 17:49:21.84 spid4 2006-06-01...
3
by: Chris Lincoln | last post by:
I am currently working on a company site and have run into a problem with the search aspect of the site. I am running .net 2.0 and iis 5.1. The issue is as follows: Indexing Service is properly...
2
by: toton | last post by:
Hi, I am trying to use boost::range with one of my own container class, and having some problem. I am missing some usage of range. Can anyone suggest a proper way for it ? To show the problem...
4
by: Emin | last post by:
Dear Experts, How much slower is dict indexing vs. list indexing (or indexing into a numpy array)? I realize that looking up a value in a dict should be constant time, but does anyone have a...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.