列表

mylist是一个多个变量组成的集合,每个变量可以存储不同的地址不同类型的变量的地址

mylist=["122",2.6,3,4,5,6,7]
print(id(mylist))
for  i  in range(len(mylist)):#len(求长度)
    print(mylist[i],id(mylist[i]))#每个元素都有一个地址,元素是变量,

更新列表原本和副本的区别

mylist=[1,2,3,4,5,6]
for  i  in range(len(mylist)): #修改列表必须索引
    if  mylist[i]==3:  #修改列表 ,mylist[i]是原本
        mylist[i]=9999


for   data in mylist: #修改失败,读取不修改用这个
    if data==2: #data是副本
        data=2222
        print(data)

mylist.append(1234) #增加
print(mylist)

当在for循环里面使用del时,记得break循环

'''
num=10
del  num #内存回收,变量不存在
print(num)
'''
mylist=[1,2,3,4,5,6]
for  i  in range(len(mylist)): #修改列表必须索引
    if  mylist[i]==3:  #修改列表 ,mylist[i]是原本
        del mylist[i]#删除一个元素,索引会变小,继续循环越界
        break #终止循环
print(mylist)

for   data in mylist: #修改失败,读取不修改用这个
    if data==2: #data是副本
        del data #对于删除列表无效
print(mylist)

三维类别的使用

mylist=[   [   [1,2],[3,4]  ],
           [   [5,6],[7,8]  ],
           [    [9,10],[11,12]  ]]

#print(mylist[0]) #列表第一个元素
#print(mylist[0][0]) #列表第一个元素的第一个元素
#print(mylist[0][0][0]) #列表第一个元素的第一个元素的第一个元素

for  i  in range(len(mylist)):
    #print(mylist[i])
    for j in range(len(mylist[i])):
        #print(mylist[i][j])
        for k  in range(len(mylist[i][j])):
            print(mylist[i][j][k])mylist1=[1,2,3,4,5,6]


'''
for  data1  in mylist:
    #print(data1)
    for data2 in data1:
        #print(data2)
        for data3 in data2:
            print(data3)

'''

列表常用方法

mylist=[1,2,3,4,5,6]
print(max(mylist)) #求最大
print(min(mylist)) #求最小

mytuple=(1,2,3,4,5) #元祖
mylistnew=list(mytuple) #转换
print(mytuple) #元祖
print(mylistnew) #列表

元组

mytuple1=(1,2,3)     #元组
mytuple2=1,2,3,4,5   #元组
print(type(mytuple1))
print(type(mytuple2))

mytuple3=(1) #规范(1) int
mytuple4=(1,)
print(type(mytuple3))
print(type(mytuple4))


mytuple5=1 #规范(1) int
mytuple6=1,
print(type(mytuple5))
print(type(mytuple6))

元素里面的值不能修改。

语音识别

语音识别框架

from win32com.client import constants
import os
import win32com.client
import pythoncom
class SpeechRecognition:
    def __init__(self, wordsToAdd):
        self.speaker = win32com.client.Dispatch("SAPI.SpVoice")
        self.listener = win32com.client.Dispatch("SAPI.SpSharedRecognizer")
        self.context = self.listener.CreateRecoContext()
        self.grammar = self.context.CreateGrammar()
        self.grammar.DictationSetState(0)
        self.wordsRule = self.grammar.Rules.Add("wordsRule", constants.SRATopLevel + constants.SRADynamic, 0)
        self.wordsRule.Clear()
        [self.wordsRule.InitialState.AddWordTransition(None, word) for word in wordsToAdd]
        self.grammar.Rules.Commit()
        self.grammar.CmdSetRuleState("wordsRule", 1)
        self.grammar.Rules.Commit()
        self.eventHandler = ContextEvents(self.context)
        self.say("Started successfully")
    def say(self, phrase):
        self.speaker.Speak(phrase)
class ContextEvents(win32com.client.getevents("SAPI.SpSharedRecoContext")):
    def OnRecognition(self, StreamNumber, StreamPosition, RecognitionType, Result):
        newResult = win32com.client.Dispatch(Result)
        print("小伙子你在说 ", newResult.PhraseInfo.GetText())

if __name__ == '__main__':
    wordsToAdd = ["关机", "取消关机", "记事本", "画图板","写字板"]
    speechReco = SpeechRecognition(wordsToAdd)
    while True:
        pythoncom.PumpWaitingMessages()

语音翻译

from win32com.client import constants
import os
import win32com.client
import pythoncom

speaker = win32com.client.Dispatch("SAPI.SPVOICE")


class SpeechRecognition:
    def __init__(self, wordsToAdd):
        self.speaker = win32com.client.Dispatch("SAPI.SpVoice")
        self.listener = win32com.client.Dispatch("SAPI.SpSharedRecognizer")
        self.context = self.listener.CreateRecoContext()
        self.grammar = self.context.CreateGrammar()
        self.grammar.DictationSetState(0)
        self.wordsRule = self.grammar.Rules.Add("wordsRule", constants.SRATopLevel + constants.SRADynamic, 0)
        self.wordsRule.Clear()
        [self.wordsRule.InitialState.AddWordTransition(None, word) for word in wordsToAdd]
        self.grammar.Rules.Commit()
        self.grammar.CmdSetRuleState("wordsRule", 1)
        self.grammar.Rules.Commit()
        self.eventHandler = ContextEvents(self.context)
        self.say("Started successfully")
    def say(self, phrase):
        self.speaker.Speak(phrase)
