Reputation: 344
So i've load a pickle file an got these dictionaries here:
{1536: {'origin': u'HW', 'department': u'Kunde', 'events': [(1411562482304633L, u'new'), (1421683468875977L, u'closed')]},
{1537: {'origin': u'HW', 'department': u'Kunde', 'events': [(1411562809498852L, u'new'), (1414071035946802L, u'closed')]},
{1538: {'origin': u'HW', 'department': u'Kunde', 'events': [(1411562928759247L, u'new')]}
as you can see there is a tuple called 'events' which can contain one or more events. I want to display all of these dictionaries which contain an event named 'closed'.
i've tried it this way but it doesnt work
ticketdata = pickle.load(open("tickets.p", "rb"))
for i in ticketdata:
for j in ticketdata[i]['events']:
if 'closed' in ticketdata[i]['events']:
print i, ticketdata[i]['events']
Upvotes: 0
Views: 62
Reputation: 4449
There is no need for two for
loops:
ticketdata = pickle.load(open("tickets.p", "rb"))
for (i,j) in ticketdata.iteritems():
if 'closed' in j['events']:
print i,j['events']
Upvotes: 0
Reputation: 402
In a pythonic way, you can do this: Put all your dic in a list named lst_dic and:
dic_closed = [dic for dic in lst_dic for evt in dic.get('events',[]) if 'closeed' in evt]
Upvotes: 0
Reputation: 11585
You're checking if 'closed' is in the list of tuples, but not in the tuple itself, so it will never match. You're already iterating over the list of tuples with j
, so just check if closed
is in j
instead.
ticketdata = pickle.load(open("tickets.p", "rb"))
for i in ticketdata:
for j in ticketdata[i]['events']:
if 'closed' in j:
print i, ticketdata[i]['events']
Upvotes: 1