【问题标题】:How to count same strings in numpy array?如何计算numpy数组中的相同字符串?
【发布时间】:2018-04-03 12:33:41
【问题描述】:

我有一个来自字符串的 numpy 数组,我想计算相同的字符串。是否可以?这是我的代码:

import numpy as np
import sys
arr = np.array(sys.stdin.read().split(), dtype = '>U20')
print(arr)

【问题讨论】:

  • “数数”是什么意思?你想要len(arr)吗?
  • 我相信你要找的是collections.Counter()
  • 我的意思是相同单词的数量。
  • 我有一个单词数组,里面有很多相同的单词,我需要数一数

标签: python python-3.x numpy


【解决方案1】:

试试这个:

import collections, numpy

collections.Counter(arr)

或者

unique, counts = numpy.unique(arr, return_counts=True)
dict(zip(unique, counts))

【讨论】:

  • 谢谢,这就是我所需要的
猜你喜欢
  • 1970-01-01
  • 2011-07-04
  • 2017-04-17
  • 1970-01-01
  • 2021-03-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多