checks if a character in a character set is included in a string
Task:
Checks if a character in a character set is present in the string
Solution:
Programme one:
ImportItertoolsdefContainany (seq,aset): forIteminchItertools.ifilter (Aset.__contains__, seq):returnTruereturnFalseif __name__=="__main__": L1= List ('python') L2= Set ('x') PrintContainany (L1,L2)
Scenario Two:
L1 = List ('python'= set ('df')def Containany (seq,aset): for inch seq: if inch return True return False print containany (L1,L2)
simplifying the use of translate methods for strings
Task:
The Translate method of string is used for fast encoding, and the String.maketrans function is simply encapsulated.
Solution:
ImportstringdefTranslator (frm ="', to ="', delete ="', keep =None):ifLen (To) = = 1: to= to*len (frm) trans=String.maketrans (frm, to)ifKeep is notNone:allchars= String.maketrans ("',"') Delete=allchars.translate (Allchars,keep.translate (allchars,delete))defTranslate (s):returns.translate (trans,delete)returnTranslate
The first chapter of the Python CookBook2 text-Check whether a string contains characters in a character set && the use of a translate method that simplifies the string