Python程序找到字符串的权重
在本文中,给定的任务是找到字符串的总重量。为了计算字符串权重,我们将给定的字符串转换为较低的形式。考虑到字符的重量,我们取 a=1、b=,2 等等,直到 z=26。在这篇 Python 文章中,使用两个不同的示例,给出了查找给定字符串的权重的方法。在第一个示例中,字符串中的给定字符为 fetc、hed,然后将它们各自的权重添加到更新后的权重中。在示例2中,首先计算给定字符在字符串中出现的频率,然后将该频率乘以相应的字符权重,然后将所有这些分量权重加在一起得到最终结果。
示例 1:使用迭代查找字符串权重并添加字符权重。
算法
步骤 1 - 首先使 atoz = ' abcdefghijklmnopqrstuvwxyz'。
步骤 2 - 我们将使用 atoz.index() 函数来获取权重数字,例如,此处空格 ' ' 将具有 0 值,b 将具有 2 值,依此类推。
步骤 3 - 现在指定要计算字符串权重的给定字符串。
第 4 步 - 迭代给定的字符串以逐个获取字符。
第5步 - 找到atoz中角色的位置值(权重值)。
第 6 步 - 通过添加字符的权重值来更新字符串权重。
第7步 - 最后,打印总结果。
示例
givenstr = 'this is a sample string' def calculateWeight(teststr): teststr = teststr.lower() atoz = ' abcdefghijklmnopqrstuvwxyz' weight = 0 for item in range(len(teststr)): elem = teststr[item] currweight = atoz.index(elem) weight += currweight print("This albhabet:",elem, ", alphabet weight:", currweight, ", Updated String Weight ", weight) return weight finalresult= calculateWeight(givenstr) print("Final String Weight: ",finalresult)
输出
This albhabet: t , alphabet weight: 20 , Updated String Weight 20 This albhabet: h , alphabet weight: 8 , Updated String Weight 28 This albhabet: i , alphabet weight: 9 , Updated String Weight 37 This albhabet: s , alphabet weight: 19 , Updated String Weight 56 This albhabet: , alphabet weight: 0 , Updated String Weight 56 This albhabet: i , alphabet weight: 9 , Updated String Weight 65 This albhabet: s , alphabet weight: 19 , Updated String Weight 84 This albhabet: , alphabet weight: 0 , Updated String Weight 84 This albhabet: a , alphabet weight: 1 , Updated String Weight 85 This albhabet: , alphabet weight: 0 , Updated String Weight 85 This albhabet: s , alphabet weight: 19 , Updated String Weight 104 This albhabet: a , alphabet weight: 1 , Updated String Weight 105 This albhabet: m , alphabet weight: 13 , Updated String Weight 118 This albhabet: p , alphabet weight: 16 , Updated String Weight 134 This albhabet: l , alphabet weight: 12 , Updated String Weight 146 This albhabet: e , alphabet weight: 5 , Updated String Weight 151 This albhabet: , alphabet weight: 0 , Updated String Weight 151 This albhabet: s , alphabet weight: 19 , Updated String Weight 170 This albhabet: t , alphabet weight: 20 , Updated String Weight 190 This albhabet: r , alphabet weight: 18 , Updated String Weight 208 This albhabet: i , alphabet weight: 9 , Updated String Weight 217 This albhabet: n , alphabet weight: 14 , Updated String Weight 231 This albhabet: g , alphabet weight: 7 , Updated String Weight 238 Final String Weight: 238
示例 2:使用字符权重和出现公式查找字符串权重
算法
第 1 步 - 首先创建一个名为 charweight= {'a': 1, 'b': 2, 'c': 3, 'd': 4, 'e': 5 的字典,“f”:6,“g”:7,…………。最多“z”:26}
步骤 2 - 现在指定要计算字符串权重的给定字符串。
第 3 步 - 查找给定字符串中字符的出现频率。
第 4 步 - 迭代字符权重字典并找到给定字符串中每个字符的权重值。
第 5 步 - 将字符出现的频率与其权重相乘。
第 6 步 - 通过添加此计算值来更新字符串权重。
第 7 步 - 重复此操作并在最后打印总结果。
给定公式中使用的术语的说明
TotalWeight 是给定测试字符串的总重量。
N1,n2表示给定测试字符串中出现的字符
Occr(n1) 表示在给定的测试字符串中出现 n1。
Weight(n1) 表示给定字符 n1 在 charweight 字典中的字符权重。
这里‘*’被用作数字的乘法运算符
这里‘+’被用作数字的加法运算符
使用的公式
TotalWeight= (Occr(n1) * 权重(n1)) + (Occr(n2) * 权重(n2)) …..依此类推
示例
givenstr = 'this is a sample string' charweight= {'a': 1, 'b': 2, 'c': 3, 'd': 4, 'e': 5, 'f': 6, 'g': 7, 'h': 8, 'i': 9, 'j': 10, 'k': 11, 'l': 12, 'm': 13, 'n': 14, 'o': 15, 'p': 16, 'q': 17, 'r': 18, 's': 19, 't': 20, 'u': 21, 'v': 22, 'w': 23, 'x': 24, 'y': 25, 'z': 26} WeightSum=0 occurFreq = {} for i in givenstr: if i in occurFreq: occurFreq[i] += 1 else: occurFreq[i] = 1 print("Char Weights: " , charweight) print("Occurance: ", occurFreq) for alphabetChar, alphabetCharCount in occurFreq.items(): print(alphabetChar, ":", alphabetCharCount) for key in charweight.keys(): if key.find(alphabetChar) > -1: #print(charweight[key]*alphabetCharCount) WeightSum=WeightSum + charweight[key]*alphabetCharCount #print(WeightSum) print("This albhabet:",alphabetChar, ", alphabet Count:", alphabetCharCount, ", alphabet Weight:", charweight[key], " Updated String Weight ", WeightSum) print("Final String Weight: ", WeightSum)
输出
Char Weights: {'a': 1, 'b': 2, 'c': 3, 'd': 4, 'e': 5, 'f': 6, 'g': 7, 'h': 8, 'i': 9, 'j': 10, 'k': 11, 'l': 12, 'm': 13, 'n': 14, 'o': 15, 'p': 16, 'q': 17, 'r': 18, 's': 19, 't': 20, 'u': 21, 'v': 22, 'w': 23, 'x': 24, 'y': 25, 'z': 26} Occurance: {'t': 2, 'h': 1, 'i': 3, 's': 4, ' ': 4, 'a': 2, 'm': 1, 'p': 1, 'l': 1, 'e': 1, 'r': 1, 'n': 1, 'g': 1} t : 2 This albhabet: t , alphabet Count: 2 , alphabet Weight: 20 Updated String Weight 40 h : 1 This albhabet: h , alphabet Count: 1 , alphabet Weight: 8 Updated String Weight 48 i : 3 This albhabet: i , alphabet Count: 3 , alphabet Weight: 9 Updated String Weight 75 s : 4 This albhabet: s , alphabet Count: 4 , alphabet Weight: 19 Updated String Weight 151 : 4 a : 2 This albhabet: a , alphabet Count: 2 , alphabet Weight: 1 Updated String Weight 153 m : 1 This albhabet: m , alphabet Count: 1 , alphabet Weight: 13 Updated String Weight 166 p : 1 This albhabet: p , alphabet Count: 1 , alphabet Weight: 16 Updated String Weight 182 l : 1 This albhabet: l , alphabet Count: 1 , alphabet Weight: 12 Updated String Weight 194 e : 1 This albhabet: e , alphabet Count: 1 , alphabet Weight: 5 Updated String Weight 199 r : 1 This albhabet: r , alphabet Count: 1 , alphabet Weight: 18 Updated String Weight 217 n : 1 This albhabet: n , alphabet Count: 1 , alphabet Weight: 14 Updated String Weight 231 g : 1 This albhabet: g , alphabet Count: 1 , alphabet Weight: 7 Updated String Weight 238 Final String Weight: 238
结论
我们在这里给出两种不同的方法来展示如何查找给定字符串的字符串权重。首先,从给定的测试字符串中一一取出所使用的字符,然后将它们各自的权重相加。通过重复此过程,计算出最终的字符串重量。在示例 2 中,首先找到字符串中的字符频率,然后将该频率乘以该字符的权重。对给定字符串中使用的所有字符重复此过程,然后计算最终的字符串权重。
以上是Python程序找到字符串的权重的详细内容。更多信息请关注PHP中文网其他相关文章!

