【问题标题】:Is this loop method correct? Python 3.4.3这种循环方法正确吗? Python 3.4.3
【发布时间】:2016-07-30 23:32:49
【问题描述】:

所以,我想要一组彼此不同的 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)

重做的位置能否与列表中的其他位置相同?

【问题讨论】:

  • 顺便说一句,Python 3.0 真的被弃用了。 Python3.x 3.4 的开发已经
  • @Slayer 嗨,不。我只想要 6 个不同的 XY 位置
  • 制作positionList = set() 会删除线性查找,您是采样一次还是多次?您还需要使用元组添加到集合中
  • 那么我需要将每个位置转换为元组吗?
  • pos = random.randint(0,5),random.randint(0,5)if pos not in pos_setpos_set.add(pos) 代替你的追加

标签: python loops python-3.x location


【解决方案1】:

重做的位置可以和列表中的其他位置一样吗?

是的,因为您使用的是随机的。如果您想确保保留独特的物品,您可以使用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)])

【讨论】:

  • 不是我的,但请尝试set([(0, 0), (4, 5), (4, 2), (0, 0), (3, 4), (0, 0)]) ;) 重点是获得 6 个独特的对
  • @PadraicCunningham 那有什么问题呢?结果将是>>> set([(0, 0), (4, 5), (4, 2), (0, 0), (3, 4), (0, )]) set([(4, 5), (4, 2), (0, 0), (3, 4), (0,)]) 但请注意(0,) 在这种情况下无效。
  • 哈哈,肯定不会是set([(0, 0), (4, 5), (4, 2), (0, 0), (3, 4), (0, )]) ,套装有骗子吗?
  • @PadraicCunningham 集合理解并不能保证集合的长度总是 6。查看更新:)。
  • 是的,这就是我想要表达的观点;)+1 看到光明
【解决方案2】:

如果你真的需要列表,你可以转换,如果不只是让代码保持原样:

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) vs O(n) 查找列表,因为顺序似乎无关紧要,完全使用集合可能就足够了。

【讨论】:

  • 不用担心,你可以按照 kasra 的代码循环直到长度为 6,我只是让代码尽可能与你自己的相似
【解决方案3】:

要确定 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)]
"""

【讨论】:

    【解决方案4】:

    我刚刚运行了您的代码,它似乎工作得很好。我相信这是正确的。让我们考虑一下您的 while 循环。

    您检查随机生成的“位置”是否已经在“位置列表”列表中。单纯的说法:

    position in positionList
    

    返回 True 或 False。如果“位置”已经出现在您的列表中,则执行 while 循环。你只需计算另一个随机位置。

    我能给出的唯一建议是添加一个循环计数器。当您用完可能的 XY 位置时,循环将永远运行。

    【讨论】:

      【解决方案5】:

      这是一种方法

      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 值即可。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2010-11-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多