class ContextEvents(win32com.client.getevents("SAPI.SpSharedRecoContext")):
    def OnRecognition(self, StreamNumber, StreamPosition, RecognitionType, Result):
        newResult = win32com.client.Dispatch(Result)
        print("小伙子你在说 ", newResult.PhraseInfo.GetText())
        speechstr=newResult.PhraseInfo.GetText()
        if  speechstr=="赵大海":
            speaker.Speak("zhaodahai  love  fengjie")
        elif  speechstr=="你好":
            speaker.Speak("hello world")
        elif  speechstr=="国庆快乐":
            speaker.Speak("Happy   nationalday")
        elif  speechstr=="新年快乐":
            speaker.Speak("happy  New Year")
        elif  speechstr=="赵琳":
            speaker.Speak("a  beauty baby")
        elif  speechstr=="王涛":
            speaker.Speak("a  little boy")
        elif  speechstr=="彭彪":
            speaker.Speak("a  boy  can  coding")
        elif  speechstr=="马剑":
            speaker.Speak("shit,  horse")
        elif  speechstr=="孟勋":
            speaker.Speak("go go  go")
        elif  speechstr=="徐振涛":
                speaker.Speak("a  boy  in the  sky")
        elif  speechstr=="陈小平":
            speaker.Speak("strong  man  ")
        else:
            pass

if __name__ == '__main__':

    speaker.Speak("语音识别开启")
    wordsToAdd = ["赵大海",
                  "你好",
                  "国庆快乐",
                  "新年快乐",
                  "王涛",
                  "赵琳",
                  "彭彪",
                  "马剑",
                  "孟勋"  ,
                   "徐振涛",
                  "陈小平"]
    speechReco = SpeechRecognition(wordsToAdd)
    while True:
        pythoncom.PumpWaitingMessages()

语音控制

from win32com.client import constants
import os
import win32com.client
import pythoncom
class SpeechRecognition:
    def __init__(self, wordsToAdd):
        self.speaker = win32com.client.Dispatch("SAPI.SpVoice")
        self.listener = win32com.client.Dispatch("SAPI.SpSharedRecognizer")
        self.context = self.listener.CreateRecoContext()
        self.grammar = self.context.CreateGrammar()
        self.grammar.DictationSetState(0)
        self.wordsRule = self.grammar.Rules.Add("wordsRule", constants.SRATopLevel + constants.SRADynamic, 0)
        self.wordsRule.Clear()
        [self.wordsRule.InitialState.AddWordTransition(None, word) for word in wordsToAdd]
        self.grammar.Rules.Commit()
        self.grammar.CmdSetRuleState("wordsRule", 1)
        self.grammar.Rules.Commit()
        self.eventHandler = ContextEvents(self.context)
        self.say("Started successfully")
    def say(self, phrase):
        self.speaker.Speak(phrase)
class ContextEvents(win32com.client.getevents("SAPI.SpSharedRecoContext")):
    def OnRecognition(self, StreamNumber, StreamPosition, RecognitionType, Result):
        newResult = win32com.client.Dispatch(Result)
        print("小伙子你在说 ", newResult.PhraseInfo.GetText())
        speechstr=newResult.PhraseInfo.GetText()#识别的文本
        if  speechstr=="关机":
            os.system("shutdown -s -t 300")
        elif  speechstr=="取消关机":
            os.system("shutdown -a")
        elif speechstr=="记事本":
            os.system("notepad")
        elif speechstr=="写字板":
            os.system("write")
        elif speechstr=="画图板":
            os.system("mspaint")
        elif speechstr=="关闭记事本":
            os.system("taskkill /f   /im  notepad.exe")
        elif  speechstr=="设置":
            os.system("msconfig")
        else:
            print("我没听到你说啥半月刊")


if __name__ == '__main__':
    wordsToAdd = ["关机", "取消关机", "记事本", "画图板","写字板","设置","关闭记事本"]
    speechReco = SpeechRecognition(wordsToAdd)
    while True:
        pythoncom.PumpWaitingMessages()

语音对话

from win32com.client import constants
import os
import win32com.client
import pythoncom

speaker = win32com.client.Dispatch("SAPI.SPVOICE")


class SpeechRecognition:
    def __init__(self, wordsToAdd):
        self.speaker = win32com.client.Dispatch("SAPI.SpVoice")
        self.listener = win32com.client.Dispatch("SAPI.SpSharedRecognizer")
        self.context = self.listener.CreateRecoContext()
        self.grammar = self.context.CreateGrammar()
        self.grammar.DictationSetState(0)
        self.wordsRule = self.grammar.Rules.Add("wordsRule", constants.SRATopLevel + constants.SRADynamic, 0)
        self.wordsRule.Clear()
        [self.wordsRule.InitialState.AddWordTransition(None, word) for word in wordsToAdd]
        self.grammar.Rules.Commit()
        self.grammar.CmdSetRuleState("wordsRule", 1)
        self.grammar.Rules.Commit()
        self.eventHandler = ContextEvents(self.context)
        self.say("Started successfully")
    def say(self, phrase):
        self.speaker.Speak(phrase)
