Python 找出最高分的单词

Python 找出最高分的单词

在Python中,字符串是用于表示字符序列的基本数据类型。它们用单引号(”)或双引号(””)括起来,并提供了广泛的操作和方法用于操作。

示例

假设我们已经输入了一个字符串。现在我们将使用上述方法从输入字符串中找到最高分的单词。

输入

inputString = "hello tutorialspoint python users and learners"

输出

Resultant maximum scoring word from the input string:  tutorialspoint

在上面的输入字符串中,单词 tutorialspoint 的字符位置值的总和最大。因此它是输入字符串中得分最高的单词。

方法1:使用for循环、split()、ord()、ascii_lowercase函数

在这种方法中,我们将使用for循环、split()、ord()、ascii_lowercase函数的组合来找到得分最高的单词。 ascii_lowercase 是一个预初始化的字符串,在Python 3中作为字符串常量使用。Python的字符串ascii_lowercase返回小写字母。 ord() 函数(返回给定字符的Unicode代码/ASCII值) ascii_lowercase (返回小写字母)

string.split(separator, maxsplit)
ord(character)
string.ascii_lowercase

步骤

下面是执行所需任务的算法/步骤。

  • 使用import关键字导入string模块。

  • 创建一个变量来存储输入字符串并打印给定字符串。

  • 初始化一个变量为0来表示当前单词得分。

  • 初始化另一个变量为0来存储最大得分。

  • 创建一个空字符串来存储从输入字符串中得分最高的单词。

  • 使用split()函数(将字符串分割成列表。可以定义分隔符,默认分隔符是任何空白字符)将输入字符串分割成单词列表,并使用for循环遍历该列表的单词.

  • 将当前单词的得分初始化为0。

  • 使用另一个嵌套的for循环遍历当前单词的每个字符。

  • 使用if条件语句检查当前字符是否为小写字母。

  • 将当前字符的ASCII值差添加到得分中。

  • 使用if条件语句检查当前单词的得分是否大于最大得分。

  • 如果条件为true,则使用当前单词得分更新最大得分。

  • 将当前单词更新为得分最高的单词。

  • 打印从输入字符串中获得的得分最高的单词。

示例

以下程序使用for循环、split()、ord()和ascii_lowercase函数从输入字符串中返回得分最高的单词。

# importing string module
import string
# input string
inputString = "hello tutorialspoint python users and learners"
# Printing input string
print("Input string: ", inputString)
# Storing current word score
curr_score = 0
# Storing maximum score
maximumScore = 0
# empty string for storing the maximum scoring word from a string
maxScoreWord = ''
# Splitting input string into a list of words and traversing through words of that list
for word in inputString.split():
    # Initializing the score of the current word as 0
    curr_score = 0
    # Traversing through each character of the current word
    for character in word:
        # Checking whether the character is lowercase
        if character in string.ascii_lowercase:
            #  Adding the ASCII value difference of the current character to the score
            curr_score += ord(character) - 96
    # checking whether the score of the current word is greater than the maximum score
    if curr_score > maximumScore:
        # updating the maximum score with the current word score if the condition is true
        maximumScore = curr_score
        # updating the current word as the maximum scoring word
        maxScoreWord = word
# printing resultant maximum scoring word from the input string
print("Resultant maximum scoring word from the input string: ", maxScoreWord)

结果

Input string:  hello tutorialspoint python users and learners
Resultant maximum scoring word from the input string:  tutorialspoint

方法2:使用for循环、sum()和ord()函数

在这个方法中,我们将学习如何使用简单的for循环、sum()和ord()函数来找到得分最高的单词。

sum() 函数(返回可迭代对象中所有项目的总和)

ord() 函数(返回给定字符的Unicode代码/ASCII值作为数字)

split() 函数(将字符串分割为列表。我们可以定义分隔符;默认分隔符是任何空白字符)

sum(iterable, start=0)
ord(character)

步骤

以下是执行所需任务的算法/步骤:

  • 使用split函数将输入字符串拆分为单词列表,并使用 for循环 遍历该列表的单词。

  • 通过迭代单词并检查其是否为小写字母,获得每个字符的ASCII值差的总和。

  • 使用 if条件 语句检查当前单词的分数是否大于最大分数。

  • 如果条件为 true ,则用当前单词的分数更新最大分数。

  • 将当前单词更新为得分最高的单词。

  • 打印输入字符串中得分最高的单词。

示例

以下程序返回输入字符串中得分最高的单词,使用了for循环、sum()和ord()函数:

# importing string module
import string
# input string
inputString = "hello tutorialspoint python users and learners"
# Printing input string
print("Input string: ", inputString)
# Storing current word score
curr_score = 0
# Storing maximum score
maximumScore = 0
# empty string for storing the maximum scoring word from a string
maxScoreWord = ''
# Splitting input string into a list of words and traversing through words of that list
for word in inputString.split():
    #  getting the score of current word
    curr_score = sum(
        ord(character) - 96 for character in word if character in string.ascii_lowercase)
    # checking whether the score of the current word is greater than the maximum score
    if curr_score > maximumScore:
        # updating the maximum score with the current word score if the condition is true
        maximumScore = curr_score
         # updating the current word as the maximum scoring word
        maxScoreWord = word
# printing resultant maximum scoring word from the input string
print("Resultant maximum scoring word from the input string: ", maxScoreWord)

输出

Input string:  hello tutorialspoint python users and learners
Resultant maximum scoring word from the input string:  tutorialspoint

结论

总之,提供的代码展示了从输入字符串中找到最高分的单词的两种不同方法。这两种方法都涉及遍历输入字符串的单词,并根据字符的ASCII值计算得分。通过比较计算得分来确定最高分的单词。该代码展示了在Python中使用字符串操作、迭代、条件语句和内置函数以实现所需结果的用法。

Camera课程

Python教程

Java教程

Web教程

数据库教程

图形图像教程

办公软件教程

Linux教程

计算机教程

大数据教程

开发工具教程