如何在Python中打印项目及其来源列表?

时间:2015-05-25 18:47:20

标签: python list

我正在尝试创建一个轮盘模拟器,目前程序只打印数字。如何打印它所在的数字和列表标题?

示例:

  • 12红色
  • 22黑色
  • import random
    red=[1,3,5,7,9,12,14,16,18,19,21,23,25,27,30,32,34,36]
    black=[2,4,6,8,10,11,13,15,17,20,22,24,26,28,29,31,33,35]
    spin=random.randint(0,36)
    print(spin)
    

    3 个答案:

    答案 0 :(得分:2)

    if spin in red:
        print('{} Red'.format(spin))
    elif spin in black:
        print('{} Black'.format(spin))
    else:
        print('{}'.format(spin))
    

    答案 1 :(得分:2)

    您只需检查两个条件,可以是0,也可以是红色或黑色,如果不是0而不是红色则必须是黑色

    import random
    
    red = {1, 3, 5, 7, 9, 12, 14, 16, 18, 19, 21, 23, 25, 27, 30, 32, 34, 36}
    spin = random.randint(0, 36)
    print("{} {}".format(spin, ["black","red"][spin in red] if spin != 0 else "green"))
    

    明确写出:

    red = {1, 3, 5, 7, 9, 12, 14, 16, 18, 19, 21, 23, 25, 27, 30, 32, 34, 36}
    
    spin = random.randint(0, 36)
    if spin == 0:
       print("green 0")
    elif spin in red:
        print("red {}".format(spin))
    else:
        print("black {}".format(spin))
    

    黑色的数字列表无关紧要。

    如果你想表示00,你将需要使用字符串而不是整数,因为前导0表示python 2中的八进制,并且在python3中是无效的语法

    import random
    
    red = {'25', '12', '14', '16', '19', '32', '30', '23', '36', '34', '1', '3', '27', '5', '7', '18', '9', '21'}
    
    nums = ["00",'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12', '13', '14', '15', '16', '17', '18', '19', '20', '21', '22', '23', '24', '25', '26', '27', '28', '29', '30', '31', '32', '33', '34', '35']
    
    spin = random.choice(nums)
    if spin  in {"0","00"}:
       print("green {}".format(spin))
    elif spin in red:
        print("red {}".format(spin))
    else:
        print("black {}".format(spin))
    

    您还可以使用dict映射数字进行着色并选择随机密钥:

    d = {'00':"green",'0':"green",'24': 'black', '25': 'red', '26': 'black', '27': 'red', '20': 'black',
         '21': 'red', '22': 'black', '23': 'red', '28': 'black', '29': 'black', '1': 'red', '3': 'red',
         '2': 'black', '5': 'red', '4': 'black', '7': 'red', '6': 'black', '9': 'red', '8': 'black',
         '11': 'black', '10': 'black', '13': 'black', '12': 'red', '15': 'black', '14': 'red', '17':
        'black', '16': 'red', '19': 'red', '18': 'red', '31': 'black', '30': 'red', '36': 'red', '35':
        'black', '34': 'red', '33': 'black', '32': 'red'}
    
    spin = random.choice(list(d))
    print("{} {}".format(d[spin],spin))
    

    如果你想获得百分比,你可以运行n试验并使用Counter dict来计算:

    nums = list(d)
    
    from collections import Counter
    
    num_count = Counter()
    colours = Counter()
    for test in range(10000):
        spin = random.choice(nums)
        num_count[spin] += 1
        colours[d[spin]] += 1
    
    
    for k,v in colours.most_common():
        print("{} rolled {} times which is {}% of the total spin".format(k,v, v / 10000.0 * 100))
    print(" ")
    for k,v in num_count.most_common():
        print("{} rolled {} times which is {}% of the total spins".format(k,v, v / 10000.0 * 100))
    

    输出:

    black rolled 4748 times which is 47.48% of the total spin
    red rolled 4726 times which is 47.26% of the total spin
    green rolled 526 times which is 5.26% of the total spin
    
    20 rolled 301 times which is 3.01% of the total spins
    36 rolled 296 times which is 2.96% of the total spins
    23 rolled 292 times which is 2.92% of the total spins
    3 rolled 285 times which is 2.85% of the total spins
    00 rolled 284 times which is 2.84% of the total spins
    13 rolled 282 times which is 2.82% of the total spins
    12 rolled 280 times which is 2.8% of the total spins
    27 rolled 278 times which is 2.78% of the total spins
    15 rolled 273 times which is 2.73% of the total spins
    14 rolled 272 times which is 2.72% of the total spins
    6 rolled 270 times which is 2.7% of the total spins
    8 rolled 270 times which is 2.7% of the total spins
    2 rolled 268 times which is 2.68% of the total spins
    5 rolled 267 times which is 2.67% of the total spins
    35 rolled 267 times which is 2.67% of the total spins
    19 rolled 266 times which is 2.66% of the total spins
    34 rolled 265 times which is 2.65% of the total spins
    33 rolled 265 times which is 2.65% of the total spins
    21 rolled 264 times which is 2.64% of the total spins
    4 rolled 264 times which is 2.64% of the total spins
    24 rolled 263 times which is 2.63% of the total spins
    17 rolled 262 times which is 2.62% of the total spins
    31 rolled 261 times which is 2.61% of the total spins
    11 rolled 260 times which is 2.6% of the total spins
    18 rolled 257 times which is 2.57% of the total spins
    29 rolled 255 times which is 2.55% of the total spins
    30 rolled 253 times which is 2.53% of the total spins
    10 rolled 251 times which is 2.51% of the total spins
    22 rolled 248 times which is 2.48% of the total spins
    28 rolled 247 times which is 2.47% of the total spins
    1 rolled 245 times which is 2.45% of the total spins
    25 rolled 243 times which is 2.43% of the total spins
    32 rolled 243 times which is 2.43% of the total spins
    0 rolled 242 times which is 2.42% of the total spins
    9 rolled 242 times which is 2.42% of the total spins
    26 rolled 241 times which is 2.41% of the total spins
    7 rolled 240 times which is 2.4% of the total spins
    16 rolled 238 times which is 2.38% of the total spins
    

    答案 2 :(得分:0)

    基本上你可以使用'in'运算符进行搜索。

    if spin in red:
        //do for red
    elif spin in black:
        //do for black...
    else:
        //take a spin again