作者IMPOSSIBLEr (I'm possible rrr)
看板Python
标题Re: [问题] DataFrame挑选特定值後的处理
时间Fri Oct 14 04:38:39 2016
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~
--
※ 发信站: 批踢踢实业坊(ptt.cc), 来自: 208.91.2.4
※ 文章网址: https://webptt.com/cn.aspx?n=bbs/Python/M.1476391122.A.308.html
※ 编辑: IMPOSSIBLEr (208.91.2.4), 10/14/2016 04:39:45
1F:→ IMPOSSIBLEr: 拍写 手机排版 完全烂掉…我回家再修 10/14 04:40
2F:推 jimmy15923: 太感谢这位高手了!完全符合我的问题~谢谢您! 10/14 10:34
3F:→ jimmy15923: 只有一个小问题,如果没vip,其他的值一样会被记录 10/14 10:36
4F:→ jimmy15923: 预设是希望没vip就不管这些值 直接为0就好 10/14 10:36
5F:推 jimmy15923: 我解决了!加个ELSE BREAK即可,再次感谢您的帮助!! 10/14 10:39