当前位置:首页 > Python教程 > python技巧

Python学习笔记1209

将string中有元音字母的去掉,replace 函数

            1
            def
             anti_vowel(text):

            2
            for i in text:
3if i in"aeiouAEIOU":
4             text=text.replace(i,‘‘)
5return text

输入一个string,例如‘hello’,计算得分

             1 score = {"a": 1, "c": 3, "b": 3, "e": 1, "d": 2, "g": 2, 
 2"f": 4, "i": 1, "h": 4, "k": 5, "j": 8, "m": 3, 
 3"l": 1, "o": 1, "n": 1, "q": 10, "p": 3, "s": 1, 
 4"r": 1, "u": 1, "t": 1, "w": 4, "v": 4, "y": 4, 
 5"x": 8, "z": 10}
 6 7def scrabble_score(word):
 8     score_point=0
 9     word=word.lower()
10for i in word:
11         score_point+=score[i]
12return score_point

text的内容中,有word的部分,替换成**,*的个数跟word的个数有关。这个也可以用replace的函数来写,很简单。

            1
            def
             censor(text,word):

            2     text=text.split()
3for i in range(len(text)):
4if text[i]==word:
5             text[i]=**len(word)
6     str1=7     text=str1.join(text)
89return text

 

原文:http://www.cnblogs.com/batur/p/4154384.html


【说明】本文章由站长整理发布,文章内容不代表本站观点,如文中有侵权行为,请与本站客服联系(QQ:254677821)!

相关教程推荐

其他课程推荐