Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #27601 > unrolled thread

Re: asking

Started byIan Foote <ian@feete.org>
First post2012-08-22 04:42 +0100
Last post2012-08-21 22:51 -0700
Articles 2 — 2 participants

Back to article view | Back to comp.lang.python

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  Re: asking Ian Foote <ian@feete.org> - 2012-08-22 04:42 +0100
    Re: asking alex23 <wuwei23@gmail.com> - 2012-08-21 22:51 -0700

#27601 — Re: asking

FromIan Foote <ian@feete.org>
Date2012-08-22 04:42 +0100
SubjectRe: asking
Message-ID<mailman.3626.1345607418.4697.python-list@python.org>
On 22/08/12 03:57, mingqiang hu wrote:
> can I use just one statement to figure out if substring “a” ,"b" "c" 
> are in string "adfbdfc" ? not use the statement like
>
> ("a" in "adfbdfc") or ( "b" in "adfbdfc") or ("c" in "adfbdfc" ) 
> ,because if I have lots of substring, this could sucks

This might not be the most efficient way, but:

 >>> set("abc") <= set("adfbdfc")
True
 >>> set("abce") <= set("adfbdfc")
False

If you want to check for substrings longer than one character, this 
won't work. A solution then is to define a custom function:

def all_in(string, substrings):
for substring in substrings:
if substring not in string:
return False
return True

Ian

[toc] | [next] | [standalone]


#27606

Fromalex23 <wuwei23@gmail.com>
Date2012-08-21 22:51 -0700
Message-ID<224a0790-ecf8-4fac-bad0-7258e1b02444@j2g2000pbg.googlegroups.com>
In reply to#27601
On 22/08/12 03:57, mingqiang hu wrote:
> can I use just one statement to figure out if substring “a” ,"b" "c"
> are in string "adfbdfc" ? not use the statement like
> ("a" in "adfbdfc") or ( "b" in "adfbdfc") or ("c" in "adfbdfc" )
> ,because if I have lots of substring, this could sucks

subs = ['a', 'b', 'c']
string = 'abcdef'

def all_in(string, substrings):
    return all(map(string.__contains__, subs))

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web