473,399 Members | 2,159 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

list item's position

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 2234
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
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
<bo*************@hotmail.com> 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
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" <bo*************@hotmail.com> 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
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
On Wed, 19 Jan 2005 22:04:44 -0500, Bob Smith
<bo*************@hotmail.com> 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
On Wed, 19 Jan 2005 22:02:51 -0700, Steven Bethard
<st************@gmail.com> wrote:

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')) []


Jul 18 '05 #7
On Wed, 19 Jan 2005 22:04:44 -0500, Bob Smith <bo*************@hotmail.com> 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?

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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Jess Austin | last post by:
hi, I like the way that Python does lists, and I love the way it does iterators. But I've decided I don't like what it does with iterators of lists. Lists are supposed to be mutable sequences,...
23
by: Stan Cook | last post by:
I was trying to take a list of files in a directory and remove all but the ".dbf" files. I used the following to try to remove the items, but they would not remove. Any help would be greatly...
10
by: Jason | last post by:
Hi, I have a few million data items being received by my program and I wish to store them in a list, with each item being inserted in any position in the list. Any performance tips so that my...
0
by: gabe_pu | last post by:
List Box Multiple Selections I have a list box on my page and I´m trying to add new items on it. The insert operation must be under the selected item. When I make the insertion it is done ...
5
by: Joe Fallon | last post by:
I have a list box with 7 text values in it. I have a pair of buttons to Move Up or Move Down the selected item one position. What is the simplest way to code the buttons so the item moves one...
1
by: akameswaran | last post by:
I have a list box, I'd like to generate a right click menu for items in the list box. The problem is unless I left click the item first, I can't figure out which item in the list I clicked over. ...
4
by: shrishjain | last post by:
Hi All, I need a type where I can store my items in sorted order. And I want to keep adding items to it, and want it to remain sorted. Is there any type in .net which I can make use of. I see...
7
by: Miguel E. | last post by:
Hi, I've been (self) studying Python for the past two months and I have had no background in OOP whatsoever. I was able to write an interactive program that randomly selects an item from a...
1
by: brian.newman | last post by:
I'm trying to link a gridview to another gridview in a Master-Details architecture. But the Details list isn't filtering like it is suppossed to. It will show all items on page load and it won't...
0
by: rahullko05 | last post by:
i have designed a menu list program in which i'm facing a problem where the last li item (white crappie) shifts down when i hover mouse pointer to just above li item (ozrack bazz) of white crappie...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.