A program that removes duplicate statements in Python

Introduction

Among the tasks assigned at the university this time, the following were the tasks as preparations.

① Create a database of 1000 English sentences using the Internet Make sure you don't have the same sentence here.

As expected, it is difficult to visually judge whether there are duplicate sentences when there are 1000 sentences, so it is a program after thinking about writing with python, which I am practicing recently.

program

check.py



# -*- coding: utf-8 -*-

import sys
str1 =""

try:
    file_name = sys.argv[1]
except IndexError:
    print('Usage: {0} TEXTFILE'.format(file_name))
    sys.exit(1)

with open(file_name, 'r') as f:
    #Duplicates disappear by making it a collective type (order is not guaranteed)
    # .rstrip()By doing so, you don't have to insert a line break on the last line.
    unique_texts = {line.rstrip() for line in f}

for i in unique_texts:
	str1 += i+"\n"

with open(file_name + '.disticted.txt', 'w') as f:
    f.writelines(str1)


Finally

This program compares one sentence of the text as it is, so if you do not start a new line after the last sentence, it will not be deleted when the last sentence is duplicated.

Postscript

I modified hatchinee's edit request and edited it again.

Recommended Posts

A program that removes duplicate statements in Python
I tried "a program that removes duplicate statements in Python"
When writing a program in Python
A general-purpose program that formats Linux command strings in python
[Python] A program that creates stairs with #
I made a payroll program in Python!
Write a Caesar cipher program in Python
A program that plays rock-paper-scissors using Python
[Python] A program that rounds the score
A program that determines whether a number entered in Python is a prime number
Duplicate combinations in Python
A simple Pub / Sub program note in Python
I made a Caesar cryptographic program in Python.
[Beginner] What happens if I write a program that runs in php in Python?
[Python] A program that counts the number of valleys
Write a super simple molecular dynamics program in python
Receive dictionary data from a Python program in AppleScript
Try embedding Python in a C ++ program with pybind11
What's in that variable (when running a Python script)
In Python, create a decorator that dynamically accepts arguments Create a decorator
A shell program that becomes aho in multiples of 3
I made a prime number generation program in Python 2
[Python] A program that compares the positions of kangaroos.
A Python program that converts ical data into text
MALSS, a tool that supports machine learning in Python
Take a screenshot in Python
Create a function in Python
Create a dictionary in Python
Make a bookmarklet in Python
Draw a heart in Python
A note that runs an external program in Python and parses the resulting line
A function that divides iterable into N pieces in Python
Published a library that hides character data in Python images
A program that removes specific characters from the entered text
Loop through a generator that returns a date iterator in Python
Let's create a script that registers with Ideone.com in Python.
[Python] A program that creates a two-dimensional array by combining integers
Create code that outputs "A and pretending B" in python
[MQTT / Python] Implemented a class that does MQTT Pub / Sub in Python
[Python] A program that finds the most common bird types
A Python program that aggregates time usage from icalendar data
A set of script files that do wordcloud in Python3
A program that automatically resizes the iOS app icon to the required image size in Python
Summary of points to keep in mind when writing a program that runs on Python 2.5
I made a program to collect images in tweets that I liked on twitter with Python
A Python program that collects tweets containing specific keywords daily and saves them in csv
Maybe in a python (original title: Maybe in Python)
Write a binary search in Python
[python] Manage functions in a list
Hit a command in Python (Windows)
Create a DI Container in Python
Draw a scatterplot matrix in python
ABC166 in Python A ~ C problem
Write A * (A-star) algorithm in Python
Compatibility diagnosis program written in python
Create a binary file in Python
Solve ABC036 A ~ C in Python
Write a pie chart in Python
Write a vim plugin in Python
Write a depth-first search in Python
Implementing a simple algorithm in Python 2