1

I made this working code but maybe it's possible to make it shorter?

sents_str = 'dimplegalla:28082000 dimplegalla:28082000'

sents = sents_str.split(' ')
uniqueList = []
uniqueRes = []

for letter in sents:
    if letter.split(':')[1] not in uniqueList:
        uniqueList.append(letter.split(':')[1])
        uniqueRes.append(letter)
print(uniqueRes)
3
  • note: you disregard any differences in the first part of the .split(':') (which happens to always be 'dimplegalla' in your example). Is that intentional? Commented Dec 29, 2020 at 20:20
  • Given just the code you have posted, then: print([sents_str.split()[0]]). But I'm not sure this is what you want. Commented Dec 29, 2020 at 20:29
  • 2
    what is the expected output if sents_str = 'dimplegalla0:28082000 dimplegalla1:28082000'? And what about 'a:0:0 b:0:1'? Commented Dec 29, 2020 at 20:36

3 Answers 3

1

How about this:

In [1]: sents_str = 'dimplegalla:28082000 dimplegalla:28082000'

In [2]: list(set(sents_str.split()))
Out[2]: ['dimplegalla:28082000']
Sign up to request clarification or add additional context in comments.

Comments

0

Yes:

list({sub for sub in sents_str.split()})

Comments

0

try this:

sents_str = 'dimplegalla:28082000 dimplegalla:28082000'
uniqueList= [l.split(':')[1] for l in list(set(sents_str.split(' ')))]
uniqueRes = list(set(sents_str.split(' ')))
print(uniqueList)
print(uniqueRes)

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.