python - Create new array from a defined array
问题描述
If I have an array, a = [1,2,3,4,2,1]
, how can I create another array, which shows the number of times each number in array a has been repeated, for example from array a then the new array would be b = [2,2,1,1]
? Is this possible using a command in the NumPy library?
解决方案
我敢肯定有很多方法可以做到这一点。这是一个:
a = [1,2,3,4,2,1]
count = {}
for item in a:
count[item] = count.get(item, 0) + 1
[v for k,v in count.items()]
结果:
[2, 2, 1, 1]
推荐阅读
- azure - 术语“msbot”未被识别为 cmdlet、函数、脚本文件或可运行程序的名称
- html - css-grid 创建一个假想的列
- vb.net - 小数四舍五入
- php - URL 中 nginx 非 ASCII 字符的 Wordpress 出现 404 错误
- dart - Dart提供的隔离对于单独执行推送的导航器是否有用
- azure - 将 SetPolicy 与 Azure 和 Windows IoT 结合使用
- java - Apache 公用 CSV | 如何在字段中忽略/包含分号、逗号?
- python - 通过正则表达式拆分word文档,然后将类似标题分组到自己的对象中
- php - Wordpress:array_push 在前一个数组中创建数组
- crystal-reports - 关于如何构建 BI 和 Crystal Reports 的基本了解的建议?