检查列表以查看其值是否与另一个列表匹配

时间:2019-06-03 18:33:00

标签: python list

我正在创建一个垄断游戏,在这里,我有一个名为属性的列表,其中包含所有属性名称:

property = ["Go", "Mediterranean Ave", "Community Chest", "Baltic Ave", "Income Tax", "Reading Railroad",
        "Oriental Ave", "Chance", "Vermont Ave", "Connecticut Ave", "jail/Just Visiting", "St. Charles Place",
        "Electric Company", "States Ave", "Virginia Ave", "Pennsylvania Railroad", "St. James Place",
        "Community Chest", "Tennessee Ave", "New York Ave", "Free Parking", "Kentucky Ave", "Chance",
        "Indiana Ave", "Illinois Ave", "B. & O. Railroad", "Atlantic Ave", "Ventnor Ave", "Water Works",
        "Marvin Gardens", "Go to Jail", "Pacific Ave", "North Carolina Ave", "Community Chest",
        "Pennsylvania Ave", "Short Line Railroad", "Chance", "Park Place", "Luxury Tax", "Boardwalk"]

我还有一个颜色列表,用于标记属性颜色:

propertyColor = [
"None", "Brown", "None", "Brown", "None", "None", "Navy", "None", "Navy", "Navy", "None", "Pink", "None", "Pink", "Pink", "None", "Orange", 
"None", "Orange", "Orange", "None", "Red", "None", "Red", "Red", "None", "Yellow", "Yellow", "None", "Yellow", "None", "Green", "Green", "None",
"Green", "None", "None", "Blue", "None", "Blue"    

]

每个玩家的名字都有一个字典

        {
         "playerName": name,
         "money": settings[4],
         "properties": ["Mediterranean Ave", "Baltic Ave"],
         "railroads": [],
         "inJail":   False,
         "PlayerLocation": 0
         }

我想这样做,以便如果用户想在某个属性上建造公寓,该程序将检查用户是否在相同的颜色组中拥有所有三个属性,它将继续执行下一个命令,会做到这一点。

1 个答案:

答案 0 :(得分:0)

代码

user_properties = ["Mediterranean Ave", "Baltic Ave"]
from collections import Counter
repeat = [propertyColor[property.index(x)] for x in user_properties]
repeat_dict = Counter(repeat)

输出

Counter({'Brown': 2})

然后检查是否有任何颜色具有3个计数。

{v: k for k, v in repeat_dict.items()}
{2: 'Brown'}