1

I want to count words from a dataframe. First I convert the dataframe's column to a list named "lst". and the list is here-

['তাদের উত্তরের মধ্যে সামরিকতার ঝাঁজ ছিল',
 'ফোন করে যা শুনেছি তা তো মানুষ জানে আমরা তো এগুলো শুনতে অভ্যস্ত না']

Then I try this code-

lst1 = []
count=0

for i in lst:
  count += 1
  lst1.append(i.split(' '))

count

and the result of lst1[:2] is -

[['তাদের', 'উত্তরের', 'মধ্যে', 'সামরিকতার', 'ঝাঁজ', 'ছিল'],
 ['ফোন',
  'করে',
  'যা',
  'শুনেছি',
  'তা',
  'তো',
  'মানুষ',
  'জানে',
  'আমরা',
  'তো',
  'এগুলো',
  'শুনতে',
  'অভ্যস্ত',
  'না']]

But I want to a list where all words are in a single list. I want like this-

['তাদের', 'উত্তরের', 'মধ্যে', 'সামরিকতার', 'ঝাঁজ', 'ছিল', 'ফোন','করে','যা','শুনেছি','তা',......,'না']

How can I do this? What should I change to get this?

Karl Knechtel
  • 62,466
  • 11
  • 102
  • 153
tonoy
  • 53
  • 1
  • 5

3 Answers3

1

Just try this. Here I use extend function. extend() is the function extended by lists in Python and hence can be used to perform this task. This function performs the inplace extension of the list. So it takes one by one from the "lst1" and adds to the final list.

lst_final = []

for i in lst1:
  lst_final.extend(i)
Samrat Alam
  • 558
  • 3
  • 19
1

You can use list.extend() method to get a single list in the end.

lst = ['তাদের উত্তরের মধ্যে সামরিকতার ঝাঁজ ছিল',
 'ফোন করে যা শুনেছি তা তো মানুষ জানে আমরা তো এগুলো শুনতে অভ্যস্ত না']

lst1 = []
count=0

for i in lst:
    lst1.extend(i.split(' '))

Output: ['তাদের', 'উত্তরের', 'মধ্যে', 'সামরিকতার', 'ঝাঁজ', 'ছিল', 'ফোন', 'করে', 'যা', 'শুনেছি', 'তা', 'তো', 'মানুষ', 'জানে', 'আমরা', 'তো', 'এগুলো', 'শুনতে', 'অভ্যস্ত', 'না']

Ahmed Javed
  • 60
  • 1
  • 9
0

i.split(' ') returns an array which you then append to an array making an array of arrays. I think you want something like lst1.concat

for i in lst:
  count += 1
  lst1.concat(i.split(' '))
Richard Barker
  • 1,161
  • 2
  • 12
  • 30