【问题标题】:How to count how many times the value appears in the matrix如何计算值在矩阵中出现的次数
【发布时间】:2022-01-25 16:54:36
【问题描述】:

如何计算值在 Python 上出现在矩阵中的次数 获取矩阵和数字并检查数字在矩阵内的次数并返回值的函数

def how_many(mat,number):
    counter = 0
    counter =  list(map(lambda x: list(map(lambda z:x  += 1 if z == mat , x)), mat))

    return counter

示例:矩阵 {1,2,3,1} 和数字 = 1

打印:2

【问题讨论】:

  • {1,2,3,1} 在 python 中是不可能的,这是一个集合,只会保留一个 1 的实例

标签: python


【解决方案1】:

使用listcount 方法(如果您的矩阵是列表)

def how_many(mat, number):
    return mat.count(number)

print(how_many([1, 2, 3, 1], 1))

# Output:
2

【讨论】:

  • print(how_many(matrix(2),1)) 返回 0....
  • 你的矩阵是什么类型的?试试print(type(mat))mat 是包含矩阵的变量。
  • 没有 print(type(mat)) 的选择更多。垫子??
猜你喜欢
  • 2020-11-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-10-25
  • 2011-09-09
  • 2013-05-23
  • 1970-01-01
相关资源
最近更新 更多