I have two strings 'I go to school and play badminton in the evening' and 'I go to e2 and r2 in the evening'. How to find e2 = 'school' and r2 = 'play badminton'. I tried using for loops but looking for a more elegant way to do it.
Asked
Active
Viewed 67 times
-2
-
What if the string is 'I go to school and swim and play badminton in the evening'? – superb rain Aug 09 '20 at 16:39
-
Yes it is possible. in that case, e2=school and r2=swim and play badminton – Curious Aug 09 '20 at 17:16
-
1Why would r2 include "in the evening" then? And why wouldn't e2 include the "and swim" instead? – superb rain Aug 09 '20 at 17:18
-
Does this answer your question? [Get difference between two lists](https://stackoverflow.com/questions/3462143/get-difference-between-two-lists) – bad_coder Aug 10 '20 at 00:56
3 Answers
2
a = set('I go to school and play badminton in the evening'.split(' '))
b = set('I go to and in the evening'.split(' '))
print(a - b)
>>> {'badminton', 'school', 'play'}
edit for your edit:
If you would also like to name them directly from the parsing, you would need to tweak the input a bit and probably also use: Does Python have an ordered set?

Or Y
- 2,088
- 3
- 16
-
-
1@Curious Just so I can know how to further edit my Answer: 1.Can you manipulate the input? 2.Are the name important? – Or Y Aug 09 '20 at 16:17
-
Kindly read the question again. You must have not read the recent edit. – Curious Aug 09 '20 at 16:19
-
@Curious I did read your edit, but I want further clarification in order to **help you** – Or Y Aug 09 '20 at 16:20
-
-
0
a = "I go to school and play badminton in the evening"
b = "I go to and in the evening"
a_set = set(a.split())
b_set = set(b.split())
print(a_set.difference(b_set))
# {'badminton', 'school', 'play'}

gnahum
- 426
- 1
- 5
- 13
-
Please don't post only code as answer, but also provide an explanation what your code does and how it solves the problem of the question. Answers with an explanation are usually more helpful and of better quality, and are more likely to attract upvotes. – Mark Rotteveel Aug 10 '20 at 06:22
0
Given that pattern may change every time along with the string it turns out to be a string compare problem. Quite elegant solution may be provided with use of difflib
.
str1 = "I go to e2 and r2 in the evening"
str2 = "I go to school and play badminton in the evening"
from difflib import SequenceMatcher
s = SequenceMatcher(None, str1, str2)
diff = [(str1[opcode[1]:opcode[2]], str2[opcode[3]:opcode[4]])
for opcode in s.get_opcodes() if opcode[0] == 'replace']
print(diff)
# [('e2', 'school'), ('r2', 'play badminton')]
Previous solution:
I think most apropriate and flexible in this case would be using regex search.
import re
pattern = "I go to (.*) and (.*) in the evening"
string = "I go to school and play badminton in the evening"
m = re.match(pattern, string)
e2 = m.groups()[0]
r2 = m.groups()[1]
result = e2 == 'school' and r2 == 'play badminton'
print(result)

apawelek
- 131
- 1
- 7
-
-
I answered your question. It's not specified in your question how pattern may change. Please clarify it or provide example. – apawelek Aug 09 '20 at 16:37
-
I think I understand what you mean now. Phrases like 'e2' or 'r2' or other apper explicitly in a text in various places - right? – apawelek Aug 09 '20 at 19:56