![]() |
ubuntu.com - launchpad.net - ubuntu help
|
|
|||||||
|
Programming Talk This forum is for all programming questions. The questions do not have to be directly related to Ubuntu and any programming language is allowed. |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Way Too Much Ubuntu
![]() Join Date: Dec 2005
Beans: 275
Ubuntu 9.04 Jaunty Jackalope
|
python - checking if any of the values in the list in a string
I have a list of stopwords.
is there an imediate way to check if a string contains any of those or do i have to loop it and test one by one? also, how do i lowercase a a string? Last edited by pedrotuga; January 4th, 2007 at 09:04 PM.. |
|
|
|
|
|
#2 |
|
A Carafe of Ubuntu
![]() Join Date: Apr 2006
My beans are hidden!
|
Re: python - checking if any of the values in the list in a string
Code:
stopwords = ["stop", "halt", "freeze"]
for word in stopwords:
if stopword in string:
return
Code:
string = "erDRTGrfg" string.lower()
__________________
KDE/Akonadi developer Last edited by Steveire; January 4th, 2007 at 09:27 PM.. |
|
|
|
|
|
#3 |
|
Ubuntu House Blend
![]() Join Date: Nov 2004
Location: Clemson, SC
Beans: 271
|
Re: python - checking if any of the values in the list in a string
or
Code:
stopwords=set('stop','halt','freeze')
stopwords.intersection(set(word.split()))
|
|
|
|
|
|
#4 | |
|
Iced Blended Vanilla Crème Ubuntu
![]() Join Date: Sep 2006
Beans: 2,719
|
Re: python - checking if any of the values in the list in a string
Quote:
Code:
stopwords = ["stop", "halt", "freeze"] if not word in stopwords: print "Word is not in stopwords" Code:
astring = "THIS STRING IS UPPER" #note: don't use "string" as a variable name. print astring.lowercase() |
|
|
|
|
|
|
#5 |
|
Way Too Much Ubuntu
![]() Join Date: Dec 2005
Beans: 275
Ubuntu 9.04 Jaunty Jackalope
|
Re: python - checking if any of the values in the list in a string
thanks everybody.
python cycle syntax is kind of elegant |
|
|
|
|
|
#6 |
|
Day Old Decaf
![]() Join Date: Jun 2006
Location: CT, USA
Beans: 5,268
Ubuntu 6.10 Edgy
|
Re: python - checking if any of the values in the list in a string
Why guess, it's not hard to time it yourself:
Program to time: Code:
import time
loopTimes = 10000000
stoplist = ["stop", "halt", "freeze"]
stopSet = set(["stop", "halt", "freeze"])
string = 'many differnet words which may containn stop or may not'
wordsSet = set(string.split())
def using_in(text, stopwords):
"return 1 (true) if any of the stopwords are in text -- using in"
for word in stopwords:
if word in text:
return 1
return 0
def using_set(wordsSet, stopSet):
"return 1 (true) if any of the stopwords are in text -- using sets"
return len( wordsSet & stopSet)
tStart = time.time()
for ii in range(loopTimes):
pass
t_empty = time.time() - tStart # empty loop
tStart = time.time()
for ii in range(loopTimes):
res = using_in(string, stoplist)
t_in = time.time() - tStart # using in
tStart = time.time()
for ii in range(loopTimes):
res = using_set(wordsSet, stopSet)
t_set = time.time() - tStart # using set
print loopTimes , 'using in:', t_in - t_empty
print loopTimes , 'using set:', t_set - t_empty
Code:
>>> ================================ RESTART 100 using in: 0.0 100 using set: 0.0 >>> ================================ RESTART 10000 using in: 0.0160000324249 10000 using set: 0.0149998664856 >>> ================================ RESTART 1000000 using in: 1.78200006485 1000000 using set: 0.983999967575 >>> ================================ RESTART 10000000 using in: 17.8279998302 10000000 using set: 9.82899999619 |
|
|
|
|
|
#7 |
|
Day Old Decaf
![]() Join Date: Jun 2006
Location: CT, USA
Beans: 5,268
Ubuntu 6.10 Edgy
|
Re: python - checking if any of the values in the list in a string
I was not happy with my previous solution: python mantra is that most obvious solution is the best. So i looked deeper. Sets approach has a little help: target string is preparsed to words. What I will preparse it for "in" approach too?
I added into obvious places these snippets: Code:
wordlist = string.split()
def using_inlist(wordlist, stopwords):
"using in from list"
for word in stopwords:
if word in wordlist:
return 1
return 0
tStart = time.time()
for ii in range(loopTimes):
res = using_inlist(wordlist, stoplist)
t_inlist = time.time() - tStart # using in list
print loopTimes , 'using in list:', t_inlist - t_empty
Code:
1000000 using in: 1.78099989891 1000000 using in list: 0.922000169754 1000000 using set: 0.952999830246 |
|
|
|
|
|
#8 |
|
Ubuntu Master Roaster
![]() Join Date: Jan 2006
My beans are hidden!
Ubuntu Jaunty Jackalope (testing)
|
Re: python - checking if any of the values in the list in a string
this problem is O(nm) complexity. you are matching all elements in one array to all elements in another array.
|
|
|
|
|
|
#9 |
|
5 Cups of Ubuntu
![]() Join Date: Aug 2006
Location: Atlanta, GA
Beans: 34
Ubuntu 7.04 Feisty Fawn
|
Re: python - checking if any of the values in the list in a string
Aw... Not to be a jerk but this is one of those moments where Ruby's Enumerable#include? would be sweet, right?
Code:
%w{list of stop words}.include?(word)
|
|
|
|
|
|
#10 |
|
Day Old Decaf
![]() Join Date: Jun 2006
Location: CT, USA
Beans: 5,268
Ubuntu 6.10 Edgy
|
Re: python - checking if any of the values in the list in a string
Code:
>>> stop_words = ['a', 'b', 'c', 'd'] >>> word = 'd' >>> word in stop_words True |
|
|
|
| Bookmarks |
| Thread Tools | |
| Display Modes | |
|
|