在python中添加元素到字典? [英] Adding element to a dictionary in python?

查看:781
本文介绍了在python中添加元素到字典?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这里比较陌生,所以请告诉我是否有什么我应该知道的东西或我在明智的方式上犯的任何错误!

I'm relatively new here, so please tell me if there is anything I should know or any mistakes I am making manner wise!

我试图通过随机选择将内容添加到字典中,但是我的代码似乎不起作用!

I am trying to add things onto a dictionary through random choice, but my code doesn't seem to work!

文件: sports.txt

The file: sports.txt

Soccer, Joshua
Lacrosse, Naome Lee
Soccer, Kat Valentine
Basketball, Huong
Tennis, Sunny
Basketball, Freddie Lacer

到目前为止我的代码:

def sportFileOpen():

    sportFile = open("sport.txt")
    readfile = sportFile.readlines()
    sportFile.close()
    return(readfile)


def sportCreateDict(sportFile):

    sportDict = {}

    for lines in sportFile:
        (sport, name) = lines.split(",")

        if sport in sportDict:
            sportDict[sport].append(name.strip())

        else:
            sportDict[sport] = [name.strip()]


    return(sportDict)



def sportRandomPick(name, sport, sportDict):


    if sport in sportDict:

        ransport = random.choice(sportDict.keys())

        sportDict[ransport].append(name)


        print(name, "has been sorted into", ransport)


def main():

    sportFile = sportFileOpen()

    sportDict = sportCreateDict(sportFile)


    name = input("Enter the name: ")

    preferredSport = input("Which sport do they want? ")

    sportRandomPick(name, preferredSport, sportDict)


main()

我正在尝试允许用户输入他们的姓名和喜欢的运动组,而他们喜欢的任何运动将比其他运动有更高的被随机选择的机会(例如,如果Jason选择足球,那么他进入足球的机会就更大了可能会加倍).

I am trying to allow a user to input their name and preferred group of sport, and whatever sport they prefer will have a higher chance of being randomly picked then the others (for example if Jason chooses soccer his chances of getting in soccer may double).

我不希望有人为我编写代码,我知道这很耗时,您还有更好的事情要做!但是,谁能给我解释一下我将如何去做呢?我知道如何做出随机选择,但我不知道如何将机会加倍".

I don't expect anyone to write code for me, I know it's time consuming and you have better things to do! But can anyone maybe explain to me how I would go about doing this? I understand how to make random choices but I don't know how I would "double" the chances.

在运行代码时,我仍然不断收到此错误:NameError: global name 'random' is not defined

Also I keep getting this error when running my code: NameError: global name 'random' is not defined

我以为我在做那个部分,但是现在我被卡住了.任何人都可以为此付出两分钱吗?

I thought I was doing that part right but now i'm stuck. Can anyone give their two cents on this?

推荐答案

尝试一下:

def sportRandomPick(name, sport, sportDict):
    if sport in sportDict:
        ransport = random.choice(list(sportDict.keys()) + [sport]) # list of sports will contain preferred sport twice.

        sportDict[ransport].append(name)

        print(name, "has been sorted into", ransport)

这将增加2项首选运动的机会.

This will increase chances of preferred sport to be picked by 2.

别忘了import random

这篇关于在python中添加元素到字典?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