Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

which lists python

Posted on 2009-05-03
5
Medium Priority
?
225 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
  • 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 500 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

Article by: Swadhin
Introduction of Lists in Python: There are six built-in types of sequences. Lists and tuples are the most common one. In this article we will see how to use Lists in python and how we can utilize it while doing our own program. In general we can al…
The purpose of this article is to demonstrate how we can use conditional statements using Python.
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 …
Learn the basics of while and for loops in Python.  while loops are used for testing while, or until, a condition is met: The structure of a while loop is as follows:     while <condition>:         do something         repeate: The break statement m…
Suggested Courses
Course of the Month6 days, 10 hours left to enroll

783 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