[python][python编程快速上手......(第2版)][6.8小程序:Pig Latin]
本帖最后由 zhi_huo 于 2022-9-6 12:44 编辑书本:
问题:1、根据下列代码,作者的意思是只检查第一位字母吧?我的理解是:第一位字母不是元音字母的话,就把他移动到最后面并加上‘ay’。是元音字母的话,就直接加‘yay’。所以不太理解元音字母组不加上‘y’时,为什么‘m’没有放到最后面。而元音字母组加上‘y’后,‘m’被放到了最后面
# Separate the consonants at the start of this word:
prefixConsonants = ''
while len(word) > 0 and not word in VOWELS:
prefixConsonants += word
word = word
# Add the Pig Latin ending to the word:
if prefixConsonants != '':
word += prefixConsonants + 'ay'
else:
word += 'yay'
2、为什么加了下列代码,不太理解。下面的代码我的理解是:提取最开头的非字母。但是第一个位置一般不会有非字母吧?
prefixNonLetters = ''
while len(word) > 0 and not word.isalpha():
prefixNonLetters += word
word = word
不加“y”的情况下(如下列代码):
# English to Pig Latin
# Pig Latin是一种傻乎乎的、可伪造的语言,它会改变英语单词。如果单词以元音开头,就在单词末尾添加yay。
# 如果单词以辅音或辅音簇(例如ch或gr)开头,那么该辅音或辅音簇会移至单词的末尾,然后加上ay。
print('Enter the English message to translate into Pig Latin: ')
message = input()
VOWELS = ('a', 'e', 'i', 'o', 'u')
pigLatin = []
for word in message.split():
# 字符串'My name is AL SWEIGART and I am4,000 years old.'
# 会导致split()方法返回['My','name','is','AL','SWEIGART','and','I','am','4,000','years','old.']。
# Separate the non-letters at the start of this word:
# 我们需要删除每个单词开头和结尾的所有非字母字符,使得像'old.' 这样的字符串转换为'oldyay.',而不是'old.yay'。
# 我们将这些非字母字符保存到名为prefixNonLetters的变量中。
prefixNonLetters = ''
while len(word) > 0 and not word.isalpha():
prefixNonLetters += word
word = word
if len(word) == 0:
pigLatin.append(prefixNonLetters)
continue
# Separate the non-letters at the end of this word:
suffixNonLetters = ''
while not word[-1].isalpha():
suffixNonLetters += word[-1]
word = word[:-1]
# Remember if the word was in uppercase or title case.
# 确保程序能够记住该单词是全大写还是首字母大写,以便能够在将单词翻译成Pig Latin后将其恢复
wasUpper = word.isupper()
wasTitle = word.istitle()
word = word.lower()# Make the word lowercase for translation.
# Separate the consonants at the start of this word:
prefixConsonants = ''
while len(word) > 0 and not word in VOWELS:
prefixConsonants += word
word = word
# Add the Pig Latin ending to the word:
if prefixConsonants != '':
word += prefixConsonants + 'ay'
else:
word += 'yay'
# Set the word back to uppercase or title case:
if wasUpper:
word = word.upper()
if wasTitle:
word = word.title()
# Add the non-letters back to the start or end of the word.
#在for循环的末尾,我们将该单词及其最初带有的任何非字母前缀或后缀附加到pigLatin列表中
pigLatin.append(prefixNonLetters + word + suffixNonLetters)
print(' '.join(pigLatin))
输出为:
Enter the English message to translate into Pig Latin:
My name is AL SWEIGART and I am 4,000 years old.
Myay amenay isyay ALYAY EIGARTSWAY andyay Iyay amyay 4,000 earsyay oldyay.
而书本上的源代码:(元音字母组里有“y”)
# English to Pig Latin
print('Enter the English message to translate into Pig Latin:')
message = input()
VOWELS = ('a', 'e', 'i', 'o', 'u', 'y')
pigLatin = []# A list of the words in Pig Latin.
for word in message.split():
# Separate the non-letters at the start of this word:
prefixNonLetters = ''
while len(word) > 0 and not word.isalpha():
prefixNonLetters += word
word = word
if len(word) == 0:
pigLatin.append(prefixNonLetters)
continue
# Separate the non-letters at the end of this word:
suffixNonLetters = ''
while not word[-1].isalpha():
suffixNonLetters += word[-1]
word = word[:-1]
# Remember if the word was in uppercase or title case.
wasUpper = word.isupper()
wasTitle = word.istitle()
word = word.lower()# Make the word lowercase for translation.
# Separate the consonants at the start of this word:
prefixConsonants = ''
while len(word) > 0 and not word in VOWELS:
prefixConsonants += word
word = word
# Add the Pig Latin ending to the word:
if prefixConsonants != '':
word += prefixConsonants + 'ay'
else:
word += 'yay'
# Set the word back to uppercase or title case:
if wasUpper:
word = word.upper()
if wasTitle:
word = word.title()
# Add the non-letters back to the start or end of the word.
pigLatin.append(prefixNonLetters + word + suffixNonLetters)
# Join all the words back together into a single string:
print(' '.join(pigLatin))
输出为:
Enter the English message to translate into Pig Latin:
My name is AL SWEIGART and I am 4,000 years old.
Ymay amenay isyay ALYAY EIGARTSWAY andyay Iyay amyay 4,000 yearsyay oldyay. 本帖最后由 lbbas 于 2022-9-6 12:55 编辑
zhi_huo 发表于 2022-9-6 12:46
word意思是检测第一个字母吧?我的理解不太对?
while len(word) > 0 and...
你的理解没错,但是要注意while循环,如果只是检测第一个,没必要循环了。
注意下面的 word = word类似于数组的pop(0)。
如果单词是“freak",则顺序是freak--f ; reak --r , eak--e,碰到元音结束循环。结果是eakfray. 来学习下咯~ 有没有采集网站文章 的教程 本帖最后由 zhi_huo 于 2022-9-5 21:07 编辑
开创者 发表于 2022-9-5 20:59
有没有采集网站文章 的教程
https://p1.meituan.net/dpplatform/e00d8cee55f1886f68781941994481e0188789.png
好像有,但是我没看到那里 开创者 发表于 2022-9-5 20:59
有没有采集网站文章 的教程
推荐你看一下《Python3网络爬虫开发实战(第二版)》
与网络爬虫相关的书籍。肯定比初学者的这本详细 感谢😊,学习了 python编程快速上手 python编程快速上手,看看不错。