Problem Statement:
We are given a sentence, we need to find all possible combinations of two words and print them in lexicographical order. Ignore all adjacent combinations and print the rest combinations only.
Code to find Two words combination in Python:
sentence = input('Enter sentence: ') sentence = sentence.split(' ') all_comb = [] for i in range(len(sentence)-2): for j in range(i+2, len(sentence)): all_comb.append([sentence[i], sentence[j]]) all_comb = sorted(all_comb, key=lambda x: (x[0])) for i in range(len(all_comb)): for j in range(2): for k in range(1, len(sentence)-1): try: if all_comb[i][j]==sentence[k]: try: if all_comb[i][j+1]==sentence[k+1]: all_comb.pop(k) except: a=1 try: if all_comb[i][j+1]==sentence[k-1]: all_comb.pop(k) except: a=1 try: if all_comb[i][j-1]==sentence[k+1]: all_comb.pop(k) except: a=1 try: if all_comb[i][j-1]==sentence[k-1]: all_comb.pop(k) except: a=1 except: a=1 print(all_comb)
Output:

Also Read:
- Hyphenate Letters in Python
- Earthquake in Python | Easy Calculation
- Striped Rectangle in Python
- Perpendicular Words in Python
- Composite Number in Python
- Greatest Among Four Numbers in Python
- Reverse the sentence in Python
- Denominations in Python
- Min and max values in an array in JavaScript
- Keyboard events in JavaScript
- Reaching Ground Floor in Python
- Number of Moves in Python
- Starks Adventure in Python
- Neutralization in Python | Assignment Expert
- Free shipping in Python
- Raj has ordered two electronic items Python | Assignment Expert
- First Place in Python
- Triples with Properties in Python
- Nested list Indexing Python
- Team Points in Python
- Two words combination in Python
- ID Card in Python
- Cipher with a key in Python | Assignment Expert
- Multiple of 5 in Python
- Sandglass Star in Python
- Multiple of 3 in Python | Assignment Expert
- Ticket selling in Cricket Stadium using Python | Assignment Expert
- Sum of List Elements in Python
- All possible subsets in Python
- Names and Nicknames in Python