【问题标题】:Set a variable to a random element from an array and not the elements value. ---python---将变量设置为数组中的随机元素,而不是元素值。 - -Python - -
【发布时间】:2016-05-27 15:09:17
【问题描述】:

这是一款基于文本的纸牌游戏。

我一直试图让 python 从数组中随机选择一个变量并将变量的名称存储在Fa 中。但是它只存储变量Fa 的值,而不是变量的名称。

我的代码:

 import random

 k='King'
 q='Queen'
 j='Jack'
 t='Ten'
 n='Nine'
 e='Eight'
 s='Seven'
 i='Six'
 f='Five'
 u='Four'
 h='Three'
 w='two'
 a='ace'
 c='of clubs'
 d='of diamonds'
 p='of spades'
 r='of hearts'
 #clubs
 A1=k+' '+c
 B1=q+' '+c
 C1=j+' '+c
 D1=t+' '+c
 E1=n+' '+c
 F1=e+' '+c
 G1=s+' '+c
 H1=i+' '+c
 I1=f+' '+c
 J1=u+' '+c
 K1=h+' '+c
 L1=w+' '+c
 M1=a+' '+c
 #diamonds
 A2=k+' '+d
 B2=q+' '+d
 C2=j+' '+d
 D2=t+' '+d
 E2=n+' '+d
 F2=e+' '+d
 G2=s+' '+d
 H2=i+' '+d
 I2=f+' '+d
 J2=u+' '+d
 K2=h+' '+d
 L2=w+' '+d
 M2=a+' '+d
 #spades
 A3=k+' '+p
 B3=q+' '+p
 C3=j+' '+p
 D3=t+' '+p
 E3=n+' '+p
 F3=e+' '+p
 G3=s+' '+p
 H3=i+' '+p
 I3=f+' '+p
 J3=u+' '+p
 K3=h+' '+p
 L3=w+' '+p
 M3=a+' '+p
 #hearts
 A=k+' '+r
 B=q+' '+r
 C=j+' '+r
 D=t+' '+r
 E=n+' '+r
 F=e+' '+r
 G=s+' '+r
 H=i+' '+r
 I=f+' '+r
 J=u+' '+r
 K=h+' '+r
 L=w+' '+r
 M=a+' '+r

 clubs=[A1,B1,C1,D1,E1,F1,G1,H1,I1,J1,K1,L1,M1]
 diamonds=[A2,B2,C2,D2,E2,F2,G2,H2,I2,J2,K2,L2,M2]
 hearts=[A3,B3,C3,D3,E3,F3,G3,H3,I3,J3,K3,L3,M3]
 spades=[A,B,C,D,E,F,G,H,I,J,K,L,M]

 CardTotal=len(clubs)

 X=''
 Da=''
 Fa=''
 sep='\n And \n'
 hand=''

 Cardhand=7

 while(Cardhand != 0):
    Cardhand=Cardhand-1
    Da=random.choice(['1', '2', '3','4'])
    if(Da=='1'):
        Fa=random.choice(clubs)
    elif(Da=='2'):
        Fa=random.choice(hearts)
    elif(Da=='3'):
        Fa=random.choice(spades)
    elif(Da=='4'):
        Fa=random.choice(diamonds)

    hand=hand+sep+Fa+sep

    if(Fa in clubs):
        clubs.pop(Fa)
    elif(Fa in diamonds):
        diamonds.pop(Fa)
    elif(Fa in hearts):
        hearts.pop(Fa)
    elif(Fa in spades):
       spades.pop(Fa)

    Da=''
    Fa=''


 cards=[clubs,diamonds,hearts,spades]    

