吾爱破解 - LCG - LSG |安卓破解|病毒分析|www.52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 2081|回复: 27
收起左侧

[求助] [python][python编程快速上手......(第2版)][6.8小程序:Pig Latin]

[复制链接]
zhi_huo 发表于 2022-9-5 20:42
50吾爱币
本帖最后由 zhi_huo 于 2022-9-6 12:44 编辑

书本:[python编程快速上手......(第2版)] [6.8小程序:Pig Latin]


问题:1、根据下列代码,作者的意思是只检查第一位字母吧?我的理解是:第一位字母不是元音字母的话,就把他移动到最后面并加上‘ay’。是元音字母的话,就直接加‘yay’。所以不太理解元音字母组不加上‘y’时,为什么‘m’没有放到最后面。而元音字母组加上‘y’后,‘m’被放到了最后面
[Python] 纯文本查看 复制代码
    # Separate the consonants at the start of this word:
    prefixConsonants = ''
    while len(word) > 0 and not word[0] in VOWELS:
        prefixConsonants += word[0]
        word = word[1:]

    # Add the Pig Latin ending to the word:
    if prefixConsonants != '':
        word += prefixConsonants + 'ay'
    else:
        word += 'yay'

2、为什么加了下列代码,不太理解。下面的代码我的理解是:提取最开头的非字母。但是第一个位置一般不会有非字母吧?
[Python] 纯文本查看 复制代码
prefixNonLetters = ''
    while len(word) > 0 and not word[0].isalpha():
        prefixNonLetters += word[0]
        word = word[1:]


不加“y”的情况下(如下列代码):
[Python] 纯文本查看 复制代码
# 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[0].isalpha():
        prefixNonLetters += word[0]
        word = word[1:]
    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[0] in VOWELS:
        prefixConsonants += word[0]
        word = word[1:]

    # 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))


输出为:
[PowerShell] 纯文本查看 复制代码
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”)
[Python] 纯文本查看 复制代码
# 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[0].isalpha():
        prefixNonLetters += word[0]
        word = word[1:]
    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[0] in VOWELS:
        prefixConsonants += word[0]
        word = word[1:]
    # 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))

输出为:
[PowerShell] 纯文本查看 复制代码
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.

最佳答案

查看完整内容

你的理解没错,但是要注意while循环,如果只是检测第一个,没必要循环了。 注意下面的 word = word[1:] 类似于数组的pop(0)。 如果单词是“freak",则顺序是 freak--f ; reak --r , eak--e,碰到元音结束循环。结果是eakfray.

免费评分

参与人数 1热心值 +1 收起 理由
Liroot + 1 学习一下

查看全部评分

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

lbbas 发表于 2022-9-5 20:42
本帖最后由 lbbas 于 2022-9-6 12:55 编辑
zhi_huo 发表于 2022-9-6 12:46
word[0]意思是检测第一个字母吧?我的理解不太对?
[mw_shl_code=python,true]while len(word) > 0 and  ...

你的理解没错,但是要注意while循环,如果只是检测第一个,没必要循环了。
注意下面的 word = word[1:]  类似于数组的pop(0)。
如果单词是“freak",则顺序是  freak--f ; reak --r , eak--e,碰到元音结束循环。结果是eakfray.

免费评分

参与人数 1热心值 +1 收起 理由
zhi_huo + 1 我很赞同!

查看全部评分

nanbeiduoqilu 发表于 2022-9-5 20:51
开创者 发表于 2022-9-5 20:59
 楼主| zhi_huo 发表于 2022-9-5 21:04
本帖最后由 zhi_huo 于 2022-9-5 21:07 编辑
开创者 发表于 2022-9-5 20:59
有没有采集网站文章 的教程


好像有,但是我没看到那里
 楼主| zhi_huo 发表于 2022-9-5 21:09
开创者 发表于 2022-9-5 20:59
有没有采集网站文章 的教程

推荐你看一下《Python3网络爬虫开发实战(第二版)》
与网络爬虫相关的书籍。肯定比初学者的这本详细

免费评分

参与人数 1吾爱币 +1 收起 理由
开创者 + 1 热心回复!

查看全部评分

mokson 发表于 2022-9-6 07:57
我以为是一本书。
a3322a 发表于 2022-9-6 07:57
感谢😊,学习了
Junlee 发表于 2022-9-6 08:14
python编程快速上手
xiaoshu1688 发表于 2022-9-6 08:56
python编程快速上手,看看不错。
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则 警告:本版块禁止回复与主题无关非技术内容,违者重罚!

快速回复 收藏帖子 返回列表 搜索

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-5-13 04:44

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表