如何统计字符串中含翻转数字在内的各数字出现次数?
统计含翻转变体的数字出现次数
首先明确普通数字和对应翻转变体的映射关系(基于你的示例):
- 1 ↔ І
- 2 ↔ ᘔ
- 3 ↔ Ɛ
- 4 ↔ ᔭ
你可以通过以下方式实现包含翻转变体的次数统计:
完整代码示例
stri = "1234 ᔭƐᘔІ " # 建立数字与翻转变体的映射字典 flip_mapping = { '1': 'І', '2': 'ᘔ', '3': 'Ɛ', '4': 'ᔭ' } # 遍历统计每个数字的总出现次数 for num, flip_char in flip_mapping.items(): total_count = stri.count(num) + stri.count(flip_char) print(f"Number of {num}'s in str = {total_count}")
运行结果
Number of 1's in str = 2 Number of 2's in str = 2 Number of 3's in str = 2 Number of 4's in str = 2
如果后续遇到其他数字的翻转变体,只需在flip_mapping字典中补充对应的键值对即可,扩展性很强。
内容的提问来源于stack exchange,提问作者Cookie
相关产品推荐
相关产品推荐

