Python的新手。 我在列表中有20个数字,每次都想要2个唯一值。因此,理想情况下,我最终有10行,并且每行都有两个以前从未使用过的唯一编号。我有这样的东西,但是它显示的数字不止一次。
numberList=["1","2","3","4","5","6","7","8","9","10","11","12","13","14","15","16","17","18","19","20"]
myset = set(numberList)
rows = len(numberList)//2
i = 0
while i < rows:
random_nums = random.sample(myset,2)
print(random_nums)
i += 1
OUTPUT:
['13', '8']
['19', '8']
['11', '9']
['16', '7']
['1', '10']
['11', '20']
['16', '18']
['18', '2']
['20', '10']
['7', '4']
答案 0 :(得分:3)
假设:
numberList
您可以使用这种非常简单有效的方法。从列表中抽样2个随机元素,并在列表不为空时将其删除。
import random
numberList = ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18",
"19", "20"]
while numberList:
a, b = random.sample(numberList, 2)
numberList.remove(a)
numberList.remove(b)
print(a, b)
示例输出:
9 14
13 12
7 5
10 2
15 20
19 4
11 1
18 3
17 6
8 16
如果稍后在代码中确实需要使用numberList
,则下一个有效的方法是处理一组拾取的元素。您将需要更复杂的逻辑:
import random
numberList = ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18",
"19", "20"]
taken = set()
pairs = len(numberList) // 2
for _ in range(pairs):
a, b = None, None
while a is None or a in taken:
a = random.choice(numberList)
taken.add(a)
while b is None or b in taken:
b = random.choice(numberList)
taken.add(b)
print(a, b)
答案 1 :(得分:0)
您可以从“ numberList”或更好的“ myset”中删除项目。
import random
numberList=["1","2","3","4","5","6","7","8","9","10","11","12","13","14","15","16","17","18","19","20"]
myset = set(numberList)
rows = len(numberList)//2
i = 0
while i < rows:
random_nums = random.sample(myset,2)
myset.remove(random_nums[0])
myset.remove(random_nums[1])
print(random_nums)
i += 1
答案 2 :(得分:0)
您可能还有一个对象:
seen = set()
在while循环的主体上:
while i < rows:
random_nums = set(random.sample(myset,2))
if not random_nums.issubset(seen):
print(random_nums)
seen.update(random_nums)
i += 1
答案 3 :(得分:0)
如果我说对了,那么您就不想重复这些数字。就像以前出现过一个数字一样,它不应再出现。然后,您可以相应地删除myset
的元素。像这样:
import random
numberList=["1","2","3","4","5","6","7","8","9","10","11","12","13","14","15","16","17","18","19","20"]
myset = set(numberList)
rows = len(numberList)//2
i = 0
while i < rows:
random_nums = random.sample(myset,2)
myset.remove(random_nums[0])
myset.remove(random_nums[1])
print(random_nums)
i += 1
答案 4 :(得分:0)
这应该可以解决问题
from random import choice
numberList =
["1","2","3","4","5","6","7","8","9","10","11","12","13","14","15","16","17",
"18","19","20"]
for i in range(10):
a = choice(numberList)
numberList.pop(numberList.index(a))
b = choice(numberList)
numberList.pop(numberList.index(b))
print([a, b])
答案 5 :(得分:0)
随着列表的增加,采样和删除将比您需要做的工作更多,并且效率很低。而是使用np.random.shuffle
numberList= ["1","2","3","4","5","6","7","8","9","10","11","12","13","14","15","16","17","18","19","20"]
np.random.shuffle(numberList)
for r in np.reshape(numberList, (10,2)):
print(r)
输出:
['18' '20']
['17' '16']
['11' '1']
['5' '13']
['7' '14']
['19' '15']
['6' '8']
['9' '12']
答案 6 :(得分:0)
您可以按照以下方式使用random.shuffle
来完成这项工作:
import random
numberList=["1","2","3","4","5","6","7","8","9","10","11","12","13","14","15","16","17","18","19","20"]
random.shuffle(numberList)
rows = len(numberList)//2
for i in range(rows):
random_nums = numberList[2*i:2*i+2]
print(random_nums)
请注意,random.shuffle
会改变列表的位置,即,如果您在代码末尾添加print(numberList)
,则会打印20个数字,但顺序是随机的。
答案 7 :(得分:0)
首先,如果您知道将始终使用整数,那么我认为将它们全部转换为整数会更容易,就像在下面的示例中使用列表推导那样。由于您恰好指定了20个数字和10对,因此我在脚本中对这些数字进行了硬编码,但是您可以轻松地设置变量。
import random
numberList = ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10",
"11", "12", "13", "14", "15", "16", "17", "18", "19", "20"]
# If you know you always have numbers, convert them all to integers → easier
numberList = [int(x) for x in numberList]
myset = set() # Will be used to add any number NOT seen before
numberList_pairs = [] # Will be used to build a list of unique pairs
while len(numberList_pairs) < 10: # You need 10 pairs in that new list
pair = []
# While you don't have a unique pair, keep generating random numbers
while len(pair) < 2:
random_num = random.randint(1, 20) # 1 to 20. Both ends are included
# If the number hasn't been seen before
if random_num not in myset:
myset.add(random_num)
pair.append(random_num)
numberList_pairs.extend([pair]) # Add the pair as a list
for pair in numberList_pairs:
print(pair)
这将是一个示例输出:
[20, 14]
[16, 2]
[13, 10]
[6, 12]
[9, 18]
[3, 19]
[17, 11]
[1, 5]
[8, 4]
[15, 7]
注意:如果您确实需要输出为字符串列表,则可以在最终循环中将其转换回字符串,如下所示:
for a, b in numberList_pairs:
a, b = str(a), str(b)
print([a, b])
您将获得与最初发布的输出相同的输出:
['11', '9']
['2', '15']
['7', '12']
['13', '3']
['19', '20']
['1', '14']
['16', '5']
['18', '8']
['10', '6']
['4', '17']