I want to create a function that I pass an argument to and that searches a csv file for strings that match the argument. The file has the following form:
Rank,Name,Platform,Year,Genre,Publisher,NA_Sales,EU_Sales,JP_Sales,Other_Sales,Global_Sales
1,Wii Sports,Wii,2006,Sports,Nintendo,41.49,29.02,3.77,8.46,82.74
2,Super Mario Bros.,NES,1985,Platform,Nintendo,29.08,3.58,6.81,0.77,40.24
3,Mario Kart Wii,Wii,2008,Racing,Nintendo,15.85,12.88,3.79,3.31,35.82
4,Wii Sports Resort,Wii,2009,Sports,Nintendo,15.75,11.01,3.28,2.96,33
5,Pokemon Red/Pokemon Blue,GB,1996,Role-Playing,Nintendo,11.27,8.89,10.22,1,31.37
6,Pokemon Diamond/Pokemon Pearl,DS,2006,Role-Playing,Nintendo,6.42,4.52,6.04,1.37,18.36
7,Grand Theft Auto V,PS3,2013,Action,Take-Two Interactive,7.01,9.27,0.97,4.14,21.4
The function I have tried to create would be the following:
def filtra_por_saga(juegos, saga):
result = {i.name for i in juegos if i.name.split()[0] == saga}
return result
Where games is the read file and saga would be the string to filter on. I work with namedtuple and the function should return the full name of the game if any part of it matches the given argument. Ex: If saga is "Grand theft autos", the function must return all the names of videogames that contain that part in it ("Grand theft autos IV","Grand theft autos V",etc). The problem with the function I've created is that it only works with non-composite arguments; ex: (saga = "Pokemon", returns "Pokemon Red/Pokemon Blue", etc).
The function to test is the following:
def test_filtra_por_saga(juegos, saga):
print(filtra_por_saga(juegos, saga))
test_filtra_por_saga(juegos, "Pokemon")
The comparison string will vary through 'test_filter_by_saga(games, " ")'. Thanks in advance.
in your role
you are verifying that, after dividing the field
.name
by spaces, if you keep the first element it must match the parametersaga
. Therefore it will only accept matches if the first word is exactly the one supplied. But ifsaga
it is true, for example"Grand theft autos"
, there will never be any matches since the first word of the name can never be"Grand theft autos"
(if anything, it could be"Grand"
.Perhaps more similar to what you were trying would be this:
Where we don't split
.name
to get the first word, but instead check to see if it starts with the string specified insaga
.Still, I don't think this meets the spec so what if
saga
it ok"Auto"
? Shouldn't it also return"Grand Theft Auto V"
, etc.? I mean, I think as long as the word insaga
appears anywhere in the name, it should return it.With this change I would do:
Finally a couple of details:
.lower()
to capitalize as muchsaga
as the name before checking if it's contained.set()
) that is a kind of list in which there can be no repeated data. I don't think that's what you were looking for, since in each iteration you're getting a different name anyway, repetitions weren't going to happen anyway. I would change the curly braces to square brackets to return a normal list instead of a set.Applying both changes: