There is an array of movies, with tuples in it:
films = [(film_name,film_rating),...]
How do I find a film name which has the same name, but the number 2 added to it (like a part 2)?
There is an array of movies, with tuples in it:
films = [(film_name,film_rating),...]
How do I find a film name which has the same name, but the number 2 added to it (like a part 2)?
Consider utilizing str.startswith
:
>>> films = [('Kill Bill: Vol. 1', 8.2), ('Pulp Fiction', 8.9), ('Kill Bill: Vol. 2', 8.0)]
>>> kill_bill_films = [(film, rating) for (film, rating) in films if film.startswith('Kill Bill')]
>>> kill_bill_films
[('Kill Bill: Vol. 1', 8.2), ('Kill Bill: Vol. 2', 8.0)]
Or str.endswith
:
>>> films_that_end_with_2 = [(film, rating) for (film, rating) in films if film.endswith('2')]
>>> films_that_end_with_2
[('Kill Bill: Vol. 2', 8.0)]
You give very little information but if you want a movie that contains "part 2" in it's name, then the below code should be fine:
films = [("start wars 1",9.0 ), ("star wars part 2",10.0 )]
filmSet = []
for tple in films:
if "part 2" in tple[0]:
filmSet.append(tple)
print(filmSet)