Iterating through a dictionary, and getting max, min, and average values from the value
Question:
For the get_gc_stats( ) function:
The first value in the returned list will be the minimum GC value in the dictionary
The second value in the returned list will be the maximum GC value in the dictionary
The third value in the returned list will be the average GC value from the dictionary
Assuming the dictionary is:
dna_dict = {
'TAGC' : [0.5, 4],
'ACGTATGC' : [0.5, 8],
'ATG' : [0.3333333333333333, 3],
'ACGGCTAG' : [0.625, 8]
}
I am going through this problem and I am getting confused on what to do. I think I need to go through each value in the dictionary and get the max, min, and average of the number that is in index 0. However I need more guidance.
This is my code
def get_gc_stats(dna_dict):
for key, value in dna_dict.items():
for i in value[0]:
Answers:
You can use make a long list of all the "GC" values and then the use min()
, max()
, and divide the sum of all values by the length of the list to get the values you would like.
dna_stats = {
'TAGC' : [0.5, 4],
'ACGTATGC' : [0.5, 8],
'ATG' : [0.3333333333333333, 3],
'ACGGCTAG' : [0.625, 8]
}
def get_gc_stats(dna_dict):
gc_values = []
for value in dna_dict.values():
gc_values.extend(value)
return [min(gc_values), max(gc_values), sum(gc_values) / len(gc_values)]
print(get_gc_stats(dna_stats))
dna_stats = {
'TAGC' : [0.5, 4],
'ACGTATGC' : [0.5, 8],
'ATG' : [0.3333333333333333, 3],
'ACGGCTAG' : [0.625, 8]
}
If you need to collect all of the first values, you can use a list comprehension.
first_vals = [v[0] for v in dna_stats.values()]
# [0.5, 0.5, 0.3333333333333333, 0.625]
Now getting the min, max and average are straightforward.
For the get_gc_stats( ) function:
The first value in the returned list will be the minimum GC value in the dictionary
The second value in the returned list will be the maximum GC value in the dictionary
The third value in the returned list will be the average GC value from the dictionary
Assuming the dictionary is:
dna_dict = {
'TAGC' : [0.5, 4],
'ACGTATGC' : [0.5, 8],
'ATG' : [0.3333333333333333, 3],
'ACGGCTAG' : [0.625, 8]
}
I am going through this problem and I am getting confused on what to do. I think I need to go through each value in the dictionary and get the max, min, and average of the number that is in index 0. However I need more guidance.
This is my code
def get_gc_stats(dna_dict):
for key, value in dna_dict.items():
for i in value[0]:
You can use make a long list of all the "GC" values and then the use min()
, max()
, and divide the sum of all values by the length of the list to get the values you would like.
dna_stats = {
'TAGC' : [0.5, 4],
'ACGTATGC' : [0.5, 8],
'ATG' : [0.3333333333333333, 3],
'ACGGCTAG' : [0.625, 8]
}
def get_gc_stats(dna_dict):
gc_values = []
for value in dna_dict.values():
gc_values.extend(value)
return [min(gc_values), max(gc_values), sum(gc_values) / len(gc_values)]
print(get_gc_stats(dna_stats))
dna_stats = {
'TAGC' : [0.5, 4],
'ACGTATGC' : [0.5, 8],
'ATG' : [0.3333333333333333, 3],
'ACGGCTAG' : [0.625, 8]
}
If you need to collect all of the first values, you can use a list comprehension.
first_vals = [v[0] for v in dna_stats.values()]
# [0.5, 0.5, 0.3333333333333333, 0.625]
Now getting the min, max and average are straightforward.