Как избежать дублирования слов в массиве [закрыто] ⇐ Python

Программы на Python
Anonymous
Как избежать дублирования слов в массиве [закрыто]

Сообщение Anonymous »

Как избежать дублирования слов в массиве. В следующем сценарии
Я хочу, чтобы Фил присутствовал в выводе.
Но я хочу, чтобы Филлис была удалена из вывода р>
# Initial array
football_players = ["New York Mets", "Philadelphia Phillies", "Phil"]
# football_players = ["New York Mets", "Philadelphia Phillies", "Phillies"]

# Function to remove smaller words contained in larger words
def remove_subwords(players):
result = []

for player in players:
# Check if the player name is a substring of any other name in the list
if not any(player in other and player != other for other in players):
result.append(player)

return result

# Call the function and print the result
filtered_players = remove_subwords(football_players)
print(filtered_players)


Подробнее здесь: https://stackoverflow.com/questions/790 ... n-an-array

Вернуться в «Python»