0

Based on a list, I need to create a list of tuple with each tuple containing (value, nbr_of_occurence_of_the_value_in_the_list). My code is working but I feel it could be improve, does someone has an idea on how to make this code better ?

def get_tuple_count_list(_list):
    tuple_count_list = []
    for v in _list:
        if v not in [v1 for (v1,count) in tuple_count_list]:
            tuple_count_list.append((v,1))
            continue
        i = [v1 for (v1,count) in tuple_count_list].index(v)
        tuple_count_list[i] = (v, tuple_count_list[i][1]+1)
    return tuple_count_list

print(get_tuple_count_list(["a","b","b","d","e","a","a","a","c","b"]))
#result expected: [('a', 4), ('b', 3), ('d', 1), ('e', 1), ('c', 1)]
Utopion
  • 935
  • 1
  • 4
  • 15

3 Answers3

3

How about simply using Counter.most_common() which is a standard library util producing exactly your desired output:

from collections import Counter

def get_tuple_count_list(_list):
    return Counter(_list).most_common()

>>> get_tuple_count_list(["a","b","b","d","e","a","a","a","c","b"])
[('a', 4), ('b', 3), ('d', 1), ('e', 1), ('c', 1)]

Also see the Counter docs.

Even with plainer means you should not operate on a list of tuples while taking the counts. The tuples' immutability and the list's linear search are big hinderances both in terms of code readability and performance. You should always use a constant time lookup structure (typically a dictionary like Counter):

def get_tuple_count_list(_list):
    counts = {}
    for x in _list:
        counts[x] = counts.get(x, 0) + 1
    return [*counts.items()]
    # return sorted(counts.items(), key=lambda i: -i[1])
user2390182
  • 72,016
  • 6
  • 67
  • 89
0
def get_tuple_count_list(_list):
    tuple_count_dict = {}
    for v in _list:
        if v not in tuple_count_dict:
            tuple_count_dict[v] = 1
            continue
        tuple_count_dict[v] = tuple_count_dict[v] + 1
    return list(tuple_count_dict.items())

Python tuple is immutable and the time complexity of searching item in list is O(n). Replacing list of tuple with dict can improve performance.

csosstudy
  • 1
  • 2
  • While this code may solve the question, [including an explanation](//meta.stackexchange.com/q/114762) of how and why this solves the problem would really help to improve the quality of your post, and probably result in more up-votes. Remember that you are answering the question for readers in the future, not just the person asking now. Please [edit] your answer to add explanations and give an indication of what limitations and assumptions apply. – Adrian Mole Oct 13 '21 at 13:37
0
def get_tuple_count_list(_list):
    output = []
    for i in _list:
        count_val = _list.count(i)
        if (i,count_val) not in output:
            output.append((i,count_val))
    return output
Dev
  • 387
  • 1
  • 12