热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

AI Hentai Generator
免费生成ai无尽的。

热门文章

热工具

记事本++7.3.1
好用且免费的代码编辑器

SublimeText3汉化版
中文版,非常好用

禅工作室 13.0.1
功能强大的PHP集成开发环境

Dreamweaver CS6
视觉化网页开发工具

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

热门话题

使用 Notepad++ 运行 Python 程序需要以下步骤:1. 安装 Python 插件;2. 创建 Python 文件;3. 设置运行选项;4. 运行程序。

Llama3来了!就在刚刚,Meta官网上新,官宣了Llama380亿和700亿参数版本。并且推出即为开源SOTA:Meta官方数据显示,Llama38B和70B版本在各自参数规模上超越一众对手。8B模型在MMLU、GPQA、HumanEval等多项基准上均胜过Gemma7B和Mistral7BInstruct。而70B模型则超越了闭源的当红炸子鸡Claude3Sonnet,和谷歌的GeminiPro1.5打得有来有回。Huggingface链接一出,开源社区再次沸腾。眼尖的盲生们还第一时间发现

PHP中int类型转字符串的方法详解在PHP开发中,经常会遇到将int类型转换为字符串类型的需求。这种转换可以通过多种方式实现,本文将详细介绍几种常用的方法,并附带具体的代码示例来帮助读者更好地理解。一、使用PHP内置函数strval()PHP提供了一个内置函数strval(),可以将不同类型的变量转换为字符串类型。当我们需要将int类型转换为字符串类型时,

