简体   繁体   English

如何使用来自其他字典的键和来自 Python 中的 csv 文件的值制作字典?

[英]How do I make a dictionary with keys from a other dictionary and values from a csv file in Python?

I need some help with a assignment for Python.我需要一些关于 Python 作业的帮助。 The task is to make a dictionary with keys from a other dictionary and values from a csv file.任务是使用来自其他字典的键和来自 csv 文件的值制作字典。 All this needs to be done with a function with arguments(The other dictionary, the_csv_file)所有这些都需要使用带参数的函数来完成(另一个字典,the_csv_file)

The other dictionary looks like this:另一本字典是这样的:

{1: 'Bob West', 2: 'Hannah North', 3: 'Bruce South', 4: 'Anna 
Bell', 5: 'George Smith'}

And i got that dictionary from a function我从一个函数中得到了那本字典

def names_dictionary():
    with open("filename.csv", 'r') as d:
        x = {num+1:name.split(" ",1)[-1].strip() for (num, name) 
        in enumerate(d)}
    print(x)

From this dictionary i need the values(the names) to be keys in the new dictionary.从这本字典中,我需要将值(名称)作为新字典中的键。

The CSV file looks like this in excel CSV 文件在 excel 中如下所示

             A                         B

1.1 11
2.3 12
3.2 14
4.7 11
5.5 12

Everything in column A. A 列中的所有内容。

From here i want the second number in every row to be the value in the new dictionary.从这里开始,我希望每一行中的第二个数字成为新字典中的值。 So (11, 12, 14, 11, 12).所以 (11, 12, 14, 11, 12)。

The result dictionary I want is我想要的结果字典是

{’Bob West’: 11, ’Hannah North’: 12, ’Bruce South’: 14, ’Anna 
Bell’: 11, ’George Smith’: 12}

The function so far.功能到此为止。

def names(names_dictionary, csvfile):

And for the end i need a main function who puts together and output:最后我需要一个主函数来组合和输出:

Bob West got 11 bananas Hannah North got 12 bananas Bruce South got 14 bananas Anna bell got 11 bananas George Smith got 12 bananas Bob West 得到 11 个香蕉 Hannah North 得到 12 个香蕉 Bruce South 得到 14 个香蕉 Anna bell 得到 11 个香蕉 George Smith 得到 12 个香蕉

How to read a column from a csv file is already covered here . 此处已经介绍如何从 csv 文件中读取列。

I'm assuming that you have constructed a list colB with the values [11, 12, 14, 11, 12] .我假设您已经构建了一个值为[11, 12, 14, 11, 12]的列表colB If d is your original dictionary, you can now use the dictionary comprehension如果d是您的原始字典,您现在可以使用字典理解

{v:colB[k - 1] for k, v in d.items()}

to construct the result.来构造结果。

Demo:演示:

>>> d = {1: 'Bob West', 2: 'Hannah North', 3: 'Bruce South', 4: 'Anna Bell', 5: 'George Smith'}
>>> colB = [11, 12, 14, 11, 12]
>>> result = {v:colB[k - 1] for k, v in d.items()}
>>> result
{'Bob West': 11, 'Hannah North': 12, 'Bruce South': 14, 'Anna Bell': 11, 'George Smith': 12}

I'm sure you'll figure out the printing yourself.我相信你会自己弄清楚印刷的。

not sure if you want one or two functions in the end.不确定您最终是否想要一两个功能。 Here I have left them as two separate functions.在这里,我将它们作为两个独立的函数。

import pandas as pd

#read in the file
csvfile = pd.read_csv(r'C:\Users\wrich\Desktop\data.csv')

#import the other dictionary
names_dictionary = {1: 'Bob West', 2: 'Hannah North', 3: 'Bruce South', 4: 'Anna Bell', 5: 'George Smith'}


def names(names_dictionary, csvfile):
    #manipulate the file to get the values you want
    csvfile = pd.DataFrame(csvfile['   A                        '].str.split(' ',1).tolist(), columns = ['id','value'])
    myValues = list(csvfile['value'])

    #output the result
    result = {}
    for entry in range(len(myValues)):
        result[names_dictionary[entry + 1]] = myValues[entry]

    return result

result = names(names_dictionary, csvfile)

def mainFunction(result):

    myString = ''

    for val in result.keys():

        myString += val + ' got ' + str(result[val]) + ' bananas '

    return myString

answer = mainFunction(result)

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

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