導航:首頁 > 編程語言 > 單詞校正python

單詞校正python

發布時間:2023-01-10 18:46:36

A. 用python寫一個找單詞程序。具體如下。。用上提示的函數

#!bin/python #-*- encoding: utf-8 -*- def counter(path, find, punctuation): infile = open(path, "r") lenth = len(find) count = [] for i in range(lenth): count.append(0) dat = infile.readline().strip("\n") while dat != '': dat = dat.split() for elemt in dat: elemt = elemt.strip(punctuation) #去除標點符號 if elemt in find: i = find.index(elemt) count[i] += 1 dat = infile.readline().strip("\n") infile.close() for i in range(lenth): print "%s:%d次" % (find[i],count[i]) if __name__ == "__main__": path = "PATH" find = ["hello", "hi", "world"] punctuation = ''',.;'":!?''' counter(path, find, punctuation)

B. python找錯誤,實現漢英翻譯單詞

我改了一下

dictionary={}#創建一個空字典


#定義一個函數,功能:向字典中增加記錄
#dictionary是字典,en是英文單詞,ch是對應中文單詞
defadd_dict(dictionary,en,ch):
dictionary[en]=ch#增添or更新一條記錄
dictionary[ch]=en
print("添加成功")


#定義一個函數,功能:翻譯
#dictionary是字典,string是要查找的單詞(中文or英文)
deffind(dictionary,string):
ifstringnotindictionary:#如果string不在dict內,列印提示信息
print("該單詞不在dict內")
else:#否則,給出對應中文釋義
print("該單詞",string,"的意思是:",dictionary[string])


#向字典內增添幾個記錄,測試增加記錄的功能,也可嘗試用while循環持續接收用戶添加詞條
foriinrange(3):
en=input("增添的英文單詞:")#接受輸入
ch=input("對應的中文單詞:")
add_dict(dictionary,en,ch)#調用add_dict函數,往字典中添加內容

#接收用戶輸入,調用find函數實現翻譯
string=input("請輸入要查詢的單詞:")
find(dictionary,string)

運行效果:

增添的英文單詞:apple
對應的中文單詞:蘋果
添加成功
增添的英文單詞:banana
對應的中文單詞:香蕉
添加成功
增添的英文單詞:peach
對應的中文單詞:桃子
添加成功
請輸入要查詢的單詞:peach
該單詞peach的意思是:桃子

錯誤應該是你定義函數時的變數名(dictionary)和函數內部的變數名(dict)不一致導致的,還有你在測試add_dict的時候把add_dict的返回值None賦給了一個名叫dictionary的變數,這是完全沒必要的,並且導致了和現有的dictionary的沖突,使得第二次循環添加單詞時出現錯誤。

C. python高手請進,關於語句中單詞反轉問題!

在你所提問的內容中,你提供的代碼很亂。我大概看了下,你的代碼功能大概是反轉字元串內容。主要工作代碼是先把字元串轉換為list,然後join起來,再把反轉好的字元串輸出到屏幕上。是這樣吧?

但有一點你要注意:

你所在的python環境是2.7,而你寫的代碼環境是python3。你要在python2下執行是不成功的。

我沒測試你的代碼,因為時間的原因(主要是你的代碼太亂,太糟糕!讓回答者花不必要的時間去做不必要的思考。)

以下是我給你的一些參考,比如,按你的思路去做的話(定義一個函數,功能是先把字元串轉換為list,然後join起來,再把反轉好的字元串輸出到屏幕上。)。代碼我們可以這樣寫:

python3.6環境下

#因代碼中有f-string格式,所以至少要python3.6環境
#定義函數reverseStrtxt
defreverseStrtxt(strtxt):
newStrtxt=[]#初始化空列表
index=len(strtxt)#返回strtxt的項目數
whileindex:
index-=1#index=index-1
newStrtxt+=strtxt[index]#newStrtxt=newStrtxt+strtxt[index]
returnf'{"".join(newStrtxt)}'#返回反轉後的字元串

if__name__=='__main__':
strtxt='hello,howareyou?Fine.'
print(reverseStrtxt(strtxt))

python2環境下

#可運行在python2環境下
#定義函數reverseStrtxt
defreverseStrtxt(strtxt):
newStrtxt=[]#初始化空列表
index=len(strtxt)#返回strtxt的項目數
whileindex:
index-=1#index=index-1
newStrtxt+=strtxt[index]#newStrtxt=newStrtxt+strtxt[index]
return"".join(newStrtxt)#返回反轉後的字元串

if__name__=='__main__':
strtxt='hello,howareyou?Fine.'
printreverseStrtxt(strtxt)

我們把以上代碼優化下:

python3環境