【问题讨论】:

  • 只需替换例如random.choice(clubs)i = random.choice(range(len(clubs))。那么i 是索引,clubs[i] 是值。

标签: python arrays variable-names


【解决方案1】:

您需要意识到python在创建列表时不会传递“变量名”。

通过做:

spades=[A,B,C,D,E,F,G,H,I,J,K,L,M]

列表只包含那些变量指向的值,你可以通过打印列表来查看:

>>> spades
['King of hearts', 'Queen of hearts', 'Jack of hearts', 'Ten of hearts', 'Nine of hearts', 'Eight of hearts', 'Seven of hearts', 'Six of hearts', 'Five of hearts', 'Four of hearts', 'Three of hearts', 'two of hearts', 'ace of hearts']

如果您想获取变量名,您可以将它们存储为不带值的字符串:

spades=['A','B','C','D','E','F','G','H','I','J','K','L','M']

或具有名称和值的元组列表:

spades=[('A',A),('B',B),('C',C),('D',D),('E',E),('F',F),('G',G),('H',H),('I',I),('J',J),('K',K),('L',L),('M',M)]

然后可以使用以下方法提取值和名称:

name, value = Fa

虽然这确实更适合映射,但您将每个值与一个名称相关联,而不是通过索引检索的元素列表:

spades = {'A':A, 'B':B, 'C':C, 'D':D, 'E':E, 'F':F, 'G':G, 'H':H, 'I':I, 'J':J, 'K':K, 'L':L, 'M':M}

虽然这会使random.choice 停止工作,但您必须将其更改为:

Fa=random.choice(tuple(spades))

由于random.choice 依赖于使用索引来选择随机元素,因此它需要一个序列才能正常工作

那么就可以通过捕捉.pop的返回值来获取卡片的文字了:

card_text = spades.pop(Fa)

您可能不喜欢传递变量值而不是变量名的想法,但您会意识到它可以大大简化代码,例如,而不是这样做:

Da=random.choice(['1', '2', '3','4'])
if(Da=='1'):
    Fa=random.choice(clubs)
elif(Da=='2'):
    Fa=random.choice(hearts)
elif(Da=='3'):
    Fa=random.choice(spades)
elif(Da=='4'):
    Fa=random.choice(diamonds)

您可以为西装选择列表/字典之一,如下所示:

Da=random.choice([clubs,diamonds,hearts,spades])
#Da is now the dict for the selected suit
Fa = random.choice(tuple(Da))

那么整个while循环可能就是这样:

spades   = { 'A': A,  'B': B,  'C': C,  'D': D,  'E': E,  'F': F,  'G': G,  'H': H,  'I': I,  'J': J,  'K': K,  'L': L,  'M': M}
clubs    = {'A1':A1, 'B1':B1, 'C1':C1, 'D1':D1, 'E1':E1, 'F1':F1, 'G1':G1, 'H1':H1, 'I1':I1, 'J1':J1, 'K1':K1, 'L1':L1, 'M1':M1}
diamonds = {'A2':A2, 'B2':B2, 'C2':C2, 'D2':D2, 'E2':E2, 'F2':F2, 'G2':G2, 'H2':H2, 'I2':I2, 'J2':J2, 'K2':K2, 'L2':L2, 'M2':M2}
hearts   = {'A3':A3, 'B3':B3, 'C3':C3, 'D3':D3, 'E3':E3, 'F3':F3, 'G3':G3, 'H3':H3, 'I3':I3, 'J3':J3, 'K3':K3, 'L3':L3, 'M3':M3}
while(Cardhand != 0):
    Cardhand=Cardhand-1
    Da=random.choice([clubs,diamonds,hearts,spades])
    #Da is now the dict for the selected suit
    Fa = random.choice(tuple(Da))
    #Fa is the key in that dict, so A or B2 or similar
    card_text = Da.pop(Fa)
    hand=hand+sep+card_text+sep

【讨论】:

  • 谢谢,这帮助很大! :D
【解决方案2】:

如果我理解清楚了,那就用字典吧。

从我的示例中复制代码:

import random

dic = {'k' : 'king', 'q' : 'queen'}

dic_keys = list(dic.keys())

which = random.choice(dic_keys)

selected_v = which
selected_v_v = dic[selected_v]

print(selected_v, selected_v_v)

【讨论】:

    【解决方案3】:

    问题在于您的数据结构: clubs=[A1,B1,C1,D1,E1,F1,G1,H1,I1,J1,K1,L1,M1] 只会在数组中包含变量的值。如果您还想拥有变量名,我建议将其更改为 dict 并将 key 作为变量名。示例:

    clubs = {
      'A1': A1,
      'B1': B1,
      'C1': C1,
      'D1': D1,
      'E1': E1,
      'F1': F1,
      'G1': G1,
      'H1': H1,
      'I1': I1,
      'J1': J1,
      'K1': K1,
      'L1': L1,
      'M1': M1
    }
    

    编辑:(正如 Ishaq Khan 所建议的,对于 python3 clubs.keys() 返回一个不可迭代的 dict_keys 对象) 然后random.choice(clubs) 将更改为random.choice(list(clubs.keys()))

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-03-07
      • 1970-01-01
      • 2017-02-28
      • 1970-01-01
      • 2021-01-01
      • 2013-11-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多