我正在尝试创建一个输入名称并输出等级的函数。创建该功能的最佳方法是什么?
我正在尝试创建输入名称并根据其具有的字母的顺序输出等级的
函数= 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您只需在
ascii_lowercase
String> String
模块中常数:
6
You can just use the
ascii_lowercase
constant in thestring
module:Output:
6
您可以使用
ascii_lowercase.index(Letter)
或创建字典来查找字母的等级。 (我的示例不包括上限,但是如果您愿意,您可以这样做)查找字典
str.index
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
str.index
您可以使用
ord()
内置函数并列表理解,以创建所需的功能,如下所示:output:
You can use
ord()
built-in function and list comprehension to create the function you need as follows:Output: