这种循环方法是否正确? Python 3.4.3
Is this loop method correct? Python 3.4.3
所以,我想要一组彼此不同的 XY 位置。为此,我使用了一个列表来存储随机生成的变量 XY。如果该位置不在列表中,则将其添加到列表中,如果在列表中,则为其重新创建位置。
我不确定这是否适用于所有情况,想知道是否有更好的方法。
import random
positionList = []
for i in range(6):
position = [random.randint(0,5),random.randint(0,5)]
print("Original", position)
while position in positionList:
position = [random.randint(0,5),random.randint(0,5)]
positionList.append(position)
print(position)
重做的位置可以和列表中的其他位置一样吗?
重做的位置能和列表中的其他位置一样吗?
是的,因为你使用的是随机的。如果您想确保保留独特的项目,您可以使用 set
对象来为您保留独特的项目。但请注意,由于列表不是可散列的对象,因此您应该为对(如元组)使用可散列的容器:
>>> position_set = set()
>>>
>>> while len(position_set) != 6:
... position = (random.randint(0,5), random.randint(0,5))
... position_set.add(position)
...
>>> position_set
set([(3, 2), (5, 0), (2, 5), (5, 2), (1, 0), (3, 5)])
要确定 6 个不同的元素,您可以使用 random.shuffle
:
from random import shuffle
all=[(x,y) for x in range(5) for y in range(5)]
shuffle(all)
print(all[:6])
"""
[(0, 1), (3, 4), (1, 1), (1, 3), (4, 3), (0, 0)]
"""
我只是 运行 你的代码,它似乎工作得很好。我相信这是正确的。让我们考虑一下您的 while 循环。
你检查随机生成的 'position' 是否已经在 'positionList' 列表中。纯声明:
position in positionList
returns 对或错。如果 'position' 已经出现在您的列表中,则执行 while 循环。你只需计算另一个随机位置。
我能给出的唯一建议是添加一个循环计数器。当您 运行 超出可能的 XY 位置时,循环 运行 永远停止。
如果你真的需要列表,你可以转换,如果不是,请保持代码不变:
import random
position_set = set()
for i in range(6):
position = random.randint(0, 5), random.randint(0, 5)
print("Original", position)
while position in position_set:
position = random.randint(0, 5), random.randint(0, 5)
position_set.add(position)
print(position)
print(position_set)
对于列表,集合查找是 O(1)
与 O(n)
,因为顺序似乎无关紧要,完全使用集合可能就足够了。
这是一种实现方式
import random
my_list =[]
num_of_points = 6
while True:
position = [random.randint(0,5),random.randint(0,5)]
if position not in my_list:
my_list.append(position)
print num_of_points
num_of_points -=1
if (num_of_points == 0):
break
print my_list
当然你只需要确保可能的随机对数超过num_op_points值即可。
所以,我想要一组彼此不同的 XY 位置。为此,我使用了一个列表来存储随机生成的变量 XY。如果该位置不在列表中,则将其添加到列表中,如果在列表中,则为其重新创建位置。
我不确定这是否适用于所有情况,想知道是否有更好的方法。
import random
positionList = []
for i in range(6):
position = [random.randint(0,5),random.randint(0,5)]
print("Original", position)
while position in positionList:
position = [random.randint(0,5),random.randint(0,5)]
positionList.append(position)
print(position)
重做的位置可以和列表中的其他位置一样吗?
重做的位置能和列表中的其他位置一样吗?
是的,因为你使用的是随机的。如果您想确保保留独特的项目,您可以使用 set
对象来为您保留独特的项目。但请注意,由于列表不是可散列的对象,因此您应该为对(如元组)使用可散列的容器:
>>> position_set = set()
>>>
>>> while len(position_set) != 6:
... position = (random.randint(0,5), random.randint(0,5))
... position_set.add(position)
...
>>> position_set
set([(3, 2), (5, 0), (2, 5), (5, 2), (1, 0), (3, 5)])
要确定 6 个不同的元素,您可以使用 random.shuffle
:
from random import shuffle
all=[(x,y) for x in range(5) for y in range(5)]
shuffle(all)
print(all[:6])
"""
[(0, 1), (3, 4), (1, 1), (1, 3), (4, 3), (0, 0)]
"""
我只是 运行 你的代码,它似乎工作得很好。我相信这是正确的。让我们考虑一下您的 while 循环。
你检查随机生成的 'position' 是否已经在 'positionList' 列表中。纯声明:
position in positionList
returns 对或错。如果 'position' 已经出现在您的列表中,则执行 while 循环。你只需计算另一个随机位置。
我能给出的唯一建议是添加一个循环计数器。当您 运行 超出可能的 XY 位置时,循环 运行 永远停止。
如果你真的需要列表,你可以转换,如果不是,请保持代码不变:
import random
position_set = set()
for i in range(6):
position = random.randint(0, 5), random.randint(0, 5)
print("Original", position)
while position in position_set:
position = random.randint(0, 5), random.randint(0, 5)
position_set.add(position)
print(position)
print(position_set)
对于列表,集合查找是 O(1)
与 O(n)
,因为顺序似乎无关紧要,完全使用集合可能就足够了。
这是一种实现方式
import random
my_list =[]
num_of_points = 6
while True:
position = [random.randint(0,5),random.randint(0,5)]
if position not in my_list:
my_list.append(position)
print num_of_points
num_of_points -=1
if (num_of_points == 0):
break
print my_list
当然你只需要确保可能的随机对数超过num_op_points值即可。