class ContextEvents(win32com.client.getevents("SAPI.SpSharedRecoContext")):
    def OnRecognition(self, StreamNumber, StreamPosition, RecognitionType, Result):
        newResult = win32com.client.Dispatch(Result)
        print("小伙子你在说 ", newResult.PhraseInfo.GetText())
        speechstr=newResult.PhraseInfo.GetText()
        if  speechstr=="赵大海":
            speaker.Speak("赵大海,你愿意抱着凤姐,面朝大海,春暖花开")
        elif  speechstr=="你好":
            speaker.Speak("好你妹")
        elif  speechstr=="国庆快乐":
            speaker.Speak("国庆快乐,快乐的要死")
        elif  speechstr=="新年快乐":
            speaker.Speak("新年happy")
        elif  speechstr=="赵琳":
            speaker.Speak("来自唐山的大美女,")
        elif  speechstr=="王涛":
            speaker.Speak("你的波涛涌用")
        elif  speechstr=="彭彪":
            speaker.Speak("彭彪你身上很多彪子吗")
        elif  speechstr=="马剑":
            speaker.Speak("马剑为什么你不学刀枪非要学剑")
        elif  speechstr=="孟勋":
            speaker.Speak("孟勋,你要建立不世的功勋么")
        elif  speechstr=="徐振涛":
            speaker.Speak("涛哥,你的波涛汹涌")
        elif  speechstr=="陈小平":
            speaker.Speak("小平,你好,为啥你不姓徐")
        else:
            pass

if __name__ == '__main__':

    speaker.Speak("语音识别开启")
    wordsToAdd = ["赵大海",
                  "你好",
                  "国庆快乐",
                  "新年快乐",
                  "王涛",
                  "赵琳",
                  "彭彪",
                  "马剑",
                  "孟勋"  ,
                   "徐振涛",
                  "陈小平"]
    speechReco = SpeechRecognition(wordsToAdd)
    while True:
        pythoncom.PumpWaitingMessages()

语音控制QQ

from win32com.client import constants
import os
import win32com.client
import pythoncom
import  win32com
import  win32con
import  win32gui
speaker = win32com.client.Dispatch("SAPI.SPVOICE")


class SpeechRecognition:
    def __init__(self, wordsToAdd):
        self.speaker = win32com.client.Dispatch("SAPI.SpVoice")
        self.listener = win32com.client.Dispatch("SAPI.SpSharedRecognizer")
        self.context = self.listener.CreateRecoContext()
        self.grammar = self.context.CreateGrammar()
        self.grammar.DictationSetState(0)
        self.wordsRule = self.grammar.Rules.Add("wordsRule", constants.SRATopLevel + constants.SRADynamic, 0)
        self.wordsRule.Clear()
        [self.wordsRule.InitialState.AddWordTransition(None, word) for word in wordsToAdd]
        self.grammar.Rules.Commit()
        self.grammar.CmdSetRuleState("wordsRule", 1)
        self.grammar.Rules.Commit()
        self.eventHandler = ContextEvents(self.context)
        self.say("Started successfully")
    def say(self, phrase):
        self.speaker.Speak(phrase)
class ContextEvents(win32com.client.getevents("SAPI.SpSharedRecoContext")):
    def OnRecognition(self, StreamNumber, StreamPosition, RecognitionType, Result):
        newResult = win32com.client.Dispatch(Result)
        print("小伙子你在说 ", newResult.PhraseInfo.GetText())
        speechstr=newResult.PhraseInfo.GetText()
        if  speechstr=="关闭":
            os.system("taskkill /f  /im QQ.exe")

            pass
        elif  speechstr=="往上":
            pass
        elif  speechstr=="往下":
            pass
        elif  speechstr=="往左":
            pass
        elif  speechstr=="往右":
            pass
        elif speechstr == "滚出来":
            QQ=win32gui.FindWindow("TXGuiFoundation","QQ")
            win32gui.ShowWindow(QQ,win32con.SW_SHOW)
            pass
        elif speechstr == "躲起来":
            QQ = win32gui.FindWindow("TXGuiFoundation", "QQ")
            win32gui.ShowWindow(QQ, win32con.SW_HIDE)
            pass
        else:
            pass

if __name__ == '__main__':

    speaker.Speak("语音识别开启")
    wordsToAdd = ["关闭",
                  "往上",
                  "往下",
                  "往左",
                  "往右",
                  "滚出来",
                  "躲起来"
                  ]
    speechReco = SpeechRecognition(wordsToAdd)
    while True:
        pythoncom.PumpWaitingMessages()

本博客所有文章除特别声明外,均采用 CC BY-SA 3.0协议 。转载请注明出处!

Flask框架简介 上一篇
运算符与条件判断 下一篇