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)          


你可能感兴趣的:(Python)