Solved

which lists python

Posted on 2009-05-03
5
221 Views
Last Modified: 2012-05-06
Hi,

Very short question. How do you search (without using for loops) in which position of an array certain condition holds.

I.e., list = [3,4,5,2,4,3,5,6,3,2,1,0]
which(list < 2) = [10, 11] cause in positions 10 and 11 the elements of the list are less than 2.
0
Comment
Question by:dfernan
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 41

Expert Comment

by:HonorGod
ID: 24292059
Don't use "list" as a variable name.

L = [3,4,5,2,4,3,5,6,3,2,1,0]
L.index( 5 )

Tells the location of the list variable that contains the specified value.

Is this what you want?
0
 
LVL 41

Accepted Solution

by:
HonorGod earned 125 total points
ID: 24292081
List comprehensions kind of work, so

>>> L = [3,4,5,2,4,3,5,6,3,2,1,0]
>>> [ i for i in L if i < 2 ]
[1, 0]

But that gives us the values, not the index.  Combining them

>>> [ L.index( i ) for i in L if i < 2 ]
[10, 11]

appears to work.  Is that what you want?
0
 

Author Comment

by:dfernan
ID: 24292082
No. I want to check for a condition. L.which(>=5) ... index in the list where the values are bigger or equal to 5. In your case, [2,6,7] cause in those positions L is bigger or equal to 5.
0
 

Author Comment

by:dfernan
ID: 24292086
sorry now i read ur second answer! yes, that's what i want. thanks!
0
 
LVL 41

Expert Comment

by:HonorGod
ID: 24292101
Great.  Glad to be of some help.

Thanks for the grade & points.

Good luck & have a great day.
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The really strange introduction Once upon a time there were individuals who intentionally put the grass seeds to the soil with anticipation of solving their nutrition problems. Or they maybe only played with seeds and noticed what happened... Som…
Sequence is something that used to store data in it in very simple words. Let us just create a list first. To create a list first of all we need to give a name to our list which I have taken as “COURSE” followed by equals sign and finally enclosed …
Learn the basics of lists in Python. Lists, as their name suggests, are a means for ordering and storing values. : Lists are declared using brackets; for example: t = [1, 2, 3]: Lists may contain a mix of data types; for example: t = ['string', 1, T…
Learn the basics of if, else, and elif statements in Python 2.7. Use "if" statements to test a specified condition.: The structure of an if statement is as follows: (CODE) Use "else" statements to allow the execution of an alternative, if the …

617 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question