Python-Detects the inclusion of strings

Source: Internet
Author: User

Detects the inclusion between strings.

 

 

 

PythonCan detect the inclusion problem between strings.

ContainsAny, As long as it contains any character;

ContainsOnly, All characters are included;

ContainsAll, Including all;

 

Code:

 

#-*-Coding: UTF-8-*-''' Created on 2014.5.25 @ author: C. l. wang ''' has any ''' def ininsany (seq, aset): for c in seq: if c in aset: return True return Falseimport itertoolsdef containsAny2 (seq, aset): for item in itertools. ifilter (aset. _ contains __, seq): return True return Falsedef containsAny3 (seq, aset): return bool (set (aset ). intersection (seq) ''' all exist ''' def containsOnly (seq, aset): for c in seq: If c not in aset: return False return True ''' contains all ''' def containsAll (seq, aset): # print (set (aset ). difference (seq) return not set (aset ). difference (seq) import stringnotrans = string. maketrans ('','') def containsAny4 (astr, strset): return len (strset )! = Len (strset. translate (notrans, astr) def containsAll2 (astr, strset): return not strset. translate (notrans, astr) if _ name _ = '_ main _': L1 = [1, 2, 3, 4] L2 = [5, 6, 7, 8] L3 = [1, 4, 7, 10] print (L1 constains any in L2: + str (containsAny (L1, L2 ))) print (L1 constains any in L3: + str (containsAny (L1, L3) print (L1 constains any in L2 (2): + str (containsAny2 (L1, l2) print (L1 constains any in L3 (2): + str (containsAny2 (L1, L3) print (L1 constains any in L2 (3 ): + str (containsAny3 (L1, L2) print (L1 constains any in L3 (3): + str (containsAny3 (L1, L3) L4 = [1, 1, 2, 2, 3, 4] L5 = [1, 1, 2, 2, 3, 4, 5] print (L1 constains only in L4: + str (containsOnly (L1, l4) print (L1 constains only in L5: + str (containsOnly (L1, L5) print (L1 constains all in L4 (2 ): + str (containsAll (L1, L4) print (L1 constains all in L5 (2): + str (containsAll (L1, L5) pass

Output:

 

 

L1 constains any in L2 : FalseL1 constains any in L3 : TrueL1 constains any in L2 (2) : FalseL1 constains any in L3 (2) : TrueL1 constains any in L2 (3) : FalseL1 constains any in L3 (3) : TrueL1 constains only in L4 : TrueL1 constains only in L5 : TrueL1 constains all in L4 (2) : TrueL1 constains all in L5 (2) : False


 

 

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.