Python中文
首页
教程
问答
标签
搜索
登录
注册
Python映射、Lambda和string.replace
回答此问题可获得
20
贡献值,回答如果被采纳可获得
50
分。
<p>我知道如何在不使用lambda/map的情况下解决我的问题(在本练习中,我不能使用regex或其他python库),使用带有string.replace()的for循环……但我确实在尝试使用map/lambda和string.replace的组合是否能达到同样的效果</p> <p>我的目标是读入一个txt文件,然后用字母“e”替换非标准“e”(如<em>的每个实例)</p> <p>我现在的主要问题是,我得到了6个列表(例如,我在<em>新文件</em>/<em>新文件列表理解</em>中有<strong>6个字符串<strong>,并且每个字符串都根据评估的1个iterable更新了原始字符串</p> <p>例如,<strong>newFile[0]</strong><em>=输出.replace('ee')</em>,<strong>newFile[1]</strong><em>=输出.replace('ee')</em>等</p> <p>所以我想返回格式化字符串的1个副本,所有的.replace()都在上面迭代</p> <p>我在下面引用的文本文件的链接可以访问<a href="https://easyupload.io/s7m0zj" rel="nofollow noreferrer">https://easyupload.io/s7m0zj</a></p> <pre><code>import string def file2str(filename): with open(filename, 'r', encoding="utf8") as fid: return fid.read() def count_letter_e(filename, ignore_accents, ignore_case): eSToAvoid = 'éêèÉÊÈ' textFile = file2str("Sentence One.txt") newFileListComprehension = [textFile.replace(e,'e') for e in eSToAvoid if ignore_accents == 1] value = textFile.count('e') #newFile = list((map(lambda element: (textFile.replace(element, 'e') if ignore_accents == 1 else textFile.count('e')), eSToAvoid))) return 0 numberOfEs = count_letter_e("Sentence One.txt", 1, 1)``` </code></pre>
0 条评论
分类:
Python问答
请先
登录
后评论
默认排序
时间排序
1 个回答
匿名
1天前
擅长:python、mysql、java
<p>可以使用<code>str.translate</code>一次替换多个字符<code>str.maketrans</code>帮助您创建所需的映射:</p> <pre><code>eSToAvoid = 'éêèÉÊÈ' textFile.translate(str.maketrans(eSToAvoid, 'e' * len(eSToAvoid))) </code></pre>
请先
登录
后评论
针对此问题:
更多的回答
关注
89
关注
收藏
1
收藏,
216
浏览
网友 提问于 2天前
相关Python问题
如何在乒乓球比赛中预测球的轨迹,对于AI球拍预测?
7 回答
如何在乒乓球游戏中阻止球
5 回答
如何在乘法和模中不乘空间?
8 回答
如何在乘法和除以2个不同的数字之间进行交换?
6 回答
如何在也是数据一部分的单个字符上拆分大字符串
7 回答
如何在乾草堆中找到針,有更好的解決方案嗎?
4 回答
如何在事件wxWidgets中传递自定义数据
4 回答
如何在事件中使用lambda i=i?
5 回答
如何在事件中心只接收最近的数据
9 回答
如何在事件发生之前保持云函数运行?
5 回答
如何在事件发生后使页面重定向到同一页面
1 回答
如何在事件回调之间保持python生成器的状态
6 回答
如何在事件处理程序(pythonsocket、sphinx)中保留docstring
10 回答
如何在事件处理程序中更改wxRichTextCtrl的光标位置?
2 回答
如何在事件处理程序中访问外部对象?
6 回答
如何在事件循环中将协程打包为正常函数?
1 回答
如何在事件循环之外运行协同程序?
5 回答
如何在事件循环结束时为并发未来的所有线程调用类方法?
4 回答
如何在事件文件中只保留一份摘要?
10 回答
如何在事件模板中添加事件
9 回答