Solved

which lists python

Posted on 2009-05-03
5
210 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 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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Strings in Python are the set of characters that, once defined, cannot be changed by any other method like replace. Even if we use the replace method it still does not modify the original string that we use, but just copies the string and then modif…
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…
Learn the basics of strings in Python: declaration, operations, indices, and slicing. Strings are declared with quotations; for example: s = "string": Strings are immutable.: Strings may be concatenated or multiplied using the addition and multiplic…
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 …

760 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now