Home > Enterprise >  How to print n numbers from text file containg n numbers without repeating them?
How to print n numbers from text file containg n numbers without repeating them?

Time:11-08

Lets say I have text file containing n integers :

1
1
2
3
2
4
3
1
5
6
3
5
2
6

How to program a function to print those integers without repeating them? My output should be :

1
2
3
4
5
6

Note : Output should not be a text file.
Note2 : File contains n integers.

CodePudding user response:

assuming that your integers live in a file named numbers.txt, you could do as follows:

with open('numbers.txt') as f:                                                                                                                                                                                                                                
    for number in set(f.readlines()):
        print(int(number))

CodePudding user response:

You can pass those numbers in set and they will be unique after that you can print out those numbers wherever you want.

set([1, 1, 2, 2, 3]) = {1, 2, 3}

CodePudding user response:

You have to add what you tried...pls, add to your question next time.

with open('your_file.txt', 'r') as file:
    data = file.read().replace('\n', '')

##data ##"11232.....
no_repeats ="".join(set(data))
no_repeats = int(no_repeats)
print(no_repeats)
  • Related