我正在尝试创建一个输入名称并输出等级的函数。创建该功能的最佳方法是什么?

发布于 2025-02-10 04:00:44 字数 648 浏览 2 评论 0原文

我正在尝试创建输入名称并根据其具有的字母的顺序输出等级的

函数= ab
等级= 3

import string

x = "richard"
y = "donald"
c = "Sam"
numbers = []


for i in range(1,27):
    numbers.append(i)

print(numbers)

alphabet_string = string.ascii_lowercase
alphabet_list = list(alphabet_string)

print(alphabet_list)

new_x = list(x)
new_y = list(y)
new_c = list(c)

zip_iterators = zip(alphabet_list,numbers)
dic = list(zip_iterators)

print(dic)

def rank(name):
    rank = 0
    for l in range(0,len(name)):
        for k,v in dic:
            if l == k:
                v += rank
        print(rank)

rank(new_c)

但我到目前为止失败了

I am trying to create function that takes an input name and outputs a rank based on the order of the letters it has for example a=1 b=2

name = ab
rank = 3

import string

x = "richard"
y = "donald"
c = "Sam"
numbers = []


for i in range(1,27):
    numbers.append(i)

print(numbers)

alphabet_string = string.ascii_lowercase
alphabet_list = list(alphabet_string)

print(alphabet_list)

new_x = list(x)
new_y = list(y)
new_c = list(c)

zip_iterators = zip(alphabet_list,numbers)
dic = list(zip_iterators)

print(dic)

def rank(name):
    rank = 0
    for l in range(0,len(name)):
        for k,v in dic:
            if l == k:
                v += rank
        print(rank)

rank(new_c)

but I failed so far

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

你如我软肋 2025-02-17 04:00:44
letter_rank = {letter:rank for rank, letter in enumerate(string.ascii_lowercase, 1)}

def rank(name):
    return sum(letter_rank.get(c, 0) for c in name.lower())
letter_rank = {letter:rank for rank, letter in enumerate(string.ascii_lowercase, 1)}

def rank(name):
    return sum(letter_rank.get(c, 0) for c in name.lower())
匿名的好友 2025-02-17 04:00:44

您只需在ascii_lowercase String> String模块中常数

from string import ascii_lowercase

def rank(x):
    total = 0
    for char in x:
        if char in ascii_lowercase:
            total += ascii_lowercase.index(char) + 1
    return total

print(rank('abc'))

6

You can just use the ascii_lowercase constant in the string module:

from string import ascii_lowercase

def rank(x):
    total = 0
    for char in x:
        if char in ascii_lowercase:
            total += ascii_lowercase.index(char) + 1
    return total

print(rank('abc'))

Output: 6

余生共白头 2025-02-17 04:00:44

您可以使用ascii_lowercase.index(Letter)或创建字典来查找字母的等级。 (我的示例不包括上限,但是如果您愿意,您可以这样做)

查找字典

from string import ascii_lowercase

alphabet_lookup = {letter:i+1 for i, letter in enumerate(ascii_lowercase)}

def rank(name):
    return sum(alphabet_lookup[c] for c in name.lower())


print(rank('baa')) # outputs 4

str.index

def rank(name):
    return sum(ascii_lowercase.index(c)+1 for c in name.lower())

You could use ascii_lowercase.index(letter) or create a dictionary to lookup the rank of a letter. (My example doesnt't include caps, but you could do so if you wish)

lookup with dictionary

from string import ascii_lowercase

alphabet_lookup = {letter:i+1 for i, letter in enumerate(ascii_lowercase)}

def rank(name):
    return sum(alphabet_lookup[c] for c in name.lower())


print(rank('baa')) # outputs 4

str.index

def rank(name):
    return sum(ascii_lowercase.index(c)+1 for c in name.lower())
谜兔 2025-02-17 04:00:44

您可以使用ord()内置函数并列表理解,以创建所需的功能,如下所示:

x = "Villalobos Velasquez Santiago"

def fun(s):
    out = 0
    for i in s.lower():
        if i != " ":
            out += (ord(i)-96)
    return out

print(fun(x))

output:

333

You can use ord() built-in function and list comprehension to create the function you need as follows:

x = "Villalobos Velasquez Santiago"

def fun(s):
    out = 0
    for i in s.lower():
        if i != " ":
            out += (ord(i)-96)
    return out

print(fun(x))

Output:

333
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文