标题:Golang中判断字符串是否以指定字符结尾的方法在Go语言中,有时候我们需要判断一个字符串是否以特定的字符结尾,这在处理字符串时十分常见。本文将介绍如何使用Go语言来实现这一功能,同时提供代码示例供大家参考。首先,让我们来看一下Golang中如何判断一个字符串是否以指定字符结尾的方法。Golang中的字符串可以通过索引来获取其中的字符,而字符串的长度可

1、首先打开pycharm,进入到pycharm主页。2、然后新建python脚本,右键--点击new--点击pythonfile。3、输入一段字符串,代码:s="-"。4、接着需要把字符串里面的符号重复20次,代码:s1=s*20。5、输入打印输出代码,代码:print(s1)。6、最后运行脚本,在最底部会看到我们的返回值:-就重复了20次。

Python 程序开发流程包括以下步骤:需求分析:明确业务需求和项目目标。设计:确定架构和数据结构,绘制流程图或使用设计模式。编写代码:使用 Python 编程,遵循编码规范和文档注释。测试:编写单元和集成测试,进行手动测试。审查和重构:审查代码,发现缺陷和改进可读性。部署:将代码部署到目标环境中。维护:修复错误、改进功能,并监控更新。

Golang中如何检查字符串是否以特定字符开头?在使用Golang编程时,经常会遇到需要检查一个字符串是否以特定字符开头的情况。针对这一需求,我们可以使用Golang中的strings包提供的函数来实现。接下来将详细介绍如何使用Golang检查字符串是否以特定字符开头,并附上具体的代码示例。在Golang中,我们可以使用strings包中的HasPrefix

Go语言是一种强大且灵活的编程语言,它提供了丰富的字符串处理功能,包括字符串截取。在Go语言中,我们可以使用切片(slice)来截取字符串。接下来,将详细介绍如何在Go语言中截取字符串,并附上具体的代码示例。一、使用切片截取字符串在Go语言中,可以使用切片表达式来截取字符串的一部分。切片表达式的语法如下:slice:=str[start:end]其中,s
