Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/310.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 使用循环或列表创建变量。动态循环不好?_Python_List_Loops_For Loop_List Comprehension - Fatal编程技术网

Python 使用循环或列表创建变量。动态循环不好?

Python 使用循环或列表创建变量。动态循环不好?,python,list,loops,for-loop,list-comprehension,Python,List,Loops,For Loop,List Comprehension,因此,我查看了stackoverflow,觉得有人提出了非常类似的问题,但我无法根据我的问题定制这些答案(对不起!) 从其他线索中,我收集到的是 1) 动态循环很糟糕 2) 列出理解或字典可能对我有帮助吗 我想要的是什么 grant1 = median(df.query('income < 25000')['grant'] grant2 = median(df.query('income > 25000 and income < 50000')['grant'] grant3

因此,我查看了stackoverflow,觉得有人提出了非常类似的问题,但我无法根据我的问题定制这些答案(对不起!)

从其他线索中,我收集到的是 1) 动态循环很糟糕 2) 列出理解或字典可能对我有帮助吗

我想要的是什么

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= ...
grant1=中值(df.query('income<25000')['grant']
grant2=中位数(df.query('收入>25000,收入<50000')['grant']
grant3=中值(df.query('收入>50000,收入<75000')['grant']
格兰特4=。。。
格兰特5=。。。
funding1=中位数(df.query('income<25000')['funding']
funding2=中位数(df.query('收入>25000,收入<50000')['资金']
funding3=中位数(df.query('收入>50000,收入<75000')['资金']
基金4=。。。
基金5=。。。
这正是我想做的

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 = .....
source=['grant','funding']
对于源中的x:
x1=中位数(df.query('收入<25000')['x']
x2=中位数(df.query('收入>25000,收入<50000')['x']
x3=。。。。
x4=。。。。。
关于我在这里尝试做什么,有什么建议或困惑吗


谢谢!

您有一大堆要分配的变量,这些变量的名称可能如下所示:grant0、funding97、grant42、grant26、funding27

因此,不要使用单独的变量,而是将所有数据放在一个变量中存储,如下所示:

source = {'grant': [], 'funding': []'}
因此,您可以执行
source['grant'][0]
,而不是从变量
grant0
访问数据。另一个示例是,使用
source['funding'][97]
而不是使用
funding97


如果我正确理解你的问题,这可能是你想要的:

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]))
字典理解的工作原理与列表理解类似,只是有键值对


使用解释器逐步解释我的解决方案:

>>> 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

你有一大堆你想要分配的变量,其中的名字可能是这样的:grant0,funding97,grant42,grant26,funding27

因此,不要使用单独的变量,而是将所有数据放在一个变量中存储,如下所示:

source = {'grant': [], 'funding': []'}
因此,您可以执行
source['grant'][0]
,而不是从变量
grant0
访问数据。另一个示例是,使用
source['funding'][97]
而不是使用
funding97


如果我正确理解你的问题,这可能是你想要的:

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]))
字典理解的工作原理与列表理解类似,只是有键值对


使用解释器逐步解释我的解决方案:

>>> 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
“动态循环”有什么问题?从代码重用的角度来看,您的后一种方法要优雅得多:

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)
source=['grant','funding']
结果=[]
对于源中的x:
r=[]
r、 追加(中位数(df.query('收入<25000')[x])
r、 追加(中位数(df.query('收入>25000,收入<50000')[x])
r、 追加(中位数(df.query('收入>50000,收入<75000')[x])
# ...
result.append(r)
我完全不知道你想做什么。 希望这能有所帮助!

动态循环有什么问题?从代码重用的角度来看,后一种方法要优雅得多:

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)
source=['grant','funding']
结果=[]
对于源中的x:
r=[]
r、 追加(中位数(df.query('收入<25000')[x])
r、 追加(中位数(df.query('收入>25000,收入<50000')[x])
r、 追加(中位数(df.query('收入>50000,收入<75000')[x])
# ...
result.append(r)
我完全不知道你想做什么。

希望这有帮助!

虽然您从未提及过这一点(?),但看起来您正在使用
pandas
,在这种情况下,有一些更好的方法来解决此问题。尽管您从未提及这一点(?),看起来很像您正在使用的熊猫,在这种情况下,有一些更好的方法来解决此问题。嗯。感谢您的回答!这可能是一个愚蠢的问题,但我如何为列表中的每个变量分配中值(df.query(…)的值天蝎座,我感谢你的建议和确认,应该使用列表或字典。然而(我无法从前面的问题中弄清楚这一点的原因是),我不确定如何使用它们来有效地将值中位数(df.query(…)分配给每个变量(aka:grant0或grant1,等等)@As3adTintin再次查看我的答案。@As3adTintin:这里不要使用单独的变量。@Scorpion\天哪,我是python新手,所以我需要花一点时间来查看您的答案,但我会让您知道它是如何运行的,谢谢您的输入!嗯,谢谢您的回答!这可能是一个愚蠢的问题,但我该如何分配每个变量呢中位数的列表(df.query(…))蝎子天哪,我感谢你的建议和确认,应该使用列表或字典。然而(我无法从前面的问题中弄清楚这一点的原因),是我不确定如何使用它们有效地将中位数(df.query(…)分配给每个变量(又名:grant0或grant1等)@As3adTintin再看一次我的答案。@As3adTintin:这里不要使用单独的变量。@Scorpion\天哪,我是python新手,所以我需要花一点时间来查看你的答案,但我会让你知道答案的进展情况,谢谢你的输入!是的,谢谢你(如果我想不出蝎子神的答案,我可能会用这个)。许多其他帖子强烈反对动态循环。例如,这里有一篇。感谢您的帮助,这很可能是我最终使用的答案。不是循环不好,而是通过修改
globals
返回的dict创建变量不好。您只是基于
sourc创建了一个值列表e
。这会变得很难处理,因为有很多收入等级。是的,这很有效,谢谢(我会支持