#可運行在python3環境下
#定義函數reverseStrtxt
defreverseStrtxt(strtxt):
newStrtxt=''#初始化空字元串
index=len(strtxt)#返回strtxt的項目數
whileindex:
index-=1#index=index-1
newStrtxt+=strtxt[index]#newStrtxt=newStrtxt+strtxt[index]
returnnewStrtxt#返回反轉後的字元串

if__name__=='__main__':
strtxt='hello,howareyou?Fine.'
print(reverseStrtxt(strtxt))

python2環境

#可運行在python2環境下
#定義函數reverseStrtxt
defreverseStrtxt(strtxt):
newStrtxt=''#初始化空字元串
index=len(strtxt)#返回strtxt的項目數
whileindex:
index-=1#index=index-1
newStrtxt+=strtxt[index]#newStrtxt=newStrtxt+strtxt[index]
returnnewStrtxt#返回反轉後的字元串

if__name__=='__main__':
strtxt='hello,howareyou?Fine.'
printreverseStrtxt(strtxt)


我們還可以更簡單

#用切片的速度最快
txtstr='hello,howareyou?Fine.'
str_lst=list(txtstr)
print(''.join(str_lst[::-1]))
print('hello,howareyou?Fine.'[::-1])

要反轉字元串的方法很多,但個人建議用切片,速度最快,而且代碼簡潔,易讀。

其實python在這方面是很靈活的。

純手工,如果對你有幫助,望採納!

D. python腳本,如何將讀取的一個文本文件,和一個詞庫中的單詞進行匹配求指導

當然這只是簡單的實現,算基本原理。

如果要投入項目中使用,實際文本和詞庫可能比較大,還需要一些處理,比如使用迭代器,防止內存溢出。

E. python怎麼實現單詞反轉字元串

假設:

data='thisisateststring'

鑒於你的問題描述得不是很清楚,下面考慮兩種情況:

1、逐個字母反轉:

>>>data[::-1]
'gnirtstsetasisiht'

2、逐個單詞反轉:

>>>''.join(data.split()[::-1])
'stringtestaisthis'

F. 用python編寫一段程序,輸入若干單詞,按照單詞長短進行排序,並統計所有單詞中每個字母(a-z)出現的次數

1、解法:對輸入的單詞進行分割得到列表,遍歷列表中的單詞,二級遍歷單詞中的字元,判斷字元是否存在字元字典中,存在則計數+1,不存在則初始化字典為1

2、知識點:字典、列表、for循環、if判斷、input獲得輸入、print列印

3、代碼如下:

#-*-coding:UTF-8-*-

#簡歷一個字典,key=26個英文字母,value為出現次數
wordDict={}
#獲得輸入單詞字元串
str=input("請輸入一串單詞")
#用空格分割單詞,存到列表
strArr=str.split(sep='')
#遍歷列表中的單詞
forwordinstrArr:
#遍歷單詞中的字母
forchinword:
#判斷字典中是否存在鍵key
ifchinwordDict:
wordDict[ch]=wordDict.get(ch)+1#計數+1
else:
wordDict[ch]=1#計數初始化為1

#列印輸出
forkey,valueinwordDict.items():
print("%s=%d"%(key,value))

G. python單詞如何讀

