Re: Performance: sets vs dicts.

2010-08-29 Thread Seth Rees

On 08/29/10 14:43, Peter Otten wrote:

John Nagle wrote:


 Is the "in" test faster for a dict or a set?
Is "frozenset" faster than "set"?  Use case is
for things like applying "in" on a list of 500 or so words
while checking a large body of text.


As Arnaud suspects: no significant difference:

$ python dictperf.py
dict -->  0.210289001465
set -->  0.202902793884
frozenset -->  0.198950052261

$ cat dictperf.py
import random
import timeit

with open("/usr/share/dict/words") as instream:
 words = [line.strip() for line in instream]

#random.seed(42)
sample = random.sample(words, 501)

n = sample.pop()
y = random.choice(sample)

d = dict.fromkeys(sample)
s = set(sample)
f = frozenset(sample)


for lookup in d, s, f:
 print type(lookup).__name__, "-->", timeit.timeit(
 "n in lookup; y in lookup",
 "from __main__ import lookup, n, y")

Peter

What about lists versus tuples?
--
http://mail.python.org/mailman/listinfo/python-list


Re: accessing a text file

2010-09-05 Thread Seth Rees
On 09/05/10 16:47, Baba wrote:
> level: beginner
> 
> how can i access the contents of a text file in Python?
> 
> i would like to compare a string (word) with the content of a text
> file (word_list). i want to see if word is in word_list. let's assume
> the TXT file is stored in the same directory as the PY file.
> 
> def is_valid_word(word, word_list)
> 
> 
> thanks
> Baba

   f = open('text.txt')
   data = f.read()
   # You may want to convert it to a list
   data = data.split()
   # Returns true if word is in data, false otherwise
   word in data
-- 
http://mail.python.org/mailman/listinfo/python-list