0

I have a list of strings that goes like this:

1;213;164
2;213;164
3;213;164
4;213;164
5;213;164
6;213;164
7;213;164
8;213;164
9;145;112
10;145;112
11;145;112
12;145;112
13;145;112
14;145;112
15;145;112
16;145;112
17;145;112
1001;1;151
1002;2;81
1003;3;171
1004;4;31

I would like to remove all duplicates where second 2 numbers are the same. So after running it through program I would get something like this:

1;213;164
9;145;112
1001;1;151
1002;2;81
1003;3;171
1004;4;31

But something like

8;213;164
15;145;112
1001;1;151
1002;2;81
1003;3;171
1004;4;31

would also be correct.

1
  • 1
    Please update your question with the code you have tried. Commented Jun 2, 2020 at 9:44

3 Answers 3

1

Here is a nice and fast trick you can use (assuming l is your list):

list({ s.split(';', 1)[1] : s for s in l }.values())

No need to import anything, and fast as can be.

In general you can define:

def custom_unique(L, keyfunc):
    return list({ keyfunc(li): li for li in L }.values())
Sign up to request clarification or add additional context in comments.

2 Comments

Oh, this solution is nice. If I understand correctly it generates a dictionary with the second 2 numbers as key and whole string as value. so whenever the same 2 numbers appear first one gets overwritten and no duplicates are made.
@vanjavk yes, that's it
1

You can group the items by this key and then use the first item in each group (assuming l is your list).

import itertools
keyfunc = lambda x: x.split(";", 1)[1]
[next(g) for k, g in itertools.groupby(sorted(l, key=keyfunc), keyfunc)]

4 Comments

Just need to change to: x.split(";")[1]+";"+x.split(";")[2]and this solution works!
I fixed my answer and it is now slightly more efficient than your suggestion.
@OneLyner, please describe where it fails, s\he asked for the second 2 numbers to be the same
@TomRon sorry I misread :) You might be interested in my answer.
0

Here is a code on the few first items, just switch my list with yours:

x = [
'7;213;164',
'8;213;164',
'9;145;112',
'10;145;112',
'11;145;112',
]
new_list = []
for i in x:
    check = True
    s_part = i[i.find(';'):]
    for j in new_list:
        if s_part in j:
            check = False
    if check == True:
        new_list.append(i)

print(new_list)

Output:

['7;213;164', '9;145;112']

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.