1

I'm a beginner in python. I have a text file where I'm supposed to find words that are connected in the text. They are "connected" if they appear in the same sentence more than once.

split_sentences=[]

for sentence in sentences:
    split_sentences.append(sentence.split())

print(split_sentences)

split_sentences is the text file I'm going to use (I have splitted the sentences into words, but still keeping them seperate as sentences). Now, I'm going to use dictionaries to go through each word in every sentence and see if words appear in the same sentence more than once. Do any of you guys know how to execute this with the use of dictionaries?

Rakesh
  • 81,458
  • 17
  • 76
  • 113
fskoft
  • 11
  • 1

1 Answers1

0

Here's an example of finding words that appear more than once in a sentence:

tokens = 'the quick brown fox jumps over the lazy dog'.split()
print(set(filter(lambda x: tokens.count(x) > 1, tokens)))
# Output is 'the', which appears twice
jspcal
  • 50,847
  • 7
  • 72
  • 76
  • *Please* don't repost inefficient solutions. I've lost count how many times I've had to post comments complaining about this exact code's time complexity. – Aran-Fey May 21 '18 at 08:00
  • It's perfectly efficient for the OP's needs. If efficiency were the primary objective, you *would not use python* for this. The language itself is a trade-off between simplicity and efficiency. – jspcal May 21 '18 at 08:08
  • It may be efficient enough for the OP, but StackOverflow is supposed to cater to a wide audience and not a single OP. Even if your solution is perfectly acceptable in this particular case, it's still worth pointing out its weaknesses. And I'm not interested in pointing out the same weakness again every time some 33k rep user decides to repost the same code that's already been posted a trillion times. – Aran-Fey May 21 '18 at 08:10
  • Note this is 1.6x faster than the solution you linked to for the sentence in question. – jspcal May 21 '18 at 08:59