list index()

A

Alex Martelli

In my case of have done os.listdir() on two directories. I want to see
what files are in directory A that are not in directory B.

So why would you care about WHERE, in the listdir of B, are to be found
the files that are in A but not B?! You should call .index only if you
CARE about the position.

def inAnotB(A, B):
inA = os.listdir(A)
inBs = set(os.listdir(B))
return [f for f in inA if f not in inBs]

is the "one obvious way to do it" (the set(...) is just a simple and
powerful optimization -- checking membership in a set is roughly O(1),
while checking membership in a list of N items is O(N)...).


Alex
 
L

Lawrence D'Oliveiro

It's letting you know that the item isn't in the list. There's no
sensible return value from an "index" function in that condition.

How about returning None? That's what's already done by the dict.get method
by default.
 
?

=?ISO-8859-1?Q?Ricardo_Ar=E1oz?=

Alex said:
In my case of have done os.listdir() on two directories. I want to see
what files are in directory A that are not in directory B.

So why would you care about WHERE, in the listdir of B, are to be found
the files that are in A but not B?! You should call .index only if you
CARE about the position.

def inAnotB(A, B):
inA = os.listdir(A)
inBs = set(os.listdir(B))
return [f for f in inA if f not in inBs]

is the "one obvious way to do it" (the set(...) is just a simple and
powerful optimization -- checking membership in a set is roughly O(1),
while checking membership in a list of N items is O(N)...).

And what is the order of passing a list into a set? O(N)+?
 
A

Alex Martelli

Ricardo Aráoz said:
Alex said:
In my case of have done os.listdir() on two directories. I want to see
what files are in directory A that are not in directory B.

So why would you care about WHERE, in the listdir of B, are to be found
the files that are in A but not B?! You should call .index only if you
CARE about the position.

def inAnotB(A, B):
inA = os.listdir(A)
inBs = set(os.listdir(B))
return [f for f in inA if f not in inBs]

is the "one obvious way to do it" (the set(...) is just a simple and
powerful optimization -- checking membership in a set is roughly O(1),
while checking membership in a list of N items is O(N)...).

And what is the order of passing a list into a set? O(N)+?

Roughly O(N), yes (with the usual caveats about hashing costs, &c;-).
So, when A has M files and B has N, your total costs are roughly O(M+N)
instead of O(M*N) -- a really juicy improvement for large M and N!


Alex
 
Z

zzbbaadd

how its defined in Python is what is important in this context.

Is there a definition on python.org for an exception?
Genius. Why didn't anyone think of that before?

With an attitude like that Chris, don't expect to be getting an invite
to beta test my Python 3 fork.
 
Z

zzbbaadd

Either I'm misunderstanding what you mean or you need to get a clue
about what Python can already do before you go around making suggestions
for what Python needs. Lists have supported "in" tests since at least
version 1.5.2:
Well IN was what I was looking for and would have saved this thread.
However I don't believe IN showed up on the doc web page that has list
methods, where I found index().
 
M

mensanator

...


So why would you care about WHERE, in the listdir of B, are to be found
the files that are in A but not B?! You should call .index only if you
CARE about the position.


is the "one obvious way to do it" (the set(...) is just a simple and
powerful optimization -- checking membership in a set is roughly O(1),
while checking membership in a list of N items is O(N)...).

Why wouldn't "the one obvious way" be:

def inAnotB(A, B):
inA = set(os.listdir(A))
inBs = set(os.listdir(B))
return inA.difference(inBs)
?
 
C

Carsten Haese

Well IN was what I was looking for and would have saved this thread.
However I don't believe IN showed up on the doc web page that has
list methods, where I found index().

