0

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?

4
Contributors
5
Replies
54
Views
1 Year
Discussion Span
Last Post by David W
1

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

0

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

0

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 )

Edited by David W

1

@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

0

@Gribouillis ...

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

I suspected that might be the case in Python :)

Edited by David W

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.