擅长:python、mysql、java
<p>实际上,您是在附加元素而不是列表。使用计数器</p>
<pre><code>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
</code></pre>
<p>下面是一个解决方案,它可以在代码后面直接假定<code>wordOccur = ['tears', 1, 'go', 1, 'i', 4, 'you', 7, 'love', 2, 'when', 3]</code></p>
<pre><code>random_iterator = iter(wordOccur)
[[next(random_iterator),next(random_iterator)] for i in range(len(wordOccur)//2)]
</code></pre>
<p><strong>输出</strong></p>
<pre><code>[['tears', 1], ['go', 1], ['i', 4], ['you', 7], ['love', 2], ['when', 3]]
</code></pre>