Could you tell me that Python Dictionary will return the key value that meets the conditions?

Asked 2 years ago, Updated 2 years ago, 17 views

Can we find a value that meets the condition and return the corresponding key value to make it a list?

For example,

listA = year = [1950,1951,...,2018]
listB = index = [value1,value2,...,value69]

I made dictionary data so that year is key and index is value by combining these two lists.

dic = {1950:value1, 1951:value2, ..., 2018:value69} If the value is greater than 1, can the corresponding key value be returned?

For example, if the two values are 1, 1.2 and the rest are all less than 1
Can I get [1951, 2017] in the form of a list of 1951, 2017 corresponding to these two keys?

I'd appreciate your help.

python

2022-09-20 18:58

1 Answers

l = [ k for k, v in dic.items() if v >=1 ] 
import pandas as pd

df = pd.DataFrame({ "year":listA, "value":listB })
df[df.value >= 1].year


2022-09-20 18:58

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.