With my code, I would like to return the key for every value in a list of values of a dict. For the following code, I get the category
list returned which is longer than my creditaccounts
list. The desired result is:
category = ['supermarket', 'supermarket', 'unknown', 'self', 'unknown']
I struggle with the loop and the 'unknown' element.
creditaccounts = ['ALBERT HEIJN 1654 ROTTERDAM NLD', 'Jumbo 199730 TILBURG NLD','10-04-14 22:38 GELDAUTOMAAT', 'ING Bank', 'Desposit']
self = ['ING Bank', 'Van Bonusrenterekening']
bank = ['Kosten OranjePakket']
supermarket = ['ALBERT HEIJN 1654 ROTTERDAM NLD',
'Jumbo 199730 TILBURG NLD', 'Albert Heijn 1617',
'Jumbo Rotterdam ROTTERDAM NLD',
'DIRK VDBROEK FIL4014 ROTTERDAM']
dict_cat = {'self' : self
,'bank' : bank
, 'supermarket': supermarket}
category = []
for creditaccount in creditaccounts:
for cat, val in dict_cat.items():
if creditaccount in val:
category.append(cat)
else:
category.append('unknown')
category
In your inner loop you have an if-else statement and in the else you are appending "unknown" to your list. This causes "unknown" to be appended each iteration if condition does not match.
Try breaking out of the loop and an else:
creditaccounts = ['ALBERT HEIJN 1654 ROTTERDAM NLD', 'Jumbo 199730 TILBURG NLD','10-04-14 22:38 GELDAUTOMAAT', 'ING Bank', 'Desposit']
self = ['ING Bank', 'Van Bonusrenterekening']
bank = ['Kosten OranjePakket']
supermarket = ['ALBERT HEIJN 1654 ROTTERDAM NLD',
'Jumbo 199730 TILBURG NLD', 'Albert Heijn 1617',
'Jumbo Rotterdam ROTTERDAM NLD',
'DIRK VDBROEK FIL4014 ROTTERDAM']
dict_cat = {'self' : self
,'bank' : bank
, 'supermarket': supermarket}
category = []
for creditaccount in creditaccounts:
for cat, val in dict_cat.items():
if creditaccount in val:
category.append(cat)
break
else:
category.append('unknown')
creditaccounts = ['ALBERT HEIJN 1654 ROTTERDAM NLD', 'Jumbo 199730 TILBURG NLD','10-04-14 22:38 GELDAUTOMAAT', 'ING Bank', 'Desposit']
self = ['ING Bank', 'Van Bonusrenterekening']
bank = ['Kosten OranjePakket']
supermarket = ['ALBERT HEIJN 1654 ROTTERDAM NLD',
'Jumbo 199730 TILBURG NLD', 'Albert Heijn 1617',
'Jumbo Rotterdam ROTTERDAM NLD',
'DIRK VDBROEK FIL4014 ROTTERDAM']
dict_cat = {'self' : self
,'bank' : bank
, 'supermarket': supermarket}
category = []
for creditaccount in creditaccounts:
node = False
for k, v in dict_cat.items():
if creditaccount in v:
category.append(k)
node = True
if node == False:
category.append('unknown')
category
I think now it should work now,
Code Syntax
for cat in dict_cat.keys():
for creditaccount in creditaccounts:
if creditaccount in dict_cat[cat]:
category.append(cat)
else:
category.append('unknown')
Explanation
What you're looking for is to check creditaccounts
values in each list
of the dict_cat
.
If the value found append 'the dict key of this list', else append 'unknown',
Output
['unknown', 'unknown', 'unknown', 'self', 'unknown', 'unknown',
'unknown', 'unknown', 'unknown', 'unknown', 'supermarket',
'supermarket', 'unknown', 'unknown', 'unknown']
[Program finished]
According to this results,
self [list]: only element[3] found into the `creditsaccounts`.
Bank [list]: no value matched with the `creditsaccounts` list, so the whole values appeneded as 'unknown'.
supermarket [list]: its elements -> element[0], element[1].
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.