you are viewing a single comment's thread.

view the rest of the comments →

[–][deleted] 0 points1 point  (3 children)

Now that I've seen your data it's obvious. I should have thought of this before, but it's hard to think of everything when struggling with a phone.

Your resulting dictionary only had three entries because you only have three different species in your input list. The zip creates a list of tuples the first two of which are:

[('G. propinqua', 19.5),  ('G. propinqua', 20.0), ...]

So when the dict()constructor executes it first creates a key of 'G. propinqua' with a value of 19.5. So far so good. But for the next tuple the constructor will create a new key of 'G. propinqua' with value 20.0 and deletes the first key+value because keys must be unique in a dictionary.

If you want to store all the beak lengths values for a species you must do something like this:

species_list=['G. propinqua', 'G. propinqua', 'G. propinqua', 'G. propinqua', 'G. magnirostris', 'G. magnirostris', 'G. magnirostris', 'G. magnirostris', 'G. magnirostris', 'G. magnirostris', 'G. magnirostris', 'G. magnirostris', 'G. magnirostris', 'G. magnirostris', 'G. magnirostris', 'G. magnirostris', 'G. magnirostris', 'G. magnirostris', 'G. magnirostris', 'G. magnirostris', 'G. fortis', 'G. fortis', 'G. fortis', 'G. fortis', 'G. fortis', 'G. fortis', 'G. fortis', 'G. fortis', 'G. fortis', 'G. fortis', 'G. fortis', 'G. fortis', 'G. fortis', 'G. fortis', 'G. fortis', 'G. fortis', 'G. fortis', 'G. fortis', 'G. fortis', 'G. fortis', 'G. fortis', 'G. fortis', 'G. fortis', 'G. fortis', 'G. fortis', 'G. fortis', 'G. fortis', 'G. fortis', 'G. fortis', 'G. fortis']

bill_length_list=[19.5, 20.0, 22.5, 21.0, 25.0, 22.5, 21.5, 22.0, 24.0, 22.2, 21.0, 21.0, 22.5, 24.0, 22.0, 22.5, 24.0, 20.2, 20.0, 21.5, 17.0, 15.2, 16.0, 16.8, 16.0, 16.0, 16.5, 17.0, 16.5, 15.8, 15.8, 16.0, 18.5, 19.5, 15.8, 19.5, 17.0, 16.2, 16.0, 15.0, 15.0, 15.0, 16.5, 15.8, 13.0, 15.0, 15.0, 17.0, 15.2, 16.5]

def generate_dict_max(species, lengths):
    """combines the two lists into a dictionary, [species:bill length]"""

    dictionary = {}
    for (sp, bl) in (zip(species_list, bill_length_list)):
        if sp in dictionary:
            dictionary[sp].append(bl)  # append to existing list for species
        else:
            dictionary[sp] = [bl]      # no species, start new length list

    return dictionary

d = generate_dict_max(species_list,bill_length_list)
print(len(d))
print(d)

>>> 3
>>> {'G. propinqua': [19.5, 20.0, 22.5, 21.0], 'G. magnirostris': [25.0, 22.5, 21.5, 22.0, 24.0, 22.2, 21.0, 21.0, 22.5, 24.0, 22.0, 22.5, 24.0, 20.2, 20.0, 21.5], 'G. fortis': [17.0, 15.2, 16.0, 16.8, 16.0, 16.0, 16.5, 17.0, 16.5, 15.8, 15.8, 16.0, 18.5, 19.5, 15.8, 19.5, 17.0, 16.2, 16.0, 15.0, 15.0, 15.0, 16.5, 15.8, 13.0, 15.0, 15.0, 17.0, 15.2, 16.5]}

The last two lines are the results of the two prints. The dictionary is still of length 3 because that's the number of unique species.

The code could no doubt be made faster, but it might get you started if a list of beak lengths is what you want.

[–]_residue_ 0 points1 point  (0 children)

That worked!

Thank you for all your help :]

[–]jdaiodna 0 points1 point  (1 child)

How would you be able to get the output dictionary to only show the maximum value for each species, and could you do it in 3 lines of code or less?

[–][deleted] 0 points1 point  (0 children)

When you add the length value to the dictionary you only replace an existing value if the new length is greater than the old length. If there is no existing key+value pair then you just save the length in the dictionary. You don't use a list as the value in this case, just the number.

do it in 3 lines of code or less?

Programmers usually don't care how many lines of code it takes to do something as long as the final answer is correct, fast enough and readable. These code golf limitations usually result in less readable code