python小知识

python使用随机数创建数组

import random

random_list = []
list_length = 20

while len(random_list) < list_length:

    random_list.append(random.randint(0,10))


定义函数

def printNum():
    num = 0
    while num < len(count_list):
        print  str(num) + "|" +str(count_list[num])
        num += 1


printNum()     


一行打印多个

print "Udacity! "*10


查找

def word_in_pos(word, parts_of_speech):
    for index in parts_of_speech:
        if index in word:
           return index
   
    return None
   

替换字符串  

parts_of_speech  = ["PLACE", "PERSON", "PLURALNOUN", "NOUN"]


test_string = """This is PLACE, no NOUN named PERSON, We have so many PLURALNOUN around here."""

def word_in_pos(word, parts_of_speech):
    for pos in parts_of_speech:
        if pos in word:
            return pos
    return None
        
def play_game(ml_string, parts_of_speech):    
    replaced = []
    ml_string = ml_string.split()
    for word in ml_string:
        replacement = word_in_pos(word,parts_of_speech) 
        if replacement != None:

         user_input = raw_input("Type in a: " + replacement + " ") 
           word = word.replace(replacement,"corgi")
           replaced.append(word)
        else:
           replaced.append(word)
    replaced = " ".join(replaced)  
    return replaced
    
print play_game(test_string, parts_of_speech)          


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值