440,630 Members | 1,252 Online
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,630 IT Pros & Developers. It's quick & easy.

# list item's position

 P: n/a 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 Jul 18 '05 #1
7 Replies

 P: n/a Bob Smith wrote: 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[i] ... 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 Jul 18 '05 #2

 P: n/a 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[i] and 'baz' in bars[i]: .....: print bars[i] .....: 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 On Wed, 19 Jan 2005 22:04:44 -0500, Bob Smith wrote: 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 -- http://mail.python.org/mailman/listinfo/python-list Jul 18 '05 #3

 P: n/a 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 "Bob Smith" wrote in message news:cs**********@solaris.cc.vt.edu... 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 Jul 18 '05 #4

 P: n/a Bill Mill wrote: 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[i] and 'baz' in bars[i]: .....: print bars[i] .....: 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 Jul 18 '05 #5

 P: n/a On Wed, 19 Jan 2005 22:04:44 -0500, Bob Smith wrote: 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. Jul 18 '05 #6

 P: n/a On Wed, 19 Jan 2005 22:02:51 -0700, Steven Bethard wrote: See Mark's post, if you "need to know the index of something" this isthe 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')) [] Jul 18 '05 #7

 P: n/a On Wed, 19 Jan 2005 22:04:44 -0500, Bob Smith wrote: Hi,I have a Python list. I can't figure out how to find an element'snumeric 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 barThis finds the right bar, but not its list position. The reason I needto find its value is so I can remove every element in the list before itso that the bar I found somewhere in the list becomes element 0... doesthat make sense? sneaky (python 2.4) one-liner (not tested beyond what you see, and not recommended as the best self-documenting version ;-) bars = [ ... '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: newbar = list(dropwhile(lambda x: 'str_1' not in x or 'str_2' not in x, bars)) 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' Regards, Bengt Richter Jul 18 '05 #8

### This discussion thread is closed

Replies have been disabled for this discussion.