python的讀音及註解如下:
(推薦教程:python基礎教程)
python 英 [?pa?θ?n] 美 [?pa?θɑ?n]
n. 蟒; 蟒蛇;
[例句]On my system, it's at/ usr/ bin/ python.
[其他] 復數:pythons
python是一門面向對象的編程語言。python本身的意思是蟒蛇,巨蟒,至於為什麼叫pthon,這里還是有緣由的。
Python的創始人為荷蘭人吉多·范羅蘇姆 (Guido van Rossum)。1989年聖誕節期間,在阿姆斯特丹,Guido為了打發聖誕節的無趣,決心開發一個新的腳本解釋程序,作為ABC 語言的一種繼承。之所以選中Python(大蟒蛇的意思)作為該編程語言的名字,是取自英國20世紀70年代首播的電視喜劇《蒙提.派森乾的飛行馬戲團》(Monty Python's Flying Circus)。
ABC是由Guido參加設計的一種教學語言。就Guido本人看來,ABC 這種語言非常優美和強大,是專門為非專業程序員設計的。但是ABC語言並沒有成功,究其原因,Guido 認為是其非開放造成的。Guido 決心在Python 中避免這一錯誤。同時,他還想實現在ABC 中閃現過但未曾實現的東西。
就這樣,Python在Guido手中誕生了。可以說,Python是從ABC發展起來,主要受到了Mola-3(另一種相當優美且強大的語言,為小型團體所設計的)的影響。並且結合了Unix shell和C的習慣。
相關推薦:python爬蟲視頻教程

H. python判斷一個單詞是否為有效的英文單詞

For (much) more power and flexibility, use a dedicated spellchecking library like PyEnchant. There's a tutorial, or you could just dive straight in:
>>> import enchant>>> d = enchant.Dict("en_US")>>> d.check("Hello")True>>> d.check("Helo")False>>> d.suggest("Helo")['He lo', 'He-lo', 'Hello', 'Helot', 'Help', 'Halo', 'Hell', 'Held', 'Helm', 'Hero', "He'll"]>>>

PyEnchant comes with a few dictionaries (en_GB, en_US, de_DE, fr_FR), but can use any of the OpenOffice ones if you want more languages.

Using a set to store the word list because looking them up will be faster:
with open("english_words.txt") as word_file:
english_words = set(word.strip().lower() for word in word_file)def is_english_word(word):
return word.lower() in english_wordsprint is_english_word("ham") # should be true if you have a good english_words.txt

To answer the second part of the question, the plurals would already be in a good word list, but if you wanted to specifically exclude those from the list for some reason, you could indeed write a function to handle it. But English pluralization rules are tricky enough that I'd just include the plurals in the word list to begin with.
As to where to find English word lists, I found several just by Googling "English word list". Here is one:rg/linguistics/wordlists/english/wordlist/wordsEn.txt You could Google for British or American English if you want specifically one of those dialects.

Using NLTK:
from nltk.corpus import wordnetif not wordnet.synsets(word_to_test):
#Not an English Wordelse:
#English Word

You should refer to this article if you have trouble installing wordnet or want to try other approaches.

I. python 2.78 如何編一個打亂單詞的游戲急,作業。

你好
如果玩家猜錯了單詞呢? 希望可以多一些具體的要求
我在做了 默認的要求是這樣
玩家看過五個單詞 是包含那些give up的
並且 shuffle letters只能一次

import random

def shuffle(st):
string = ''
while st != '':
l = len(st)
x = int(random.random() * l)
char = st[x]
st = st[0:x] + st[x+1:l]
string = string + char
return string

word_list = ['give', 'put', 'hate', 'betrayal', 'embrassment', 'disappointment', 'fury', 'happiness', 'dream', 'opinion', 'behaviour']

print('Game begins :')
x = 0
score = 0

while True:
word = random.choice(word_list)
word_list.pop(word_list.index(word))
shuffled = shuffle(word)
print('The shuffled word is : ' + shuffled)
st = input('Type guess / shuffle letters / give up : ')
if st == 'guess':
w = input('Type the word : ')
w = w.rstrip()
if w == word:
print('Congrats!')
score += 1
else:
print('Wrong!')
elif st == 'shuffle letters':
shuffled = shuffle(shuffled)
print('The shuffled word is : ' + shuffled)
w = input('Please guess : ')
w = w.rstrip()
if w == word:
print('Congrats!')
score += 1
else:
print('Wrong!')
else:
print('You gave up.')
x += 1
if x == 5:
break

print('Game ends.')
print('Your score is : ' + str(score))

J. python怎麼讀

python[英]['paɪθən] [美][ˈpaɪˌθɑn, -θən]
生詞本
簡明釋義
n.巨蛇,大蟒
復數:pythons

易混淆的單詞:Python

以下結果由 金山詞霸 提供
柯林斯高階英漢詞典 網路釋義 網路釋義
1.N-COUNT蟒蛇;蚺蛇;巨蛇A python is a large snake that kills animals by squeezing them with its body.

閱讀全文

與單詞校正python相關的資料

熱點內容
加密貨幣都有哪些平台 瀏覽:625
python和matlab難度 瀏覽:388
python爬蟲很難學么 瀏覽:572
小米解壓積木可以組成什麼呢 瀏覽:816
為什麼滴滴出行app還能用 瀏覽:564
怎麼升級手機android 瀏覽:922
php權威編程pdf 瀏覽:994
扣扣加密技巧 瀏覽:720
蘋果如何創建伺服器錯誤 瀏覽:495
軟考初級程序員大題分值 瀏覽:473
js壓縮視頻文件 瀏覽:578
linux如何通過命令創建文件 瀏覽:990
應用加密app還能訪問應用嘛 瀏覽:433
安卓怎麼用支付寶交違章罰款 瀏覽:665
php面向對象的程序設計 瀏覽:504
數據挖掘演算法書籍推薦 瀏覽:894
投訴聯通用什麼app 瀏覽:152
web伺服器變更ip地址 瀏覽:956
java正則表達式驗證郵箱 瀏覽:362
成熟商務男裝下載什麼軟體app 瀏覽:610