-3

I need some help with a assignment for Python. The task is to make a dictionary with keys from a other dictionary and values from a csv file. All this needs to be done with a function with arguments(The other dictionary, 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

             A                         B

1.1 11
2.3 12
3.2 14
4.7 11
5.5 12

Everything in column 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).

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

Maurice Meyer
  • 17,279
  • 4
  • 30
  • 47
gmanread
  • 33
  • 7
  • Is the problem reading a column from a csv file or constructing the dictionary or printing the final output? Please ask only one question per post. – timgeb Nov 02 '18 at 13:52
  • The problem is constructing the dictionary to get the final output, with reading the column and the dictionary. – gmanread Nov 02 '18 at 14:13

2 Answers2

0

How to read a column from a csv file is already covered here.

I'm assuming that you have constructed a list colB with the values [11, 12, 14, 11, 12]. If d is your original dictionary, you can now use the dictionary comprehension

{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.

timgeb
  • 76,762
  • 20
  • 123
  • 145
  • Unfortunately i can´t construct a list (colB), i need to construct a dictionary. – gmanread Nov 02 '18 at 14:15
  • @gmanread you are supposed to construct `colB` with the `csv` module, adapting the answer in the question I linked to. You need `colB` to construct the final `result`, which is indeed a dictionary. – timgeb Nov 02 '18 at 14:57
0

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)
rich
  • 520
  • 6
  • 21
  • Thx, but how do I get the pandas module? When I try to run, File , line 1, in import pandas as pd ModuleNotFoundError: No module named 'pandas' >>> – gmanread Nov 02 '18 at 14:22
  • Ok so it means that you haven't installed that module on your pc. Go to the command prompt and type in 'pip install pandas'. Then it will install pandas on your pc. https://stackoverflow.com/questions/29817447/how-to-run-pip-commands-from-cmd – rich Nov 03 '18 at 07:40