1,如何实现"1,2,3"变成[‘1’,‘2’,‘3’]? 如何实现[‘1’,‘2’,‘3’]变成[1,2,3]?
a = '1,2,3'
print(a.split(','))
结果['1','2','3']
b = ['1', '2', '3']
print(list(map(lambda x: int(x), b)))
2,1,2,3,4,5
能组成多少个互不相同且无重复的三位数?
print([(i, j, k) for i in range(1, 6) for j in range(1, 6) for k in range(1, 6) if i != k and i != j and j != k])
输出结果为:
[(1, 2, 3), (1, 2, 4), (1, 2, 5), (1, 3, 2), (1, 3, 4), (1, 3, 5), (1, 4, 2), (1, 4, 3), (1, 4, 5), (1, 5, 2), (1, 5, 3), (1, 5, 4), (2, 1, 3), (2, 1, 4), (2, 1, 5), (2, 3, 1), (2, 3, 4), (2, 3, 5), (2, 4, 1), (2, 4, 3), (2, 4, 5), (2, 5, 1), (2, 5, 3), (2, 5, 4), (3, 1, 2), (3, 1, 4), (3, 1, 5), (3, 2, 1), (3, 2, 4), (3, 2, 5), (3, 4, 1), (3, 4, 2), (3, 4, 5), (3, 5, 1), (3, 5, 2), (3, 5, 4), (4, 1, 2), (4, 1, 3), (4, 1, 5), (4, 2, 1), (4, 2, 3), (4, 2, 5), (4, 3, 1), (4, 3, 2), (4, 3, 5), (4, 5, 1), (4, 5, 2), (4, 5, 3), (5, 1, 2), (5, 1, 3), (5, 1, 4), (5, 2, 1), (5, 2, 3), (5, 2, 4), (5, 3, 1), (5, 3, 2), (5, 3, 4), (5, 4, 1), (5, 4, 2), (5, 4, 3)]
3,有两个6面的筛子,同时抛掷100000次,求两个筛子点数之和所占的百分比?(输出字典形式)
def count_tt():
res = list()
for i in range(10000):
a = random.randint(1, 6)
b = random.randint(1, 6)
res.append(a + b)
res_d = dict()
for i in set(res):
res_d[i] = "{:.2%}".format(res.count(i) / 10000)
print(res_d)
count_tt()
输出结果为:
{2: '2.831%', 3: '5.499%', 4: '8.308%', 5: '10.965%', 6: '13.916%', 7: '16.805%', 8: '13.897%', 9: '11.112%', 10: '8.281%', 11: '5.495%', 12: '2.891%'}