首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >循环丢失迭代

循环丢失迭代
EN

Stack Overflow用户
提问于 2019-07-23 08:57:55
回答 1查看 70关注 0票数 0

我正在尝试测试我正在迭代的单词是否为大写。如果单词不是大写,也不在字典中,则应按原样打印。如果单词不是大写的,也不在字典中,就应该替换并打印出来。如果单词是大写的,则需要将其设置为小写,以测试它是否在字典中。如果它是大写的,并且在字典中,这个词应该被替换并打印出来。如果它是大写的,并且不在字典中,那么这个词应该再次变成大写并打印出来。

我已经尝试了下面的两个代码,它们似乎在做同样的事情。它不会更改大写字母。它甚至可能没有对它们进行迭代。

代码语言:javascript
复制
def replace_mode(text_list,misspelling):
    update = ''
#loop iterates over the lines and over each word
    for line in text_list:
        word = line.split(' ')
        for key in word:
            if key.isupper():
                key = key.lower()
                if key in misspelling.keys(): #checks if the word is in the dictionary
                    update = misspelling[key]
                    print(update.capitalize(), end=(' '))
                else: 
                    print(key.capitalize(), end=(' '))
            elif key in misspelling.keys():
                update = misspelling[key]
                print(update, end=(' '))
            else:  
                print(key, end=(' '))
        print()
代码语言:javascript
复制
def replace_mode(text_list,misspelling):
    update = ''
#loop iterates over the lines and over each word
    for line in text_list:
        word = line.split(' ')
        for key in word:
            capital = False
            if key.isupper():
                capital = True
                key = key.lower()
            if capital == True:
                if key in misspelling.keys(): #checks if the word is in the dictionary
                    update = misspelling[key]
                    print(update.capitalize(), end=(' '))
                else:
                    print(key.capitalize(), end=(' '))
            if capital == False:
                if key in misspelling.keys():
                    update = misspelling[key]
                    print(update, end=(' '))
                else:
                    print(key, end=(' '))
        print()
代码语言:javascript
复制
--- OUTPUT ---                               | --- OUTPUT ---                              
Sitting in the Morni sun                     | Sitting in the Morning sun                  
I will be sitting when the evening comes     | I will be sitting when the evening comes    
Watching the Ships roll in                   | Watching the Ships roll in                  
Then I watch them roll away again yeah       | Then I watch them roll away again yeah      
                                             |                                             
I am sitting on the dock of the bay          | I am sitting on the dock of the bay         
Watchin the tide roll away ooh               | Watching the tide roll away ooh             
I am just Siting on the dock of the bay      | I am just Sitting on the dock of the bay    
Wastin time                                  | Wasting time
EN

回答 1

Stack Overflow用户

发布于 2019-07-23 09:28:40

你的代码在你的帖子中格式不正确,所以读起来有点困难,但我认为我正在收集你想要做的事情。以下是一些小贴士:

isupper()检查字符串中的所有字符是否都是大写的,但通常情况下,如果只有第一个字母是大写,则认为单词是大写的。

keys()返回一个包含字典键的迭代量,但是检查字典中的成员关系要简单得多:if key in misspelling:将检查key是否是字典misspelling中的键之一

当涉及到bool时,您不必将它们与条件句中的其他bool进行比较;您应该编写if capital,而不是if capital == True:。您可以使用else,而不是检查False条件。

把这些想法放在一起:

代码语言:javascript
复制
    # Is the first letter of this word capitalized?
    capital = key[0].isupper()

    # Is the lowercased version of the word in the dictionary?
    in_dict = key.lower() in misspelling

    if capital and in_dict:
        # Word is capitalized and in the dictionary
        pass  # do something
    elif capital and not in_dict:
        # Word is capitalized and not in the dictionary
        pass  # do something
    elif capital and in_dict:
        # Word is not capitalized and in the dictionary
        pass  # do something
    else:
        # Word is not capitalized and not in the dictionary
        pass  # do something

要更新字典,您必须访问一个键并将其替换为其他人。update = misspelling[key]不这样做;它访问与key对应的值,并将其存储在变量update中。你可能想做的是misspelling[key] = key;或者类似的东西,从你的帖子中看不清字典应该包含什么

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57155356

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档