2
\$\begingroup\$

Need to write a function that takes an open file as the only parameter and returns a dictionary that maps a string to a list of strings and integers.

each line in the text will have a username, first name, last name, age, gender and an e-mail address. The function will insert each person's information into a dictionary with their username as the key, and the value being a list of [last name, first name, e-mail, age, gender].

basically what im trying to do is open a text file that contains this:

           ajones Alice Jones 44 F alice@alicejones.net

and return something like this:

          {ajones: ['Jones', 'Alice', 'alice@alicejones.net', 44, 'F']}

so far i have done this, but is there any other easier way?

def create_dict(file_name):
    '''(io.TextIOWrapper) -> dict of {str: [str, str, str, int, str]}

    '''
    newdict = {}
    list2 = []
    for line in file_name:
        while line:
            list1 = line.split() #for a key, create a list of values
    if list2(0):
        value += list1(1)
    if list2(1):
        value += list1(2)
    if list2(2):
        value += list1(3)
    if list2(3):
        value += list1(4)
    newdict[list1(0)] = list2

    for next_line in file_name: 
        list1 = line.split()
        newdict[list1(0)] = list1 
    return newdict


def helper_func(fieldname):
    '''(str) -> int
    Returns the index of the field in the value list in the dictionary
    >>> helper_func(age)
    3
    '''

    if fieldname is "lastname":
        return 0
    elif fieldname is "firstname":
        return 1
    elif fieldname is "email":
        return 2
    elif fieldname is "age":
        return 3
    elif fieldname is "gender":
        return 4
\$\endgroup\$
2
\$\begingroup\$

There are more efficient ways to implement this:

1) If you only need a dictionary to keep your data together without semantics, you could split as follows:

>> line = "ajones Alice Jones 44 F alice@alicejones.net"
>> identifier, *userdata=line.split()

results in:

>> identifier
'ajones'
>> userdata
['Alice', 'Jones', '44', 'F', 'alice@alicejones.net']

That's a simple point to start, but as mentinoed, the resulting array is very dumb, i.e. you have no semantics about what is what. You end up with a simple array.

2) Another way is using named tuples

from collections import namedtuple

[...]
Userdata = namedtuple("Userdata", "first_name last_name age gender email")
[...]
key, firstname, lastname, age, gender, email = a.split()
user=Userdata(firstname, lastname, age, gender, email)

Now your user contains the data in a meaningful way:

>> user.last_name
'Jones'

Besides: Your code looks a little bit messy. It's really hard, to get ones head around what you wrote. If you come again in 3 months i doubt, you understand anything you did above.

Your naming of variables is not only poor (list,value,newdict) but terribly misleading:

for line in file_name:

You are not iterating over lines in file_name you are iterating over lines, which are perhaps from a file; in the function (and for python) it doesn't matter what the source is, but you should choose an apropriate name.

You make your and your peers life easier, if you try to write better, i.e. cleaner code.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.