我的字典看起来像这样:
select
'Actuals' as fc_version,
A.tra_fiscal_year as fiscal_year,
A.tra_fiscal_week as fiscal_week,
A.tra_allocation_category_code as category,
A.tra_grand_pricing_sales_channel as grandparent_channel,
D.fc_prod_id,
coalesce(
case
when A.tra_ticket_product_code like 'A%' then 'Total'
else null
end,
case
when A.tra_grand_pricing_sales_channel = 'INMKT' Then 'In Market'
else 'All Other'
end )
as fc_sales_channel,
sum(A.tra_ticket_quantity) as units_sold,
from ((pd_plan_forecast_db.adm_rev_detail A
left outer join
pd_plan_forecast_db.adm_rev_prod_code_to_fc_id B
on (A.tra_ticket_product_code = B.product_code))
left outer join
pd_plan_forecast_db.adm_rev_ticket_code_to_fc_id C
on (A.tra_ticket_code = C.ticket_code)
left outer join
pd_plan_forecast_db.adm_rev_fc_prod_info D
on (coalesce(B.fc_prod_id, C.fc_prod_id) = D.fc_prod_id))
group by 1, 2, 3, 4, 5, 6, 7
union
select fc_version, fiscal_year, fiscal_week, category, null as grandparent_channel, fc_prod_id, fc_sales_channel, sum(units_sold) as units_sold
from pd_plan_forecast_db.adm_rev_fc
where fiscal_year = 2017 and fiscal_week = 1
group by 1,2,3,4,5,6,7
我希望在{'0': 'correct g',
'1': 'correct g',
'2': 'incorrect ng',
'3': 'correct g',
'4': 'correct g',
'5': 'incorrect g',
'6': 'incorrect ng'}
之后识别等于incorrect ng
的连续值,并返回相应的键。
所以输出看起来像......
correct g
其中2是{2: [3,4]}
的键,值列表是连续键,其值等于incorrect ng
识别我拥有的correct g
:
incorrect ng
但我不知道自己如何能够继续下去这些值,直到我再次点击{k:v for k,v in dictionary.items() if v == 'incorrect ng'}
。有什么建议吗?
答案 0 :(得分:0)
newDict = {}
Arr = []
num = -1
start = False
for key in dictionary:
value = dictionary[key]
if value == 'incorrect ng' and start == False:
num = int(key)
start = True
elif value == 'incorrect ng' and start == True:
start = False
elif value == 'correct g' and start == True:
Arr.append(key)
if num != -1:
newDict[int(num)] = Arr
print(newDict)
答案 1 :(得分:-1)
假设每个项目都有一个数字键,并且没有间隙,你可以像这样迭代这些项目:
answer = {}
for key in range(len(dictionary)):
if dictionary[key] == 'incorrect ng':
answer[key] = []
for next_key in range(key, len(dictionary)):
if dictionary[next_key] == 'correct g':
answer[key].append(next_key)
else:
break
当您获得第一个'incorrect ng'
时,只需跟踪其键值,然后查看下几个条目,直到您点击不是'correct g'
的条目。
答案 2 :(得分:-1)
请注意,默认情况下,python 2.7中的字典不保留顺序。因此,请使用OrderedDict
来保留订单,或者只使用list
from collections import OrderedDict, defaultdict
values = OrderedDict()
values['0'] = 'correct g'
values['1'] = 'correct g'
values['2'] = 'incorrect ng'
values['3'] = 'correct g'
values['4'] = 'correct g'
values['5'] = 'incorrect g'
values['6'] = 'incorrect ng'
results = defaultdict(list)
new_key = None
for key in values:
# when we encounter 'incorrect ng' we will set it as new_key.
if values[key] == 'incorrect ng':
new_key = key
elif values[key] == 'correct g' and new_key:
# if new_key is present and current value is 'correct g'
results[new_key].append(key)
else:
# any other key value comes in between then we clear the new_key
# remove this piece of code if intermediate values could be 'correct ng' or other things.
new_key = None
results
输出:
defaultdict(list, {'2': ['3', '4']})