[英]Creating variables using loops or lists. Dynamic Loops are bad?
So i've looked through stackoverflow and feel that very similar questions have been asked, however I wasn't able to quite tailor those answers to my problem (sorry!). 因此,我查看了stackoverflow并感到提出了非常类似的问题,但是我无法完全针对我的问题量身定制这些答案(对不起!)。
From other threads, what I have gathered is that 1) dynamic loops are bad 2) list comprehension or dictionaries might help me? 从其他线程来看,我得到的是1)动态循环不好2)列表理解或字典可能对我有帮助?
What I want to end up with 我想最后得到什么
grant1 = median(df.query('income < 25000')['grant']
grant2 = median(df.query('income > 25000 and income < 50000')['grant']
grant3 = median(df.query('income > 50000 and income < 75000')['grant']
grant4 = ...
grant5 = ...
funding1 = median(df.query('income < 25000')['funding']
funding2 = median(df.query('income > 25000 and income < 50000')['funding']
funding3 = median(df.query('income > 50000 and income < 75000')['funding']
funding4 = ...
funding5= ...
Sort of what I want to do 我想做什么
source = ['grant', 'funding']
for x in source:
x1 = median(df.query('income < 25000')['x']
x2 = median(df.query('income > 25000 and income < 50000')['x']
x3 = ....
x4 = .....
Any suggestions, or confusions regarding what I am trying to do here? 关于我要在这里做什么的任何建议或困惑?
Thanks! 谢谢!
You have a bunch a variables that you want to assign to, where the names might look like these: grant0, funding97, grant42, grant26, funding27 您有一堆要分配的变量,其名称可能类似于以下内容:Grant0,funding97,grant42,grant26,funding27
So instead of having seperate variables, put the data all together into one variable that stores them like this: 因此,与其使用单独的变量,不如将所有数据放到一个像这样存储它们的变量中:
source = {'grant': [], 'funding': []'}
So instead of accessing the data from the variable grant0
, you can do source['grant'][0]
. 因此,您可以执行source['grant'][0]
来代替从变量grant0
访问数据。 As another example, instead of using funding97
, use source['funding'][97]
再举一个例子,使用source['funding'][97]
代替使用funding97
If I understand your question correctly, this may be what you want: 如果我正确理解您的问题,则可能是您想要的:
income_brackets = [(0, 25000), (25000, 50000)]
source = {'grant': [], 'funding': []}
#Hard to understand one-liner
#source = {key: [median(df.query('income > {} and income < {}'.format(lower, upper))[key]) for lower, upper in income_brackets] for key in source}
#Easy to understand three-liner
for lower, upper in income_brackets:
for key in source:
source[key].append(median(df.query('income > {} and income < {}'.format(lower, upper))[key]))
How list comprehensions work: 列表推导如何工作:
>>> foo = []
>>> for i in range(10):
foo.append(i)
>>> foo
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> bar = [i for i in range(10)]
>>> bar
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
Dictionary comprehensions work similar to list comprehensions, except you have key value pairs. 字典推导的工作方式类似于列表推导,但您拥有键值对。
Step-by-step explanation of my solution using the interpreter: 使用解释程序逐步解决我的问题:
>>> income_brackets = [(0, 25000), (25000, 50000)]
>>> source = {'grant': [], 'funding': []}
>>> for lower, upper in income_brackets:
print(lower, upper)
0 25000
25000 50000
>>> for lower, upper in income_brackets:
for key in source:
print(lower, upper, key)
0 25000 grant
0 25000 funding
25000 50000 grant
25000 50000 funding
>>> for lower, upper in income_brackets:
for key in source:
source[key].append(5)
>>> source
{'grant': [5, 5], 'funding': [5, 5]}
>>> source['grant'][0]
5
>>> source['grant'][1]
5
>>> source['funding'][0]
5
>>> source['funding'][1]
5
What's wrong with "dynamic loops"? “动态循环”有什么问题? From the point of view of code reuse, your latter approach is much more elegant: 从代码重用的角度来看,您的后一种方法更加优雅:
source = ['grant', 'funding']
result = []
for x in source:
r = []
r.append(median(df.query('income < 25000')[x])
r.append(median(df.query('income > 25000 and income < 50000')[x])
r.append(median(df.query('income > 50000 and income < 75000')[x])
# ...
result.append(r)
I am not altogether sure what you are attempting to do. 我完全不确定您要做什么。 Hope this helps! 希望这可以帮助!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.