Удаление дублей строк
Удаляйте повторяющиеся строки из текста с настройками чувствительности и сортировки.
Часто задаваемые вопросы
Реализация кода
def deduplicate_lines(text: str, case_sensitive: bool = True) -> str:
"""Remove duplicate lines from text, preserving order of first occurrence."""
lines = text.splitlines()
seen = set()
unique_lines = []
for line in lines:
key = line if case_sensitive else line.lower()
if key not in seen:
seen.add(key)
unique_lines.append(line)
return "\n".join(unique_lines)
# Example usage
text = """apple
banana
apple
cherry
BANANA
banana"""
print(deduplicate_lines(text))
# apple
# banana
# cherry
# BANANA
print(deduplicate_lines(text, case_sensitive=False))
# apple
# banana
# cherry
Comments & Feedback
Comments are powered by Giscus. Sign in with GitHub to leave a comment.