This question already has answers here:
How to retrieve a variable's name in python at runtime?
(9 answers)
Closed 9 years ago.
I've designed a code that calculates the percentage of 'CG' appears in a string; GC_content(DNA).
Now I can use this code so that it prints the the value of the string with the highest GC-content;
print (max((GC_content(DNA1)),(GC_content(DNA2)),(GC_content(DNA3)))).
Now how would I get to print the variable name of this max GC_content?
You can get the max of some tuples:
max_content, max_name = max(
(GC_content(DNA1), "DNA1"),
(GC_content(DNA2), "DNA2"),
(GC_content(DNA3), "DNA3")
)
print(max_name)
If you have many DNA variables you could place them in a list
DNA_list = [DNA1, DNA2, DNA3]
I would coerce them into a dictionary to associate the name with the raw data and result.
DNA_dict = dict([("DNA%i" % i, {'data': DNA, 'GC': GC_Content(DNA)}) for i, DNA in enumerate(DNA_list)])
Then list comprehension to get the data you want
name = max([(DNA_dict[key]['GC'], key) for key in DNA_dict])[1]
This has the benefit of allowing variable list length
You seem to want
max([DNA1, DNA2, DNA3], key=GC_content)
It's not what you asked for but it seems to be what you need.
Related
This question already has answers here:
Getting key with maximum value in dictionary?
(29 answers)
Closed 1 year ago.
I have this dictionary:
a_dict = {"car":5,"laptop":17,"telephone":3,"photo":14}
I would like to print the key which has the highest value.
For example, I would like to print out the key laptop because it has the highest number
I have tried this so far:
def get_oldest(things):
new_list = []
for elements in things:
new_list.append(things[element])
new_list.sort()
Now, I have sorted the list from the smallest to the highest, so I know that the last item in the list has the highest value, but how do I match that to the correct key and print that.
There is a much easier way:
a_dict = {"car": 5,"laptop": 17,"telephone": 3,"photo": 14}
oldest = max(a_dict, key=a_dict.get)
# "laptop"
This uses max with a key function. You can use max on the plain dict because iterating a dict produces its keys.
This question already has answers here:
How to find the maximum number in a list using a loop?
(11 answers)
Closed 2 years ago.
by using only a temporary variable, a for loop, and an if to compare the values-hackinscience.org
Find the biggest value in a given list.
the_list = [
143266561,
1738152473,
312377936,
1027708881,
1495785517,
1858250798,
1693786723,
1871655963,
374455497,
430158267,
]
max_in = 0
for val in the_list:
if val > max_in:
max_in = val
There is the for, there is the if, max_in is somehow a temp var cause it changes over the loop. You get it.
No need for either of that. Use max(the_list).
This question already has answers here:
Finding the average of a list
(25 answers)
Find a value from a dictionary in python by taking a key from user input [closed]
(3 answers)
Closed 2 years ago.
marks={'A':[50,70,90],'B':[60,80,70],'C':[70,80,90]}
In the above dictionary, I need to access the value of B and to find the average of list (i:e:(60+80+70)/3). How could I access the value and find the average? What I tried was...
marks={'A':[50,70,90],'B':[60,80,70],'C':[70,80,90]}
get_name=input()
for i in marks:
if i==get_name:
for j in i:
add += marks[j]
print(add/3)
It shows up error. How to access the values in the dictionary of the list[60,80,70] with respect to key 'B'.
Here's a one liner -
avg = sum(marks['B'])/3
sum() will total the value in that respective list and you just have to divide it by the size of the list.
input = 'A'
average = sum(marks[input])/len(marks[input])
marks={'A':[50,70,90],'B':[60,80,70],'C':[70,80,90]}
For the above code, marks[get_name] should print out the list [60,80,70] through which the mean can be then taken.
To iterate over a dictionary you would use, for key, val in marks.items() and check if the provided user input equals to one of the key and then take the average.
This question already has answers here:
Simpler way to create dictionary of separate variables?
(27 answers)
Closed 4 years ago.
Here bkd_train, bkd_test are dataframe.
I wanted to print dataframe name along with it's shape.
for data in [bkd_train, bkd_test]:
print("{} : {}".format(?, data.shape))
If i am using "data" then it's printing dataframe.
But I want o/p like this:
bkd_train : (10886, 12)
bkd_test : (1111,11)
you cannot get the name of a variable as string, but you can pass a string in input and query locals dictionary to get the value:
for data in ["bkd_train", "bkd_test"]:
print("{} : {}".format(data,locals()[data].shape))
it works if the variables are local. If they're global, use globals, with fallback on locals, or just eval (not a problem in that context since the strings to evaluate are constant)
also read: How to get a variable name as a string?
I would iterate over the variable names themselves and then use eval to evaluate the names which will give you the dataframe.
for dataName in ['bkd_train', 'bkd_test']:
data = eval(dataName)
print("{} : {}".format(dataName, data.shape))
This question already has answers here:
What is getattr() exactly and how do I use it?
(14 answers)
How can I select a variable by (string) name?
(5 answers)
Closed 6 years ago.
I need to check variables looking like this:
if name1 != "":
(do something)
Where the number right after "name" is incremented between 1 and 10.
Do I need to write the test ten times or is there a way (without using an array or a dict) to "concatenate", so to speak, variable names?
I'm thinking about something like this:
for i in range(10):
if "name" + str(i) != "":
(do something)
Edit: I can't use a list because I'm actually trying to parse results from a Flask WTF form, where results are retrieved like this:
print(form.name1.data)
print(form.name2.data)
print(form.name3.data)
etc.
Use a list, such as:
names = ['bob', 'alice', 'john']
And then iterate on the list:
for n in names:
if n != "":
(do something)
or you could have a compounded if statement:
if (name1 != "" or name2 != "" or name3 != "")
The best solution would be to use solution #1.
If you cannot use a list or a dict, you could use eval
for i in range(10):
if eval("name" + str(i)) != "":
(do something)
First of all, your app have invalid logic. You should use list, dict or your custom obj.
You can get all variable in globals. Globals is a dict.
You can do next:
for i in range(10):
if globals().get('name%d' % i):
# do something