Re: [問題] DataFrame挑選特定值後的處理

作者: IMPOSSIBLEr (I'm possible rrr)   2016-10-14 04:38:39
If I understand the question correctly, this is really straightforward and can be done in 1 pass. Code as following
def vip_partitioning(arr):
before = {}
after = {}
seen_vip = False
for x in arr:
if x == 'vip':
seen_vip = True
continue
if seen_vip:
if x in after:
after[x] += 1
else:
after[x] = 1
else:
if x in before:
before[x] += 1
else:
before[x] = 1
Then you have before and after which maps the
Value in the array to its occurrences.
The idea is if the array value not in the dict,
it means the occurrence is 0.
Let me know if I am understanding this correctly.
Best~
作者: IMPOSSIBLEr (I'm possible rrr)   2016-10-14 04:40:00
拍寫 手機排版 完全爛掉…我回家再修
作者: jimmy15923 (jimmy)   2016-10-14 10:34:00
太感謝這位高手了!完全符合我的問題~謝謝您!只有一個小問題,如果沒vip,其他的值一樣會被記錄預設是希望沒vip就不管這些值 直接為0就好我解決了!加個ELSE BREAK即可,再次感謝您的幫助!!

Links booklink

Contact Us: admin [ a t ] ucptt.com