-1

Here is what my current list looks like:

wordOccur = ['tears', 1, 'go', 1, 'i', 4, 'you', 7, 'love', 2, 'when', 3]

This is how I created it:

    wordOccur = []

    for x in keywords:

            count = words.count(x)

            wordOccur.append(x)

            wordOccur.append(count)

the term words refers to a list of strings. Each string is a singular word from a poem

How do I make wordOccur = [['tears', 1], ['go', 1],[ 'i', 4],[ 'you', 7],[ 'love', 2],[ 'when', 3]] ?

Hai Vu
  • 37,849
  • 11
  • 66
  • 93

5 Answers5

1

You can use a simple list comprehension:

words = 'i love you. when you cry tears i cry. when i love you. you, you, you! when i go to you.'
keywords = ['tears', 'go', 'i', 'you', 'love', 'when']

wordOccur = [[w, words.count(w)] for w in keywords]

Output:

[['tears', 1], ['go', 1], ['i', 4], ['you', 7], ['love', 2], ['when', 3]]

You might find a dictionary more useful though:

wordOccur = { w : words.count(w) for w in keywords }

Output:

{'i': 4, 'tears': 1, 'go': 1, 'love': 2, 'when': 3, 'you': 7}
Nick
  • 138,499
  • 22
  • 57
  • 95
0

You are actually appending element rather than list. Using Counter

from collections import Counter
d = Counter(keywords)
[[word,d.get(word,0)] for word in set(keywords)] # use set here to remove the duplicates but will not conserve the order

Below is the solution which can go after your code directly assuming wordOccur = ['tears', 1, 'go', 1, 'i', 4, 'you', 7, 'love', 2, 'when', 3]

random_iterator = iter(wordOccur)
[[next(random_iterator),next(random_iterator)] for i in range(len(wordOccur)//2)]

OUTPUT

[['tears', 1], ['go', 1], ['i', 4], ['you', 7], ['love', 2], ['when', 3]]
mad_
  • 8,121
  • 2
  • 25
  • 40
0

You can change this code.

wordOccur = []

for x in keywords:
  count = words.count(x)
  wordOccur.append(x)
  wordOccur.append(count)

Into this code.

wordOccur = [[word, keywords.count(word)] for word in keywords]
dzakyputra
  • 682
  • 4
  • 16
0

this result is from your given input to desire output

wordOccur = ['tears', 1, 'go', 1, 'i', 4, 'you', 7, 'love', 2, 'when', 3]

result = []

for index in range(0, len(wordOccur),2):
    tmp = [wordOccur[index], wordOccur[index+1]]
    result.append(tmp)

print(result) # [['tears', 1], ['go', 1], ['i', 4], ['you', 7], ['love', 2], ['when', 3]]


# or 

result2 = [ [a,b] for a,b in zip(wordOccur[0::2], wordOccur[1::2])]
print(result2) # [['tears', 1], ['go', 1], ['i', 4], ['you', 7], ['love', 2], ['when', 3]]

lets say your keywords contain all the keyword releated to poem and doesn't have the frequency on them so in that case you can use counter

 keywords = [] # your word list
 from collections import Counter

 result_freq = Counter(keywords)

 result_list_of_list = [[k,v] for k,v in result_freq.items()]
sahasrara62
  • 10,069
  • 3
  • 29
  • 44
0

I do recommend you use a dictionary for this. But if you want to use arrays that's perfectly fine as well, will just make your code a little harder to work with.

try something like this:

words = 'i love you. when you cry tears i cry. when i love you. you, you, you! when i go to you.'

keywords = ['tears', 'go', 'i', 'you', 'love', 'when']
wordOccur = []

for w in keywords:
    if w in words:
        wordOccur += [[w, words.count(w)]]

your output will look like what you're looking for

>>> wordOccur
[['tears', 1], ['go', 1], ['i', 4], ['you', 7], ['love', 2], ['when', 3]]
ingenium21
  • 55
  • 1
  • 10