Python检测字符串中的两个相同的东西

标签 python

while testcount < 100:  
   num1=randint(0,10)
   num2=randint(0,10)
   num3=randint(0,10)
   num4=randint(0,10)
   num5=randint(0,10)
   numo=[num1,num2,num3,num4,num5]

   if #there are two 7s# in numo:
      testcount=testcount+1
      num_of_successes=num_of_successes+1
   else:
      testcount=testcount+1

    print(num_of_successes,"out of 100 there were two 7s")

如何检测“numo”中是否有两个 7? 即使它没有使用很多代码。

最佳答案

对于一个列表,可以使用count()函数来查询count函数传入的参数中的元素在列表中有多少。

例子-

>>> l = [1,2,3,4,5,6,4]
>>> l.count(4)
2

关于Python检测字符串中的两个相同的东西,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30994829/

相关文章:

python - 正则表达式转义字符串的一部分

python - 如何不计算单词之间的标点符号

python - pandas multi-index 如何通过二级屏蔽数据

python - Anaconda 安装 TensorFlow 1.15 而不是 2.0

python - Django 如何在 Amazon S3 中存储 HTML 文件

python - Django休息框架时间字段输入格式

python - Python 3.2 可以在 Windows 2000 上运行吗?

python - 从 numpy 数组在 paraview 中创建等高线图

python - 如何在 sympy 中默认设置 **设置

python - 构建这个简单工厂类的更 pythonic 方法是什么?