python - 如何显示元组列表并计算相同数量的元素
问题描述
输入 定义初始(daftar):
daftar = (
"Michael","Viny","Aurelio","Michael",
"Felix","Kevin","Vincen","Vincen","Michael")
inisial(daftar)
输出:
迈克尔
维尼
奥雷里奥
迈克尔2
菲利克斯
凯文
文森2
文森3
迈克尔3
解决方案
您可以使用以下代码解决问题。
daftar = ("Michael", "Viny", "Aurelio", "Michael",
"Felix", "Kevin", "Vincen", "Vincen", "Michael")
temp_dict = {}
for name in daftar:
if temp_dict.get(name):
temp_dict[name] += 1
else:
temp_dict[name] = 1
if temp_dict[name] > 1:
print('%s%d' % (name, temp_dict[name]))
else:
print(name)
推荐阅读
- apache - 未出现在 Apache Netbeans 11 中添加手动依赖项
- r - 使用马赛克图旋转 y 轴标签而不重叠
- c# - 删除成员失败
- java - 在 for 循环中声明变量以及 print 语句时出错
- reactjs - Redux 表单问题(dispatch 不起作用?)
- angular - plainToClass 重载选择导致类型错误
- javascript - 如何将动态导入与流星链接?
- javascript - 试图将 python 转换为 javascript opencv.js
- javascript - 如何使用javascript在vscode中滚动webview
- selenium - 本地用户如何通过chromedriver运行自动化测试来继承用户的缓存/cookie?