Stores = ["alpha","bravo","charlie","delta"]
KnownName = Stores.pop()
KnownName2 = Stores.pop()
KnownName3 = Stores.pop()
print KnownName+" "+Stores.pop()
print KnownName2+" "+KnownName
print KnownName3+" "+KnownName2
print KnownName3
输出:
delta alpha
charlie delta
bravo charlie
bravo
所以如果我有一个m1,m2,m3 ...... mn的列表 我试图随机抽出这样的
m1
m2 m3
m4 m2
m5
m6 m7
m8 m9
m10 m8
m11 m10
如果查看输出,则一行中最多两个值。只有第一个值可以重复。
如何改进我的代码?
foo = ['m1', 'm2', 'm3', 'm4', 'm5','m6','m7','m8']
foo.reverse()
while foo:
try:
if random.randrange(0,2) == 0:
print foo.pop()
else:
Value2 = foo.pop()
print Value2 + " " + foo.pop()
if random.randrange(0,2) == 0:
Value3 = foo.pop()
print Value3 + " " + Value2
else:
foo.pop() + foo.pop()
except:
break
答案 0 :(得分:0)
from random import randint
foo = ['m1', 'm2', 'm3', 'm4', 'm5','m6','m7','m8']
while foo:
x = randint(0, len(foo) - 1)
y = randint(0, len(foo) - 1)
while x == y and len(foo) > 1:
y = randint(0, len(foo) - 1)
if randint(0, 1) == 0 or len(foo) <= 1:
print foo[x]
else:
print foo[y], foo[x]
foo = foo[:x] + foo[x+1:]