First of all, your dictionary is wrong, because it repeats the same keys, overwriting the previous values, so in fact it only has this:
{'NAME': 'CARLOS', 'AGE': '35', 'CITY': 'BAHIA'}
Maybe what you want is a list containing 3 dictionaries:
dados = [
{ "NAME": "LUIS", "AGE": "25", "CITY": "SAO PAULO" },
{ "NAME": "LUCAS", "AGE": "30", "CITY": "SANTA CATARINA" },
{ "NAME": "CARLOS", "AGE": "35", "CITY": "BAHIA" }
]
Now we can do the following:
import re
dados = [
{ "NAME": "LUIS", "AGE": "25", "CITY": "SAO PAULO" },
{ "NAME": "LUCAS", "AGE": "30", "CITY": "SANTA CATARINA" },
{ "NAME": "CARLOS", "AGE": "35", "CITY": "BAHIA" }
]
array = ["LUCAS", "LUIS"]
matches = []
for linha in dados:
pattern = re.compile('^{}$'.format(linha["NAME"]))
if any(x for x in array if pattern.match(x)):
matches.append(linha)
for linha in matches:
print(linha)
For each dictionary, I check if there is any element of array
which corresponds to the NAME
of this dictionary (using the function any
, returning True
if there is any element satisfying the criterion).
The exit is:
{'NAME': 'LUIS', 'AGE': '25', 'CITY': 'SAO PAULO'}
{'NAME': 'LUCAS', 'AGE': '30', 'CITY': 'SANTA CATARINA'}
If you want the search to be case insensitive (that is, that does not differentiate between upper and lower case, so the array
could have 'luis'
that the search would also find), just use the flag IGNORECASE
when creating the regex:
pattern = re.compile('^{}$'.format(linha["NAME"]), re.IGNORECASE)
Remembering that your regex uses the markers ^
and $
, which are respectively the beginning and end of the string. That is, if the name contained in the dictionary is "LUIS", the regex will be ^LUIS$
: this means that it looks exactly for the string "LUIS". If you want to search for something that contain the string "LUIS", but that may have other characters before or after, just remove the ^
and the $
.
For the case that you informed in the comments (in the dictionary has "LUIS01" and in the array has "LUIS"), there is the opposite: what has in the array that should be used in regex, and you should check if it finds a match in the contents of the dictionary:
import re
dados = [
{ "NAME": "LUIS01", "AGE": "25", "CITY": "SAO PAULO" },
{ "NAME": "LUCAS", "AGE": "30", "CITY": "SANTA CATARINA" },
{ "NAME": "CARLOS", "AGE": "35", "CITY": "BAHIA" }
]
array = ["LUCAS", "LUIS"]
matches = []
for linha in dados:
if any(x for x in array if re.match(x, linha['NAME'], re.IGNORECASE)):
matches.append(linha)
for linha in matches:
print(linha)
Exit:
{'NAME': 'LUIS01', 'AGE': '25', 'CITY': 'SAO PAULO'}
{'NAME': 'LUCAS', 'AGE': '30', 'CITY': 'SANTA CATARINA'}
The way in which your dictionary is being used is wrong, first do not use the variable name as dicit that word is part of python, that is, a reserved keyword. Another thing, the way your dictionary is structured makes there is only one line in it. If you print on it the output will be:
{'CITY': 'BAHIA', 'NAME': 'CARLOS', 'AGE': '35'}
– Esdras Xavier
Okay, if Dictionary is correct, as the search should be ?
– Luis Henrique
The search is right. The only thing you would do is turn to Lower case to avoid problems (unless you can), and then turn into an array (list in python), so each item would be a dictionary
– Esdras Xavier
Sounds more like a case of XY problem. It was mentioned that the dictionary data came from a database, so this filter should be done in the query using a clause
WHERE
... Example:WHERE NAME LIKE "%LUCAS%" OR NAME LIKE "%LUIS%"
– fernandosavio
Needs to be processed in back-end data, are many elements within the array, the @hkotsubo response solved the problem, thank you!
– Luis Henrique