2014-04-08 57 views
0

所以我查看了一下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= ... 

排序的我想要做什麼方面,我想在這裏做

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

任何建議或混亂結束了?

謝謝!

+1

儘管你從未提到過這個(?),它看起來很像你使用的「pandas」,在這種情況下,有一些更好的方法來解決這個問題。 – DSM

回答

3

你有一堆要分配到,這裏的名字看起來像這樣一個變量:grant0,funding97,grant42,grant26,funding27

因此,而不是具有單獨的變量,把數據放在一起爲一個變量存儲他們這樣的:

source = {'grant': [], 'funding': []'} 

因此,而不是從變量grant0訪問數據,你可以做source['grant'][0]。再舉一個例子,而不是使用funding97,使用source['funding'][97]


如果我正確理解你的問題,這可能是你想要什麼:

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

如何list解析的工作:

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

字典理解工作類似於列表comprehensio ns,除了你有鍵值對。


步驟一步解釋使用解釋我的解決方案:

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

嗯。感謝您的迴應!這可能是一個愚蠢的問題,但我怎麼會然後分配列表中的每個變量中值(df.query(...)) – As3adTintin

+0

Scorpion_God,我感謝您的建議,並確認列表或詞典應該使用。然而(我之所以無法從以前的問題中得出這個結論)是因爲我不確定如何使用它們來有效地將中位數(df.query(...)) 賦值給每個變量(又名:grant0或grant1等) – As3adTintin

+0

@ As3adTintin再次看看我的答案。 –

2

出了什麼問題「的動態循環」?但從代碼重用的時候,你的後一種方法更優雅:

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) 

我並不完全相信你正在試圖做的事。 希望這有助於!

+0

是的,確實有用,謝謝(如果我找不出scorpion_god的答案,我可能會使用它)。許多其他帖子強烈阻止動態循環。這裏有一個,例如[鏈接](http://stupidpythonideas.blogspot.com/2013/05/why-you-dont-want-to-dynamically-create.html)。 感謝您的幫助,它可能是我最終使用的答案。 – As3adTintin

+1

這不是循環的壞處,它是通過修改'globals'返回的字典來創建一個變量,這是不好的。你只是簡單地創建一個基於'source'的值列表。 – chepner

+1

這變得很難處理,收入很多。 –