Hi, I have text file where I have these lines with numbers:

e, 1, 2
e, 1, 3
e, 2, 1
e, 3, 4
etc.

I need remove similar lines, in this case line e, 2, 1. Is it possible?

Recommended Answers

All 5 Replies

Please post the code you have tried so far, to be used as a starting point. The question can not be answered until we know how "e, 2, 1" is similar

Hi, I am not using Python but I have script in python:

part of script

`elif line.find("CONECT") > -1:
                con = line.split()
                line_value = line_value + 1
                #print line_value
                #print con[2]
                try:
                        line_j = "e" + ', ' + str(line_value) + ', ' + con[2] + "\n"
                        output_file.write(line_j)
                        print(line_j)
                        line_i = "e" + ', ' + str(line_value) + ', ' + con[3] + "\n"
                        output_file.write(line_i)
                        print(line_i)
                        line_k = "e"+ ', ' + str(line_value) + ', ' + con[4] + "\n"
                        print(line_k)
                        output_file.write(line_k)
                except IndexError:
                        continue` 

which give .txt output in format

e, 1, 2

e, 1, 3

e, 1, 4

e, 2, 1

e, 2, 3

etc.

I need remove similar lines with the same numbers, but no matter on order this numbers

i.e. line e, 2, 1..

Is it possible? Please help me

This may get you started ...

# removeSimilarItems.py #  # 2015-08-29 #

lst = [ ['e', 1, 2], ['e', 2, 1], ['e', 1, 2], ['e', 2, 3] ]

lst2 = []

for item in lst:
    if item in lst2 or [item[0], item[2], item[1]] in lst2:
        pass
    else:
        lst2.append( item )

print( lst )
print( lst2 )

@David W This is also a universal problem with a known solution

>>> lst = [ ['e', 1, 2], ['e', 2, 1], ['e', 1, 2], ['e', 2, 3] ]
>>> lst2 = list(unique_everseen(lst, key=lambda x: tuple(sorted(x[1:3]))))
>>> lst2
[['e', 1, 2], ['e', 2, 3]]

unique_everseen is in the itertools module's documentation Click Here

@Gribouillis ...

Thank you for the more Pythonic example code ... (and ref.)

I suspected that might be the case in Python :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.