嘿,我需要以一种特殊的方式整理出三个列表,它们被混在一起,就像放在一排一排的甲板上(链接到完整的问题):
a=[1,2,3]
b=[4,5,6]
c=[7,8,9]现在的顺序是b,a,c,你必须把所有的数字从左到右(我需要这样做的甲板,所以数字将包含字母在他们旁边,使他们成为一个字符串)。
预期成果:
*注意我正在编写的列表中包含了3个以上的值
发布于 2017-05-18 18:34:34
只需zip他们:
>>> a=[1,2,3]
>>> b=[4,5,6]
>>> c=[7,8,9]
>>> a, b, c = map(list, zip(b, a, c)) # in the order specified: b, a, c
>>> a
[4, 1, 7]
>>> b
[5, 2, 8]
>>> c
[6, 3, 9]它也可以使用(但这会改变现有的列表):
>>> a[:], b[:], c[:] = zip(b, a, c)而不是map(list, ...)。
如果您有更多的价值,并希望分发它们:
>>> a=[1,2,3,4]
>>> b=[5,6,7,8]
>>> c=[9,10,11,12]
>>> tmp = b + a + c # concatenate them in the order
>>> # distribute them: every third element, starting with 0 (a), 1 (b) and 2 (c)
>>> a, b, c = tmp[::3], tmp[1::3], tmp[2::3]
>>> a
[5, 8, 3, 10]
>>> b
[6, 1, 4, 11]
>>> c
[7, 2, 9, 12]发布于 2017-05-18 17:57:08
一旦选定的桩已选定,你需要把选定的桩在中间的其他两桩。一种选择是将三个不同的列表合并成一个列表,所选的列表位于中间。
if pile == 1: NewCards = Pile2 + Pile1 + Pile3
elif pile == 2: NewCards = Pile1 + Pile2 + Pile3
else: NewCards = Pile1 + Pile3 + Pile2编辑:好的,所以你需要把卡片组合成3堆。你可以在每一个甲板上绕一圈,然后把它们分发出去。确保右甲板在中间!(在这种情况下,我将使用甲板2)。
# create three empty temporary lists
temp = [[],[],[]]
cardnum = 0
# loop through the 3 decks
for i in range(3):
if i == 0: deck = partA
elif i == 1: deck = partB
else: deck = partC
# loop through the cards in the decks
for card in deck:
i = cardnum % 3 # mod 3 to split the cards among the decks
temp[i] += card
cardnum += 1
# update the decks now
partA = temp[0]
partB = temp[1]
partC = temp[2]发布于 2017-05-18 19:03:44
嗯..。一个有趣的问题:P
我要做一些我讨厌的事,当人们对我做.邮编..。看看这里有没有什么能帮到你的。
提示提示:查看一下get_order函数。
from itertools import product, izip_longest
import random
def grouped(iterable, n, fillvalue=None):
return izip_longest(fillvalue=fillvalue, *[iter(iterable)] * n)
def get_order(selected, seq=[0,1,2]):
seq = [i for i in seq if i!=selected]
seq.insert(len(seq)/2, selected)
return seq
deck = ['-'.join(card) for card in product('c h d s'.split(), 'a 1 2 3 4 5 6 7 8 9 10 j q k'.split())]
plycards= random.sample(deck,21)
piles = [[],[],[]]
for idx, i in enumerate(grouped(plycards, 3)):
for idx, item in enumerate(i):
piles[idx].append(item)
print (i)
for j in range(0,3):
# Combine again
plycards = []
for i in get_order(int(raw_input('Which pile? '))-1):
plycards += piles[i]
print ('-------------')
piles = [[],[],[]]
for idx, i in enumerate(grouped(plycards, 3)):
for jdx, item in enumerate(i):
piles[jdx].append(item)
print (i)
print ("You selected card :'{}'".format(plycards[10])https://stackoverflow.com/questions/44054490
复制相似问题