0
def random_integers(lb, ub, trials):
    ri = []
    for i in range(trials):
        ri.insert(i, randint(lb, ub))
    return ri

This function creates the list of random integers. (Range of the integers: lb <= the integers <= ub, Length of the integers: trials) What I want to do is count the frequency of elements of list that created by this function. Ex) [(1, 1), (2, 1), (3, 1), (4, 0), (5,1)] for [1,2,3,5] So I wrote this code:

def freq_integers(num_list):
    fi = []
    for i in range(ub - lb + 1)
        fi.insert(i, (lb + i, num_list.count(lb + i)))
    return fi

If lb, ub in this function is same as random_integers function, it will work well. But lb and ub is only vaild in random_integers function. I should use one parameter(num_list) so I can't define function as freq_integers(lb, ub, num_list). And I can't use lb, ub as a global variable because random_integers function has a parameter for that.

This problem is different from (How to count the frequency of the elements in an unordered list?)

Because this problem has to count the freuquency of item that not in the list. Ex) [(1, 1), (2, 1), (3, 1), (4, 0), (5,1)] for [1,2,3,5]: from this it should count 4 as 0 which is not in the list. + Other quesitons count the thing only in list. like [1,1,1,2,2] : 1 appears 3 times 2 appears 2times.My problem is [1,1,1,2,2] can be counted as 1 appears 3times 2 appears 2times 3 appears 0times because of the input "Range". In this case Range would be 1<=integers<=3, length was 5. I want to know if other solution exists that don't use lb and ub –

Community
  • 1
  • 1
KKR
  • 9
  • 4
  • 2
    In my opinion the easiest way to achieve such behaviour is relying on the [`collections.Counter`](https://docs.python.org/3.8/library/collections.html#collections.Counter) class. – Marco May 14 '20 at 09:20
  • 1
    https://stackoverflow.com/questions/2161752/how-to-count-the-frequency-of-the-elements-in-an-unordered-list – TitoOrt May 14 '20 at 09:22
  • thank you but using counter method is not allowed... – KKR May 14 '20 at 09:24
  • https://stackoverflow.com/questions/6046387/counting-occurrences-in-a-python-list. This is a common problem and has many implementations you can look for online – Tomerikoo May 14 '20 at 09:29
  • What's the point of *"counting"* something that is not in the list? If it is not in the list, you know its count is `0`... Also you say that you can't access `lb` and `ub` and only have `num_list` so that is not even possible – Tomerikoo May 14 '20 at 09:47
  • I edited the question more specific for your question. sorry English is not my first language. – KKR May 14 '20 at 10:15
  • This doesn't change my question. You are saying you can't use `lb` and `ub`. So you can only work with `num_list` which is the numbers **present**. You don't know what numbers to *"count"* as zero. But I don't understand how is that a problem. If you didn't count a number at all (it is not in `num_list`) then its count is `0`... – Tomerikoo May 14 '20 at 11:17
  • You just need to use a `Counter` (or just a regular dict if you are not allowed), and then just query the counts as `counter.get(num, 0)`. This way, if a number was not present, you will print `0` – Tomerikoo May 14 '20 at 11:20
  • how is that a problem.-It's simply because looks good,,,thanks anyway. I define lb = min(num_list) , ub = max(num_list) in the second function. It couldn't express the 0 in some cases but i should done this problem.. – KKR May 14 '20 at 11:38
  • I think you overcomplicating but ok... Just use a dict and the `get` method – Tomerikoo May 14 '20 at 11:45

1 Answers1

0

convert list to set before counting

random_list = random_integers(lb, ub, trials)
union = set(random_list)
counting = {num:random_list.count(num) for num in union}
mtdot
  • 312
  • 2
  • 10