They're not on the exact same page, but index() is in section 3.6.4 of the
library reference (http://docs.python.org/lib/typesseq-mutable.html), whereas
"in" is in section 3.6 of the library reference
(http://docs.python.org/lib/typesseq.html). I'm wondering how you managed to
find subsection 3.6.4 without finding section 3.6.
 
C

Carsten Haese

With an attitude like that Chris, don't expect to be getting an
invite to beta test my Python 3 fork.

Welcome to comp.lang.python, where laziness is punished with brutal sarcasm.
 
T

TheFlyingDutchman

On Thu, 30 Aug 2007 20:17:00 -0700, zzbbaadd wrote


They're not on the exact same page, but index() is in section 3.6.4 of the
library reference (http://docs.python.org/lib/typesseq-mutable.html), whereas
"in" is in section 3.6 of the library reference
(http://docs.python.org/lib/typesseq.html). I'm wondering how you managed to
find subsection 3.6.4 without finding section 3.6.

www.google.com

search "python list methods"

first search find:

5. Data Structures
The list methods make it very easy to use a list as a stack, where the
last element added .... Another useful data type built into Python is
the dictionary. ...
http://docs.python.org/tut/node7.html

"The list data type has some more methods. Here are all of the methods
of list objects:"
 
T

Terry Reedy

| On Thu, 30 Aug 2007 17:09:36 +1000, Ben Finney wrote:
|
| > (e-mail address removed) writes:
| >
| >> What's with the index() function of lists throwing an exception on not
| >> found?
| >
| > It's letting you know that the item isn't in the list. There's no
| > sensible return value from an "index" function in that condition.
|
| What about -1? C programmers do this all the time. :)

Because they do not have exceptions.
 
H

Hendrik van Rooyen

Carsten Haese said:
.................. If we start labeling
people, this thread will earn you a label that rhymes with "roll".

weird this - maybe a native English speaker can comment -
when I pronounce what fishermen do - it rhymes with roll,
but when I am talking about the thing that lives under bridges
and munches goats, the "O" sound is shorter, and more
towards the back of my mouth.

- Hendrik
 
B

Bruno Desthuilliers

(e-mail address removed) a écrit :
On Aug 30, 12:09 am, Ben Finney <[email protected]>
wrote:
It's letting you know that the item isn't in the list. There's no
sensible return value from an "index" function in that condition.
for str:
find( sub[, start[, end]])
[...]
Return -1 if sub is not found.
-1 is used in other languages as well.
It is no more sensible there than in the 'str.find' method, which is a
historical wart.

One man's sensible is another man's insensible. For instance, some
people feel -1 as a valid index into a list is sensible. Other's find
it insensible.

Python 2.5.1 (r251:54863, May 2 2007, 16:56:35)
[GCC 4.1.2 (Ubuntu 4.1.2-0ubuntu4)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
 
E

Erik Max Francis

Hendrik said:
weird this - maybe a native English speaker can comment -
when I pronounce what fishermen do - it rhymes with roll,
but when I am talking about the thing that lives under bridges
and munches goats, the "O" sound is shorter, and more
towards the back of my mouth.

Native English accents vary as well, but _roll_ rhymes with _troll_, not
_trawl_. _Trawl_ would rhyme with _fall_, and _fall_ definitely doesn't
rhyme with _roll_.
 
P

Paddy

Native English accents vary as well, but _roll_ rhymes with _troll_, not
_trawl_. _Trawl_ would rhyme with _fall_, and _fall_ definitely doesn't
rhyme with _roll_.

--
Erik Max Francis && (e-mail address removed) &&http://www.alcyone.com/max/
San Jose, CA, USA && 37 20 N 121 53 W && AIM, Y!M erikmaxfrancis
I do not like work even when someone else does it.
-- Mark Twain

I say the 'oll' in troll like the 'ol' in frolic, and pronounce roll
and role similarly.

My accent is probably from the East Midlands of the UK, but is not
pronounced.

- Paddy.
 
R

Richie Hindle

[Carsten]
.................. If we start labeling
people, this thread will earn you a label that rhymes with "roll".
[Hendrik]
weird this - maybe a native English speaker can comment -
when I pronounce what fishermen do - it rhymes with roll,
but when I am talking about the thing that lives under bridges
and munches goats, the "O" sound is shorter, and more
towards the back of my mouth.

But - the word for someone who posts to the internet with the intention of
stirring up trouble derives from the word for what fishermen do, not from
the word for something that lives under a bridge. It derives from "trolling
for suckers" or "trolling for newbies".
 
E

Erik Max Francis

Paddy said:
I say the 'oll' in troll like the 'ol' in frolic, and pronounce roll
and role similarly.

My accent is probably from the East Midlands of the UK, but is not
pronounced.

_Troll_ and _frolic_ aren't pronounced with the same "o" sound in any
accent I've ever heard of. Which you pronounce _boat_ and _bot_ the
same way, too?
 
T

Tim Golden

Erik said:
_Troll_ and _frolic_ aren't pronounced with the same "o" sound in any
accent I've ever heard of. Which you pronounce _boat_ and _bot_ the
same way, too?

[Amusingly contemplating a trolling war about the pronunciation of "troll"]

Well they sound the same in my more-or-less South London accent.
I can't write those funny phonetic symbols (and I hate to
imagine the Unicode encoding hoops I'd have to jump through
to make them readable anyway) but both "o"s sound short to me.
Like "bot" rather than "boat" using your example.

TJG
 

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
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top