如何从21个唯一值列表中获取3个唯一值列表?

时间:2018-04-17 09:05:33

标签: python list

我正在做基本的Python卡技巧,我搜索的内容太复杂了,这是我的代码:

import random
import itertools
deck = ["S A","S 2","S 3","S 4","S 5","S 6","S 7","D A","D 2","D 3","D 4","D 
5","D 6","D 7","C A","C 2","C 3","C 4","C 5","C 6","C 7"]
random.shuffle(deck)
for z in range(3):
    print("Pile ",[z+1])
    for i in range(7):
        print(deck[i])

我想要做的就是得到它,当我得到我的3堆时,我不想要任何重复的值,但我不知道我是如何做到的,或者我是如何得到它们的3个单独的栏目?

4 个答案:

答案 0 :(得分:0)

使用

piles=[deck[0::3],deck[1::3],deck[2::3]]

此构建3列表分别从位置0,1和2开始,从一个项目跳到另一个项目,长度为3.无需知道初始列表的大小。

  • 首先选择0,3,6,......
  • 项目
  • 第二次选择第1,4,7项......
  • 第三个拿起第2,5,8,......

答案 1 :(得分:0)

代码中的轻微修改。

df_noyau_yes

答案 2 :(得分:0)

试试这个:

import random

deck = ["S A","S 2","S 3","S 4","S 5","S 6","S 7","D A","D 2","D 3","D 4","D 5","D 6","D 7","C A","C 2","C 3","C 4","C 5","C 6","C 7"]
data = {}
for i in range(3):
    data[i] = []
    for j in range(7):
        data[i].append(deck.pop(random.randint(0,len(deck)-1)))
print(data)

答案 3 :(得分:0)

Randex!正如你在问题中提到的那样,我并没有想到制作3列,但这可能是你有用的:

import random

deck = [
  "S A", "S 2", "S 3", "S 4", "S 5", "S 6", "S 7",
  "D A", "D 2", "D 3", "D 4", "D 5", "D 6", "D 7",
  "C A", "C 2", "C 3", "C 4", "C 5", "C 6", "C 7"
  ]

hands = {}

for stack in range(3):
  hands[stack] = []

您也可以稍后为.append()分配一个随机生成的值,然后从列表中删除以获得更多代码可读性

  for card in range(7):
    hands[stack].append(deck.pop(random.randint(0, len(deck) - 1)))

“牌”将从牌组中分配一张随机牌,直到每个“牌”牌都有7张牌叠加

for stack in hands:
  print(stack, '-', hands[stack])