luokaoge
发表于 2018-3-23 18:30:45
本帖最后由 luokaoge 于 2018-3-23 19:04 编辑
import random
time = random. randint(3,10)
def dice():
print('掷骰子游戏,请掷' + str(time) + '轮骰子,每人掷三次骰子。')
list_1 = []
print('开始' + '\n' + '.'+ '\n' + '..' + '\n' + '...')
for i in range(1,time+1):
list_2 = []
for i in range(0,3):
a = random. randint(1,6)
list_2.append(a)
list_1.append(tuple(list_2))
print('掷的骰子为:' + str(list_1))
b = []
d = []
f = []
for i in range(0,time):
a = []
com = list_1
if com == com == com:
a.append(com)
a.append(i)
else:
a.append(0)
a.append(i)
b.append(tuple(a))
for i in range(0,time):
c = []
com = list_1
if com == com and com != com:
c.append(com)
c.append(i)
elif com == com and com != com:
c.append(com)
c.append(i)
elif com == com and com != com:
c.append(com)
c.append(i)
else:
c.append(0)
c.append(i)
d.append(tuple(c))
for i in range(0,time):
e = []
com = list_1
if com != com and com != com and com != com:
e.append(com + com + com)
e.append(i)
else:
e.append(0)
e.append(i)
f.append(tuple(e))
b.sort(key=lambda x:x)
d.sort(key=lambda x:x)
f.sort(key=lambda x:x)
b = list(reversed(b))
d = list(reversed(d))
f = list(reversed(f))
list_r = []
for i in range(0,time):
com1 = list(b)
if com1 != 0:
list_r.append(list_1])
for i in range(0,time):
com1 = list(d)
if com1 != 0:
list_r.append(list_1])
for i in range(0,time):
com1 = list(f)
if com1 != 0:
list_r.append(list_1])
print('大小顺序为' + str(list_r))
dice()
dfh93@163.com
发表于 2018-3-23 19:38:38
def shu_sum(x,y,z):
if (x==y) and (x==z):
p=(x+y+z)*100
elif ((x==y) and (x!=z)) or ((x==z) and (y!=z)) or ((y==z) and (x!= y)):
p=(x+y+z)*10
else:
p=x+y+z
return p
b=[]
c=[]
d=[]
a=[(4, 1, 1), (2, 2, 2), (5, 6, 1), (4, 3, 6), (2, 4, 3), (1, 1, 1), (4, 3, 6), (6, 3, 3), (1, 3, 6), (4, 4, 3), (6, 2, 2), (1, 5, 2), (5, 6, 6), (1, 4, 6), (2, 1, 6), (1, 3, 3), (6, 6, 5)]
num_long=len(a)
for i in range(num_long):
m=a
n=a
l=a
b.append(shu_sum(m,n,l))
c.append(shu_sum(m,n,l))
print(b)
c.sort()
c.reverse()
for i in range(num_long):
j=0
var = 1
while var==1:
if c==b:
d.append(a)
var=2
j+=1
print("********** 排序的列表 **********")
print(d)
海阔天空0987
发表于 2018-3-23 21:00:52
s=[(4, 1, 1), (2, 2, 2), (5, 6, 1), (4, 3, 6), (2, 4, 3), (1, 1, 1), (4, 3, 6), (6, 3, 3), (1, 3, 6), (4, 4, 3), (6, 2, 2), (1, 5, 2), (5, 6, 6), (1, 4, 6), (2, 1, 6), (1, 3, 3), (6, 6, 5)]
num=len(s)
a=0;b=0;c=0
A=[()]*2;B=[()]*8;C=[()]*7
#获取三种不同的元组的个数并分别存储在A、B、C列表中
for i in range(0,num):
n=();n=s
if n == n == n:
a +=1
A=n
elif n != n and n != n and n != n:
b +=1
B=n
else:
c +=1
C=n
#对每个元组进行求和
def total(t):
tn=len(t)
ts=0
i=0
while i <= tn-1:
ts +=t
i +=1
return ts
#定义对A和B排序的函数list_fuc(N)
def list_fuc(N):
sub=[()]
for j in range(0,len(N)):
for i in range(j+1,len(N)):
if total(N) < total(N):
sub=N
N=N
N=sub
return N
#定义函数double_num(M),将重复的数字放在第一位,不重复的数字放在第二位
def double_num(M):
m=[]*2
if M==M:
m=,M]
elif M==M:
m=,M]
else:
m=,M]
return m
#对C进行排序
suc=[()]
for i in range(0,c):
for j in range(i+1,c):
if double_num(C) < double_num(C) or (double_num(C) == double_num(C) and double_num(C) < double_num(C)) :
suc=C
C=C
C=suc
#将结果打印出来
print list_fuc(A)+C+list_fuc(B)
wyf345491656
发表于 2018-3-23 22:45:08
import random
tri = {}
dou = {}
sin = {}
list1 = []
times = (int)(input('请输入掷骰子次数:'))
for num in range(0, times):
a = random.randint(1, 6)
b = random.randint(1, 6)
c = random.randint(1, 6)
if (a == b) and (b == c):
tri[(a, b, c)] = a
elif (a == b) or (b == c) or (a == c):
if a == b:
dou[(a, b, c)] = a
elif b == c:
dou[(b, c, a)] = b
elif a == c:
dou[(c, a, b)] = c
else:
sin[(a, b, c)] = a + b + c
list_tri = sorted(tri.items(), key = lambda item : item, reverse = True)
list_dou = sorted(dou.items(), key = lambda item : item, reverse = True)
list_sin = sorted(sin.items(), key = lambda item : item, reverse = True)
for i in range(0, len(list_tri)):
list1.append(list_tri)
for i in range(0, len(list_dou)):
list1.append(list_dou)
for i in range(0, len(list_sin)):
list1.append(list_sin)
print(list1)
差一个功能:两个一样的,若两个一样的点数相同,如何比较第三个的点数。。。小白求教
8306最硬
发表于 2018-3-23 23:15:27
本帖最后由 8306最硬 于 2018-3-23 23:35 编辑
排序一直是弱项啊{:10_266:}
def fun(lst):
def fun1(x):
if x == x == x: return -7
if x == x: return -x
if x == x: return -x
return 0
lst = sorted(lst, key = sum, reverse = True)
lst = sorted(lst, key = fun1)
return lst
tsembrace
发表于 2018-3-23 23:32:17
'''
这回我们来掷骰子,每轮掷3个骰子,多轮掷后,将骰子的点数记为一个列表,例如:
[(4, 1, 1), (2, 2, 2), (5, 6, 1), (4, 3, 6), (2, 4, 3), (1, 1, 1), (4, 3, 6),
(6, 3, 3), (1, 3, 6), (4, 4, 3), (6, 2, 2), (1, 5, 2), (5, 6, 6), (1, 4, 6),
(2, 1, 6), (1, 3, 3), (6, 6, 5)]
现在要将每轮的掷骰子的情况由大到小排序,
规则是,三个一样的最大,两个一样的次之,然后是都不一样的。
三个一样的最大再以点数排序,两个一样的,先比较两个一样的点数,
如果还相同,再比较第三个的点数,都不一样的按点数之后。
例如:(6,6,6)>(1,1,1)>(5,5,2)>(4,4,6)>(6,5,4)>(3,2,1)
最后给出按此规则排序的列表。
注:点数相同的排序次序不限,如(6,3,1)和(5,4,1)谁排在前面都行。
'''
#定义一个函数,参数为元组,返回一个数字
#各个位数表达分别为:点数相同个数、点位相同的数值、各点数之和(占两位)
def mysort(t):
s=t+t+t
num=0
if t==t:
if t==t:
num=3000+t*100+s
else:
num=2000+t*100+s
elif t==t:
num=2000+t*100+s
elif t==t:
num=2000+t*100+s
else:
num=1000+s
return num
myl=[(4, 1, 1), (2, 2, 2), (5, 6, 1), (4, 3, 6), (2, 4, 3), (1, 1, 1), (4, 3, 6),
(6, 3, 3), (1, 3, 6), (4, 4, 3), (6, 2, 2), (1, 5, 2), (5, 6, 6), (1, 4, 6),
(2, 1, 6), (1, 3, 3), (6, 6, 5)]
print(sorted(myl,key=mysort,reverse=True))
冰封雪舞
发表于 2018-3-24 00:58:48
本帖最后由 冰封雪舞 于 2018-3-24 01:11 编辑
import random
tt=[]
w=int(input('请输入你想要扔骰子的次数'))
for i in range(0,w):
a = random.randint(1,6)
b = random.randint(1,6)
c = random.randint(1,6)
tt.append()
print('3个骰子扔%d次是'%w,tt)
a1=[]
b20=[]
b=[]
c3=[]
for i in tt:
if i==i==i:
a1.append(i)
elif i==i:
b20.append(,i,tt.index(i)])
elif i==i:
b20.append(,i,tt.index(i)])
elif i==i:
b20.append(,i,tt.index(i)])
else:
c3.append(i)
a=sorted(a1,key=lambda x:x,reverse=True)
b0=sorted(b20,key=lambda x:x,reverse=True)
b0=sorted(b0,key=lambda x:x,reverse=True)
for bb in b0:
b.append(tt])
c=sorted(c3,key=lambda x:x+x+x,reverse=True)
a.extend(b)
a.extend(c)
print('按规则排序后是',a)
shigure_takimi
发表于 2018-3-24 08:19:34
#!/usr/bin/python
a = [(4, 1, 1), (2, 2, 2), (5, 6, 1), (4, 3, 6), (2, 4, 3), (1, 1, 1), (4, 3, 6), (6, 3, 3), (1, 3, 6), (4, 4, 3), (6, 2, 2), (1, 5, 2), (5, 6, 6), (1, 4, 6), (2, 1, 6), (1, 3, 3), (6, 6, 5)]
def sortA(a):
allSame = sorted(, key = lambda x: x, reverse = True)
twoSame =
noSame = sorted(, key = lambda x: sum(x), reverse = True)
def f(lst):
a,b = sorted(, key = lambda x: lst.count(x), reverse = True)
return a,b
twoSame = sorted(twoSame, key = f, reverse = True)
allSame.extend(twoSame)
allSame.extend(noSame)
return allSame
print(sortA(a))
##结果:
##[(2, 2, 2), (1, 1, 1), (5, 6, 6), (6, 6, 5), (4, 4, 3), (6, 3, 3), (1, 3, 3), (6, 2, 2), (4, 1, 1), (4, 3, 6), (4, 3, 6), (5, 6, 1), (1, 4, 6), (1, 3, 6), (2, 4, 3), (2, 1, 6), (1, 5, 2)]
victor.xu
发表于 2018-3-24 09:23:20
本帖最后由 victor.xu 于 2018-3-24 10:17 编辑
def f(lis):
lis1 = []
lis3 = []
for i in lis:
lis2 = []
if len(set(i)) == 1:
n = 3
elif len(set(i)) == 2:
n = 2
else:
n = 0
lis2.append(n)
j = list(i)
for k in j:
if j.count(k) == 2:
j.remove(k)
j.remove(k)
j.insert(0, k)
j.insert(0, k)
i = tuple(j)
lis2.append(i)
lis1.append(lis2)
lis1.sort(reverse = True)
for m in lis1:
lis3.append(m)
return lis3
lis = [(4, 1, 1), (2, 2, 2), (5, 6, 1), (4, 3, 6), (2, 4, 3), (1, 1, 1), (4, 3, 6), (6, 3, 3), (1, 3, 6), (4, 4, 3), (6, 2, 2), (1, 5, 2), (5, 6, 6), (1, 4, 6), (2, 1, 6), (1, 3, 3), (6, 6, 5)]
result = f(lis)
print(result)
yjsx86
发表于 2018-3-24 09:36:19
本帖最后由 yjsx86 于 2018-3-24 10:10 编辑
import random
def rankPoint(times):
while times:
points = [(1,1,1)]+) for x in range(15)]+[(5,5,5)]
print('排序前:%s' % points)
print('排序后:%s' % sorted(points,key=lambda x:(len(set(x)),[-i for i in x if x.count(i) == 2],-sum(x))))
print()
times -= 1
if __name__ == '__main__':
rankPoint(5)
排序前:[(1, 1, 1), (2, 5, 4), (6, 3, 2), (4, 4, 3), (5, 2, 6), (3, 5, 6), (1, 2, 4), (1, 2, 4), (5, 1, 6), (1, 4, 4), (4, 4, 4), (3, 1, 3), (6, 2, 4), (3, 3, 3), (4, 1, 5), (1, 3, 4), (5, 5,5)]
排序后:[(5, 5, 5), (4, 4, 4), (3, 3, 3), (1, 1, 1), (4, 4, 3), (1, 4, 4), (3, 1, 3), (3, 5, 6), (5, 2, 6), (5, 1, 6), (6, 2, 4), (2, 5, 4), (6, 3, 2), (4, 1, 5), (1, 3, 4), (1, 2, 4), (1, 2,4)]
排序前:[(1, 1, 1), (1, 6, 5), (1, 3, 3), (1, 5, 5), (6, 2, 4), (3, 2, 3), (1, 3, 5), (2, 6, 4), (1, 4, 4), (5, 1, 5), (3, 3, 6), (5, 4, 2), (4, 4, 1), (5, 6, 3), (2, 1, 5), (1, 6, 3), (5, 5,5)]
排序后:[(5, 5, 5), (1, 1, 1), (1, 5, 5), (5, 1, 5), (1, 4, 4), (4, 4, 1), (3, 3, 6), (3, 2, 3), (1, 3, 3), (5, 6, 3), (1, 6, 5), (6, 2, 4), (2, 6, 4), (5, 4, 2), (1, 6, 3), (1, 3, 5), (2, 1,5)]
排序前:[(1, 1, 1), (5, 5, 4), (3, 1, 6), (2, 3, 5), (6, 1, 4), (3, 6, 6), (5, 3, 3), (2, 6, 6), (6, 2, 3), (6, 6, 2), (5, 3, 1), (2, 5, 3), (6, 5, 2), (5, 5, 2), (3, 6, 6), (6, 1, 3), (5, 5,5)]
排序后:[(5, 5, 5), (1, 1, 1), (3, 6, 6), (3, 6, 6), (2, 6, 6), (6, 6, 2), (5, 5, 4), (5, 5, 2), (5, 3, 3), (6, 5, 2), (6, 1, 4), (6, 2, 3), (3, 1, 6), (2, 3, 5), (2, 5, 3), (6, 1, 3), (5, 3,1)]
排序前:[(1, 1, 1), (6, 5, 5), (2, 1, 4), (6, 4, 3), (2, 6, 4), (5, 1, 5), (3, 4, 2), (6, 1, 2), (3, 6, 3), (4, 1, 1), (4, 2, 2), (2, 5, 6), (3, 2, 3), (6, 2, 2), (5, 3, 2), (6, 5, 3), (5, 5,5)]
排序后:[(5, 5, 5), (1, 1, 1), (6, 5, 5), (5, 1, 5), (3, 6, 3), (3, 2, 3), (6, 2, 2), (4, 2, 2), (4, 1, 1), (6, 5, 3), (6, 4, 3), (2, 5, 6), (2, 6, 4), (5, 3, 2), (3, 4, 2), (6, 1, 2), (2, 1,4)]
排序前:[(1, 1, 1), (1, 4, 1), (5, 4, 1), (4, 2, 5), (5, 6, 5), (3, 3, 4), (1, 2, 6), (3, 6, 5), (3, 5, 2), (6, 2, 3), (4, 6, 2), (4, 5, 6), (2, 6, 4), (4, 6, 3), (5, 2, 5), (2, 5, 5), (5, 5,5)]
排序后:[(5, 5, 5), (1, 1, 1), (5, 6, 5), (5, 2, 5), (2, 5, 5), (3, 3, 4), (1, 4, 1), (4, 5, 6), (3, 6, 5), (4, 6, 3), (4, 6, 2), (2, 6, 4), (4, 2, 5), (6, 2, 3), (5, 4, 1), (3, 5, 2), (1, 2,6)]
晓屁屁
发表于 2018-3-24 09:57:29
写的脑瓜子老大,总算写出来了
import random
number = []
list1 =
three = []
two =[]
one = []
two_dict = {}
def dice_number():
number = []
for a in range(3):
number.append(random.randint(list1,list1))
return list(number)
def people_number(num):
count = 0
for i in range(num):
number.append(dice_number())
yield sorted(number,reverse=True)
count += 1
def input_num():
count = 0
temp = int(input('请输入几个人玩游戏:'))
for i in people_number(temp):
count += 1
input('第%d个人是%s'%(count,i))
def change_num():
count = -1
for a in number:
count += 1
for b in range(len(a)):
if a == a == a:
three.append(sorted(a,reverse=True))
count = -1
break
if a == a or a == a or a ==a:
two.append(sorted(a,reverse=True))
count = -1
break
else:
one.append(sorted(a,reverse=True))
count = -1
break
def main():
input_num()
change_num()
thr = sorted(three,reverse=True)
for a in thr:
print(tuple(a),end='>')
for b in two:
for c in range(1):
if b == b:
if (b,b) in two_dict.keys():
two_dict,b] += str(b)
else:
two_dict, b] = str(b)
elif b == b:
if (b,b) in two_dict.keys():
two_dict,b] += str(b)
else:
two_dict, b] = str(b)
elif b == b:
if (b,b) in two_dict.keys():
two_dict,b] += str(b)
else:
two_dict, b] = str(b)
two_dict1 = dict(sorted(two_dict.items(), key=lambda e: e, reverse=True))
cc = 0
for aa in two_dict1.values():
for bb in aa :
cc += 1
try:
for d,e in two_dict1.items():
for f in sorted(e,reverse=True):
cc -= 1
if cc or one != []:
print('(',d,',',d,',',f,')',end='>')
else:
print('(',d,',',d,',',f,')',end='')
except IndexError:
pass
one_dict1 = dict()
one_list1 = []
dd = 0
for g in one:
for k in range(1):
one_list1.append(g+g+g)
for m in one:
for l in range(1):
if (m,m,m) in one_dict1.keys():
dd += 1
one_dict1[((m,m,m)),dd] = m+m+m
else:
one_dict1[(m, m, m)] = m + m + m
one_dict2 = dict(sorted(one_dict1.items(), key=lambda eee: eee, reverse=True))
con = len(one_dict2.values())
for kkk in one_dict2.keys():
con -= 1
if len(kkk) == 3 and con != 0:
print(kkk,end='>')
elif len(kkk) == 3 and con == 0:
print(kkk,end='')
if len(kkk) !=3 and con !=0:
print(kkk,end='>')
elif len(kkk) !=3 and con ==0:
print(kkk,end='')
main()
the0503
发表于 2018-3-24 11:55:35
本帖最后由 the0503 于 2018-3-24 23:05 编辑
import random
def rev(lst):#添加标识符并内部排序
lst2=sorted(list(lst))
lst2.reverse()
lst1=[]
x,y,z = lst2
if x == y and y == z:
lst1 =
return lst1
elif x == y or y ==z :
lst1 =
return lst1
else:
lst1 =
return lst1
def zhi():#生成数组
a1=random.randint(1,6)
a2=random.randint(1,6)
a3=random.randint(1,6)
return (a1,a2,a3)
def comp(lst1,lst2):
if lst1== 2 and lst2 ==2: #比较标识符
if lst1>lst2: #特例是有一对时,比较第二位即可
return True
elif lst1 == lst2:
return sum(lst1) > sum(lst2)
else:
return False
elif lst1== 1 and lst2 ==1:
return sum(lst1) > sum(lst2)
else:
return lst1>lst2
def sortlst(lst):
c = len(lst)
for i in range(c-1):
for l in range(c-1-i):
if comp(lst,lst)==False:
lst,lst = lst,lst
return lst
lst = []
lst1 = []
lst3 = []
num = int(input('骰子掷几次:'))
for i in range(num):#生成骰子数组
temp = zhi()
lst.append(temp)
for l in lst: #添加对比符号
l = rev(l)
lst1.append(l)
lst2=sortlst(lst1)
for m in lst2:
lst3.append(m)
print(lst3)
冬雪雪冬
发表于 2018-3-24 21:32:51
the0503 发表于 2018-3-24 11:55
测试有问题
骰子掷几次:30
[(1, 1, 1), (6, 6, 3), (6, 6, 3), (6, 5, 3), (6, 5, 3), (6, 5, 3), (5, 5, 4), (5, 5, 2), (5, 5, 1), (5, 5, 1), (5, 5, 1), (6, 4, 4), (5, 3, 3), (6, 4, 2), (3, 3, 2), (6, 2, 2), (6, 4, 2), (6, 4, 1), (6, 4, 1), (6, 4, 1), (6, 3, 2), (5, 4, 3), (5, 2, 2), (3, 2, 2), (2, 2, 1), (5, 4, 3), (5, 1, 1), (5, 2, 1), (4, 3, 2), (4, 3, 1)]
冬雪雪冬
发表于 2018-3-24 21:33:07
评分截至标记
Chase_Kas
发表于 2018-3-24 22:10:53
塔利班 发表于 2018-3-22 21:29
大佬们的思路总是让人耳目一新,一些常见的BIF也能玩出花样~~看来想要进阶,得认真看看help()了~~
塔利班
发表于 2018-3-24 22:15:29
Chase_Kas 发表于 2018-3-24 22:10
大佬们的思路总是让人耳目一新,一些常见的BIF也能玩出花样~~看来想要进阶,得认真看看help()了~~
不是大佬,3个月前我是C语言都不懂的人,想跳槽才开始接触python
Chase_Kas
发表于 2018-3-24 22:26:24
本帖最后由 Chase_Kas 于 2018-3-24 22:30 编辑
第四时空 发表于 2018-3-22 20:10
修改
怎么这么厉害的..
坑得飞起
发表于 2018-3-24 23:26:26
新手·ing 发表于 2018-3-22 21:26
>>> fuck([(3,3,1),(3,2,3),(4,3,3),(3,3,3)])
[(3, 3, 3), (3, 3, 1), (3, 2, 3), (4, 3, 3)]
冰封雪舞
发表于 2018-3-25 19:01:03
新手·ing 发表于 2018-3-22 21:26
我....这个代码写的简洁、漂亮,羡慕,看了半天,最后发现好像如果有2次2个筛子数值一样的没法根据第三个筛子分大小,比如(2,3,2),(2,1,2),(2,4,2),(2,55,2),其实分辨这种情况才是这道题最难的难点,结果你用颜值混过去了,能把这种情况也写出来吗?我也学习学习,我写的好长,虽然能解决问题,但是看着好笨拙。
冰封雪舞
发表于 2018-3-25 19:06:22
塔利班 发表于 2018-3-22 21:29
你这个纯属考颜值糊弄人,两个筛子仍一样的点数,按两个一样大小的筛子数值分大小,如果出现两个这种情况,再按第三个筛子分大小,你这算法很多种情况分的都错的。