Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
String python3以stings为单位求和每个字母的值_String_Algorithm_Sorting_Python 3.x - Fatal编程技术网

String python3以stings为单位求和每个字母的值

String python3以stings为单位求和每个字母的值,string,algorithm,sorting,python-3.x,String,Algorithm,Sorting,Python 3.x,我需要字符串字母的和值。 a=1 b=2 c=3 d=4 alphabet = 'abcdefghijklmnopqrstuvwxyz' v1 string = "abcd" # #result = sum(string) so if string[0] and string[1] and string[2] and string[3] in alphabet: if string[0] is alphabet[0] and string[1] is alphabet[1] and

我需要字符串字母的和值。 a=1 b=2 c=3 d=4

alphabet = 'abcdefghijklmnopqrstuvwxyz'

v1

string = "abcd"

# #result = sum(string) so 
if string[0] and string[1] and string[2] and string[3] in alphabet:
   if string[0] is alphabet[0] and string[1] is alphabet[1] and string[2] is alphabet[2] and string[3] is alphabet[3]:
      print(a+b+c+d)

v2

string = ("ab","aa","dc",)

if string[0][0] and string[0][1] and string[1][0] and string[1][1] and string[2][0] and string[2][1] in alphabet:
   if string[0] is alphabet[0] and string[1] is alphabet[1] and string[2] is alphabet[2] and string[3] is alphabet[3]:
      print(a+b+c+d)   
解决办法是什么?你能帮我使用和a;基于构建的词典可以作为获取每个字母的整数值的方法:

from string import ascii_lowercase

letter_value = {c: i for i, c in enumerate(ascii_lowercase, 1)}
wordsum = sum(letter_value.get(c, 0) for c in word if c)
枚举(ascii_小写,1)
从1开始迭代时产生
(索引,字母)
对。这就为您提供了
(1,'a')
(2,'b')
等,可以在字典中转换为
c:i
字母对,将字母映射到整数

接下来,使用可以选择默认值;对于输入字符串中的任何字符,都可以查找数值并将其映射为整数,但如果该字符不是小写字母,则返回
0
sum(…)
与循环分开,然后简单地将这些值相加

如果需要支持单词序列,只需再次使用
sum()
。将上述
sum()
调用放入一个函数中,并将该函数按顺序应用于每个单词:

from string import ascii_lowercase

letter_value = {c: i for i, c in enumerate(ascii_lowercase, 1)}

def sum_word(word):
    return sum(letter_value.get(c, 0) for c in word if c)

def sum_words(words):
    return sum(sum_word(word) for word in words)
使用和a;基于构建的词典可以作为获取每个字母的整数值的方法:

from string import ascii_lowercase

letter_value = {c: i for i, c in enumerate(ascii_lowercase, 1)}
wordsum = sum(letter_value.get(c, 0) for c in word if c)
枚举(ascii_小写,1)
从1开始迭代时产生
(索引,字母)
对。这就为您提供了
(1,'a')
(2,'b')
等,可以在字典中转换为
c:i
字母对,将字母映射到整数

接下来,使用可以选择默认值;对于输入字符串中的任何字符,都可以查找数值并将其映射为整数,但如果该字符不是小写字母,则返回
0
sum(…)
与循环分开,然后简单地将这些值相加

如果需要支持单词序列,只需再次使用
sum()
。将上述
sum()
调用放入一个函数中,并将该函数按顺序应用于每个单词:

from string import ascii_lowercase

letter_value = {c: i for i, c in enumerate(ascii_lowercase, 1)}

def sum_word(word):
    return sum(letter_value.get(c, 0) for c in word if c)

def sum_words(words):
    return sum(sum_word(word) for word in words)

老式的方法是利用小写字母是连续的,因此
ord(b)-ord(a)==1

data = "abcd"
print("Sum:", sum(ord(c)-ord("a")+1 for c in data))
当然,您可以“优化”它以减少计算量,尽管在这种情况下这看起来很愚蠢:

ord_a = ord("a")
print("Sum:", sum(ord(c)-ord_a for c in data)+len(data))

老式的方法是利用小写字母是连续的,因此
ord(b)-ord(a)==1

data = "abcd"
print("Sum:", sum(ord(c)-ord("a")+1 for c in data))
当然,您可以“优化”它以减少计算量,尽管在这种情况下这看起来很愚蠢:

ord_a = ord("a")
print("Sum:", sum(ord(c)-ord_a for c in data)+len(data))

你的布尔逻辑是完全不正确的,你的布尔逻辑是完全不正确的,我会使用
{chr(I+96):I for I in range(1,27)}
而不是导入
ascii_小写
@stevensmmers:这是一个品味问题;我认为这是一种自我记录方式。@MartijnPieters你是我的hero@MartijnPieters我如何做求和字符串组合乘积,我尝试了求和(字母u值.get(c,0)*字母u值.values(),如果是c,则为zip(m)中的c),但我得到了不支持错误的操作数类型*:“int”和“dict值”@PyDroid:use
导入运算符
,然后
reduce(operator.mul,(字母_value.get(c,0)表示单词中的c,如果是c),1)
生成单词乘积。我会使用
{chr(I+96):I表示范围(1,27)中的I}
而不是导入
ascii_小写
@StevenSummers:这是品味的问题;我认为这是自我记录的方式。@MartijnPieters你是我的hero@MartijnPieters如何实现和字符串组合乘积,我尝试了和(字母_value.get(c,0)*字母_value.values(),如果是c,则为zip(m)中的c),但得到了错误不支持的操作数类型(s) 对于*:'int'和'dict_values'@PyDroid:使用
导入操作符
,然后
reduce(operator.mul,(字母_value.get(c,0)for c in word if c),1)
生成一个词乘积。谢谢alex,你能帮我吗,如何获取produc字符串?例如,如果我有和a+b+c+d,我需要comapre sum and product if a+b+c!=a*b*c*dI谷歌搜索“python产品”和。google结果中的第二个点击向您展示了如何操作。(如图所示定义它,然后使用
prod()
而不是
sum()
)谢谢alex,您能帮助我,如何获取produc字符串吗?例如,如果我有sum a+b+c+d,我需要comapre sum和product如果a+b+c+d!=a*b*c*dI googled”python产品和。google结果中的第二次点击显示了如何操作。(如图所示定义它,然后使用
prod()
而不是
sum()