I'm trying to replace elements in a sublist with keys from a dictionary (data below).
I have a function attempting to follow this logic:
- See if element in the sublist matches any exact value in the dictionary
- If it does, append the associated dictionary key to a sublist within a new list
- If not, try to convert the element into an integer and append that to a sublist in the new list
- If that doesn't work, append the whole sublist to an entirely new list
- Return a list with sublists of integers converted from the strings in the original file and a list with sublists that can't be converted
I hope that logic makes sense. Thanks to some comments on a previous version of this question, I've edited the code so the re.search
function now works. Here's the new code below:
import re
def conversion(d, file):
list1 = []
list2 = []
for key, value in d.items():
for i in file:
for j in i:
list3 = []
good = []
if re.search(fr"\b{re.escape(j)}\b", value):
good.append(key)
list3.append(good)
else:
try:
good.append(int(j))
list3.append(good)
except:
bad = []
bad.append(i)
list1.append(list3)
list2.append(bad)
return list1, list2
However, this is not outputting what I would expect:
([[]], [[['einundzwanzig', 'vierzehn', 'eins', 'zwei', 'vier']]])
The string that could not be converted has been put into the second list dataset (although I wish it was simply a sublist within a list rather than a sublist within a sublist within a list so any help on that would be appreciated!). However, the strings that have been converted to numbers have not been inputted into the first list dataset. This is probably something to do with the placing of my lists or something along those lines but I'm really new to python and I'm not following the logic of where things should go yet.
Any help would be appreciated!
Many thanks,
Carolina
Data:
d = {0: " zero null ",
1: " one un eins ",
2: " two deux zwei ",
3: " three trois drei ",
4: " four quatre vier ",
5: " five cinq funf ",
6: " six sechs ",
7: " seven sept sieben ",
8: " eight huit acht ",
9: " nine neuf neun ",
10: " ten dix zehn ",
11: " eleven onze elf ",
12: " twelve douze zwolf ",
13: " thirteen treize dreizehn ",
14: " fourteen quatorze vierzehn ",
15: " fifteen quinze funfzehn ",
16: " sixteen seize sechzehn ",
17: " seventeen dix-sept siebzehn ",
18: " eighteen dix-huit achtzehn ",
19: " nineteen dix-neuf neunzehn ",
20: " twenty vingt zwanzig "}
file = [['16', '10', '8', '3', '7'], ['8', '9', '19', '20', '4'], ['sechs', 'acht', 'sechzehn', 'funf', 'null'], ['1', '30', '2', '5', '7'], ['vierzehn', 'eins', 'zwei', 'neun', 'drei'], ['six', 'neuf', 'seize', 'zer'], ['fourteen', 'eleven', 'forteen', 'eight', 'twenty'], ['douze', 'onze', 'huit', 'quinze', 'sept'], ['18', '9', '9', '22', '4'], ['un', 'trois', 'quatorze', 'dix-huit', 'vingt'], ['five', 'three', 'nineteen', 'twenty', 'zero'], ['einundzwanzig', 'vierzehn', 'eins', 'zwei', 'vier']]