计算Python列表中的不同值

时间:2018-08-06 18:57:44

标签: python pandas nlp

我有一个类似下面的数据报

lable                          unigrams                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   
ham    [ive, searching, right, word, thank, breather, i, promise, wont] 
spam   [free, entry, 2, wkly, comp, win, fa, cup, final, tkts, 21st, may]

我想计算不同的/独特的火腿字母组合和不同的垃圾邮件字母组合。

我可以使用df.unigrams.nunique()计算一列中的不同值。 我可以使用unigramCount = unigramCorpus.loc["ham", "unigrams"].count('ive')

来计算给定的字母组合在火腿中出现的次数

但是如何计算给定列表中不同值的数量?例如:["ham", "spam"]

预期输出: 火腿= 9 垃圾邮件= 12

4 个答案:

答案 0 :(得分:2)

您需要:

$(".list").on("click", ".button", function() {
  var x = $(this).closest(".item");
  var y = $(".button").text();
  $('#value').append('(' + y + ')');
});

答案 1 :(得分:1)

使用np.unique
(在每个字母组合列表中仅计数不同的词,因此重复项将被忽略):

df['counts'] = df.apply(lambda x: len(np.unique(x['unigrams'])), axis=1) 
print(df)

>   label   unigrams    counts
0   ham [ive, searching, right, word, thank, breather,...   9
1   spam    [free, entry, 2, wkly, comp, win, fa, cup, fin...   12

答案 2 :(得分:0)

unigramCount = len(set(eval(unigramCorpus.loc [“ ham”,“ unigrams]])))

答案 3 :(得分:0)

您的问题不是很清楚,但这可能有用:

df['count'] = df['unigrams'].map(lambda x: len(x))