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.