Count frequency of item in a list of tuples

前端 未结 4 1036
梦谈多话
梦谈多话 2020-12-17 15:30

I have a list of tuples as shown below. I have to count how many items have a number greater than 1. The code that I have written so far is very slow. Even if there are arou

4条回答
  •  难免孤独
    2020-12-17 16:04

    Let me give you an example to make you understand.Although this example is very much different than your example, I found it very helpful while solving these type of questions.

    from collections import Counter
    
    a = [
    (0, "Hadoop"), (0, "Big Data"), (0, "HBase"), (0, "Java"),
    (1, "Postgres"), (2, "Python"), (2, "scikit-learn"), (2, "scipy"),
    (2, "numpy"), (2, "statsmodels"), (2, "pandas"), (3, "R"), (3, "Python"),
    (3, "statistics"), (3, "regression"), (3, "probability"),
    (4, "machine learning"), (4, "regression"), (4, "decision trees"),
    (4, "libsvm"), (5, "Python"), (5, "R"), (5, "Java"), (5, "C++"),
    (5, "Haskell"), (5, "programming languages"), (6, "statistics"),
    (6, "probability"), (6, "mathematics"), (6, "theory"),
    (7, "machine learning"), (7, "scikit-learn"), (7, "Mahout"),
    (7, "neural networks"), (8, "neural networks"), (8, "deep learning"),
    (8, "Big Data"), (8, "artificial intelligence"), (9, "Hadoop"),
    (9, "Java"), (9, "MapReduce"), (9, "Big Data")
    ]
    # 
    # 1. Lowercase everything
    # 2. Split it into words.
    # 3. Count the results.
    
    dictionary = Counter(word for i, j in a for word in j.lower().split())
    
    print(dictionary)
    
    # print out every words if the count > 1
    [print(word, count) for word, count in dictionary.most_common() if count > 1]
    

    Now this is your example solved in the above manner

    from collections import Counter
    a=[('example',123),('example-one',456),('example',987),('example2',987),('example3',987)]
    
    dict = Counter(word for i,j in a for word in i.lower().split() )
    
    print(dict)
    
    [print(word ,count) for word,count in dict.most_common() if count > 1  ]
    

提交回复
热议问题