0

I am having a txt file with a text that I import in Python and I want to separate it at every 3 words.

For example,

Python is an interpreted, high-level and general-purpose programming language

I want to be,

[['Python', 'is', 'an'],['interpreted,', 'high-level','and'],['general-purpose','programming','language']].

My code so far,

lines = [word.split() for word in open(r"c:\\python\4_TRIPLETS\Sample.txt", "r")]
print(lines)

gives me this output,

[['Python', 'is', 'an', 'interpreted,', 'high-level', 'and', 'general-purpose', 'programming', 'language.', "Python's", 'design', 'philosophy', 'emphasizes', 'code', 'readability', 'with', 'its', 'notable', 'use', 'of', 'significant', 'whitespace.', 'Its', 'language', 'constructs', 'and', 'object-oriented', 'approach', 'aim', 'to', 'help', 'programmers', 'write', 'clear,', 'logical', 'code', 'for', 'small', 'and', 'large-scale', 'projects.']]

Any ideas?

Alex_Pap
  • 276
  • 3
  • 15

2 Answers2

2

Use list comprehension to convert list into chunks of n items

with open('c:\\python\4_TRIPLETS\Sample.txt', 'r') as file:
    data = file.read().replace('\n', '').split()
    lines = [data[i:i + 3] for i in range(0, len(data), 3)]
    print(lines)
Shreevardhan
  • 12,233
  • 3
  • 36
  • 50
0

You can use a split string to separate each word and then go through the list and group them into pairs of 3 words.

   final =  = [None] * math.ceil(lines/3)
   temp = [None] * 3
   i = 0
   for x in lines:
      if(i % 3 == 0)
         final.append(temp)
         temp = [None] * 3
      temp.append(x)            
deamonLucy
  • 205
  • 3
  • 9