如何更改以从列表中选择大于2的唯一数字计数 - 刚才,只选择了两个数字。 [英] How to change to pick a count of unique numbers from the list greater than 2 – just now, only two numbers are picked.

查看:45
本文介绍了如何更改以从列表中选择大于2的唯一数字计数 - 刚才,只选择了两个数字。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

<pre>import random

longList = [ 0, 3, 4, 5, 6, 7, 8, 12, 15, 16, 17 ]

# Pick 2 unique numbers from longList, at random - any 2 numbers as long
# as they're not the same

uniques = [ longList[ random.randint( 0, len( longList ) - 1 ) ] ]

while len( uniques ) < 2:
    nextCandidate = random.randint( 0, len( longList ) - 1 )
    if longList[ nextCandidate ] != uniques[ 0 ]:
        uniques = uniques + [ longList[ nextCandidate ] ]

print(longList)
print("The unique numbers are", uniques[ 0 ], "and", uniques[ 1 ])





我是什么尝试过:



我输了这个我尝试了一些但不起作用



What I have tried:

I lost with this I have tried something but non works

推荐答案

首先,转储您的代码。

我建议的方法是:

1.转换原始列表到一个将消除重复元素的集合。

2.获取答案集的大小。

怎么样?查看您的编程说明或询问Google。
First, dump that code of yours.
My suggested approach is:
1. convert the original list to a set which will eliminate duplicate elements.
2. Get the size of the set which is the answer.
How? check out your programming note or ask Google.


这篇关于如何更改以从列表中选择大于2的唯一数字计数 - 刚才,只选择了两个数字。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