python - 如何打印多个数组/列表中的多个值,字符串仅在第一个输出前面,值之间用逗号分隔?
问题描述
我想将我的输出格式化为“Mode = 15, 59”。我对 Python 还很陌生,所以可能有一种完全更简单的方法,我只是还不知道或者如何更好地用谷歌搜索这个问题。感谢您的帮助。
请看下面的代码:
import numpy as np
dataset = [15, 21, 59, 15, 37, 59, 11, 41]
dataset.sort()
print(dataset)
unique = np.unique(dataset)
print(unique)
testing = []
for d in unique:
testing.append(dataset.count(d))
print(testing)
combine = np.column_stack((unique,testing))
print(combine)
g = max(testing)
for i, j in combine:
if j == g:
#print('{}'.format(i), end='')
#print("Mode = %s" %(i),end='')
print('Mode =', i, end='')
#print(i, end='')
输出:
[11, 15, 15, 21, 37, 41, 59, 59]
[11 15 21 37 41 59]
[1, 2, 1, 1, 1, 2]
[[11 1]
[15 2]
[21 1]
[37 1]
[41 1]
[59 2]]
Mode = 15Mode = 59
解决方案
combine = [
[11, 1],
[15, 2],
[21, 1],
[37, 1],
[41, 1],
[59, 2],
]
g = 2
modes = []
for i, j in combine:
if j == g:
modes.append(str(i))
print('Mode =', ', '.join(modes))
推荐阅读
- laravel - Laravel - 如何在 Laravel 应用程序中捕获所有异常并防止用户看到异常
- javascript - 如何将 mongodb 时间戳转换为实际日期?
- .net - 在 Jenkins 的声纳扫描阶段生成的测试结果文件和覆盖率文件存在问题
- excel - 将问题 PDF 复制并粘贴到 Excel
- java - 如何解决 pyspark Databricks 中的 GC 开销
- javascript - 使用 map 遍历对象数组时访问数组内的嵌套对象值
- angular - 我们如何在 rxjs 的管道中捕获事件发射器的事件发射
- wpf - 使用基于视图的导航时隐藏 Prism 区域
- animation - 逆向工程 3d 动画浮动压缩
- ruby-on-rails - Rails - 如何配置不同的 s3 上传和下载位置?