简体   繁体   English

如何打印项目以及它来自Python的列表?

[英]How do I print an item and the list it came from in Python?

I am trying to create a roulette simulator and currently the program only prints the number. 我正在尝试创建轮盘赌模拟器,当前该程序仅打印号码。 How do I get it to print the number and the list title it is in? 如何获得打印号码及其所在列表标题的信息?

Example: 例:

  • 12 Red 12红
  • 22 Black 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)
    
    if spin in red:
        print('{} Red'.format(spin))
    elif spin in black:
        print('{} Black'.format(spin))
    else:
        print('{}'.format(spin))
    

    You only need check two conditions, either it is 0 or in one of red or black, if it is not 0 and not in red in must be it black 您只需要检查两个条件,要么为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"))
    

    Which written out explicitly becomes: 明确写出的内容变为:

    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))
    

    The black list of numbers is irrelevant. 数字黑名单无关紧要。

    If you want to represent 00 you will need to use strings instead of integers as a leading 0 means octal in python 2 and is invalid syntax in python3 如果要表示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))
    

    you could also use a dict mapping numbers to colour and picking a random key: 您还可以使用字典映射数字为颜色着色并选择一个随机键:

    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))
    

    If you want to get the percentages you can run n trials and use a Counter dict to keep count: 如果要获取百分比,则可以运行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))
    

    Output: 输出:

    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
    

    Basically you can search using 'in' operator. 基本上,您可以使用“ in”运算符进行搜索。

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

    声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

     
    粤ICP备18138465号  © 2020-2024 STACKOOM.COM