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. Here's the code below:
import re
def conversion(d, file):
list1 = []
list2 = []
for key, value in d.items():
good = []
bad = []
for i in file:
for j in i:
if re.search("\b" + j + "\b", value):
good.append(key)
else:
try:
good.append(int(j))
except:
bad.append(i)
list1.append(good)
list2.append(bad)
return list1, list2
I'm sure there are lots of issues in this code (I'm very very new to python!) but the current issue I'm getting is this error for the re.search function:
TypeError: expected string or bytes-like object
I'm not sure why it's not pulling through a string? If I write the same function outside of the for loop and if statement and specify the indeces, it seems to work.
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']]
Aucun commentaire:
Enregistrer un commentaire