list item's position

B

Bob Smith

Hi,

I have a Python list. I can't figure out how to find an element's
numeric value (0,1,2,3...) in the list. Here's an example of what I'm doing:

for bar in bars:
if 'str_1' in bar and 'str_2' in bar:
print bar

This finds the right bar, but not its list position. The reason I need
to find its value is so I can remove every element in the list before it
so that the bar I found somewhere in the list becomes element 0... does
that make sense?

Thanks,

Bob
 
M

Mark McEahern

Bob said:
Hi,

I have a Python list. I can't figure out how to find an element's
numeric value (0,1,2,3...) in the list. Here's an example of what I'm
doing:

Use enumerate() (new in Python 2.3, IIRC). Otherwise:

for i in range(len(sequence)):
item = sequence
...
for bar in bars:
if 'str_1' in bar and 'str_2' in bar:
print bar

This finds the right bar, but not its list position. The reason I need
to find its value is so I can remove every element in the list before
it so that the bar I found somewhere in the list becomes element 0...
does that make sense?

Sure. You want to slice the list starting at the index of the first
occurrence:

index = min([i for i, item in enumerate(sequence) if 'str_1' in item and
'str_2' in item])
print sequence[index:]

// m
 
B

Bill Mill

2 solutions:

In [98]: bars = ["str", "foobaz", "barbaz", "foobar"]

In [99]: for bar in bars:
....: if 'bar' in bar and 'baz' in bar:
....: print bar
....: print bars.index(bar)
....:
barbaz
2

In [100]: for i in range(len(bars)):
.....: if 'bar' in bars and 'baz' in bars:
.....: print bars
.....: print i
.....:
barbaz
2

The first one is slow and pretty, the second one is fast and (a bit)
ugly. I believe that you should avoid range(len(x)) when you can, but
use it when you need to know the index of something without an
additional x.index() call.

Peace
Bill Mill
bill.mill at gmail.com
 
S

Sean

Not sure if this is what you are looking for but...
li = ['this','is','a','list','of','strings']
li = [l for l in li if li.index(l) >= li.index('a')]
li ['a', 'list', 'of', 'strings']



--
Sean Berry ~ Internet Systems Programmer
BuildingOnline Inc.
The Building Industry's Web Design and Marketing Agency
Celebrating our 9th year in business, founded Aug. 1995
Ph: 888-496-6648 ~ Fax: 949-496-0036
--> Web Design Agency site: http://www.BuildingOnline.net
--> Building Industry Portal: http://www.BuildingOnline.com
--> Building Industry News: http://www.BuildingOnline.com/news/
--> Home Plans: http://www.eHomePlans.com
 
S

Steven Bethard

Bill said:
2 solutions:

In [98]: bars = ["str", "foobaz", "barbaz", "foobar"]

In [99]: for bar in bars:
....: if 'bar' in bar and 'baz' in bar:
....: print bar
....: print bars.index(bar)
....:
barbaz
2

In [100]: for i in range(len(bars)):
.....: if 'bar' in bars and 'baz' in bars:
.....: print bars
.....: print i
.....:
barbaz
2

The first one is slow and pretty, the second one is fast and (a bit)
ugly. I believe that you should avoid range(len(x)) when you can, but
use it when you need to know the index of something without an
additional x.index() call.


See Mark's post, if you "need to know the index of something" this is
the perfect case for enumerate (assuming you have at least Python 2.3):

py> bars = ["str", "foobaz", "barbaz", "foobar"]
py> for i, bar in enumerate(bars):
.... if 'bar' in bar and 'baz' in bar:
.... print bar
.... print i
....
barbaz
2

The only time where I even consider using range(len(x)) is when I don't
also need to look at the item -- which I find to be quite uncommon...

Steve
 
S

Stephen Thorne

Hi,

I have a Python list. I can't figure out how to find an element's
numeric value (0,1,2,3...) in the list. Here's an example of what I'm doing:

for bar in bars:
if 'str_1' in bar and 'str_2' in bar:
print bar

This finds the right bar, but not its list position. The reason I need
to find its value is so I can remove every element in the list before it
so that the bar I found somewhere in the list becomes element 0... does
that make sense?

Given a list and a function:

def dropPredicate(x):
return not 'somecontents' in x

mylist = ['a', 'b', 'c', 'xxxxsomecontentsxxx', 'd', 'e', 'f']

import itertools

mylist = list(itertools.dropwhile(dropPredicate, mylist))

assert mylist == ['xxxxsomecontentsxxx', 'd', 'e', 'f']

This will drop everything at the start of the list for which
'dropPredicate' returns true. This will mean that even if
dropPredicate returns false for more than one element of the list, it
will stop at the first element. If there are no elements for which
dropPredicate returns true, the result will be an empty list.

Regards,
Stephen Thorne.
 
J

John Machin

See Mark's post, if you "need to know the index of something" this is
the perfect case for enumerate (assuming you have at least Python 2.3):

But the OP (despite what he says) _doesn't_ need to know the index of
the first thingy containing both a bar and a baz, if all he wants to
do is remove earlier thingies.

def barbaz(iterable, bar, baz):
seq = iter(iterable)
for anobj in seq:
if bar in anobj and baz in anobj:
yield anobj
break
for anobj in seq:
yield anobj
import barbaz
bars = ["str", "foobaz", "barbaz", "foobar"]
print list(barbaz.barbaz(bars, 'bar', 'baz')) ['barbaz', 'foobar']
print list(barbaz.barbaz(bars, 'o', 'b')) ['foobaz', 'barbaz', 'foobar']
print list(barbaz.barbaz(bars, '', 'b')) ['foobaz', 'barbaz', 'foobar']
print list(barbaz.barbaz(bars, '', '')) ['str', 'foobaz', 'barbaz', 'foobar']
print list(barbaz.barbaz(bars, 'q', 'x')) []
 
B

Bengt Richter

Hi,

I have a Python list. I can't figure out how to find an element's
numeric value (0,1,2,3...) in the list. Here's an example of what I'm doing:

for bar in bars:
if 'str_1' in bar and 'str_2' in bar:
print bar

This finds the right bar, but not its list position. The reason I need
to find its value is so I can remove every element in the list before it
so that the bar I found somewhere in the list becomes element 0... does
that make sense?
sneaky (python 2.4) one-liner (not tested beyond what you see, and not recommended
as the best self-documenting version ;-)
... 'zero',
... 'one',
... 'str_1 and str_2 both in line two',
... 'three',
... 'four',
... 'str_1 and str_2 both in line five',
... 'last line']
>>> newbar=bars[sum(iter(('str_1' not in bar or 'str_2' not in bar for bar in bars).next, 0)):]
>>> for s in newbar: print repr(s)
...
'str_1 and str_2 both in line two'
'three'
'four'
'str_1 and str_2 both in line five'
'last line'

Alternatively:
>>> newbar=bars[[i for i,v in enumerate(bars) if 'str_1' in v and 'str_2' in v][0]:]
>>> for s in newbar: print repr(s)
...
'str_1 and str_2 both in line two'
'three'
'four'
'str_1 and str_2 both in line five'
'last line'

Alternatively:
...
'str_1 and str_2 both in line two'
'three'
'four'
'str_1 and str_2 both in line five'
'last line'

Regards,
Bengt Richter
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top