Python Forum
frequency of largest number group
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
frequency of largest number group
#1
Hello,

i have a list e.g
a=[112211121112222121122111]
i want to find the frequency of the largest group of '1' and '2'

e.g in 'a' for number 2: '22' is the largest group and its frequency= 2
and for number 1: '111' is the largest group and its frequency=3

Thank you,
Reply
#2
this is a list with jut one element. Is this really what you work with?
why the expected output for 2 is 22? There is group 2222.
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
sorry, my mistake, please ignore '2222' rest of the question is same,

Thank you
Reply
#4
Hello,

i have a list e.g

a=[112211121112121122111]

i want to find the frequency of the largest group of '1' and '2'

e.g in 'a' for number 2: '22' is the largest group and its frequency= 2
and for number 1: '111' is the largest group and its frequency=3

Thank you,
Reply
#5
a = [112211121112121122111] is a list with only one element.
The answer to your question is effortless because there is only one element in your list.

I guess you mean a sequence like this:
a = [1, 1, 2, 2, 1, 1, 1, 2, 1, 1, 1, 2, 2, 2, 2, 1, 2, 1, 1, 2, 2, 1, 1, 1]
The items in the list are Integers, but they could also be str or something else.

Make a function, which saves the last_value, start_index and current index.
If there is a difference between last and current value, you've to add the last value with start_index, end_index to a list. Then you assign the current_value to last_value and the start_index. After your loop is done, you've to add the last remaining group.

Then you have a list with items where one value is the value itself, start_index and end_index.
You can get the shortest and longest group with a key function.

So if you save the groups like this pattern:
(value, start_index, end_index)
You can get the longest and shortest distance by calculating it.

def by_distance(element):
    value, start, end = element
    return end - start


result = [(42, 4, 22), (33, 22, 25)]
print("Shortest")
print(min(result, key=by_distance))
print("Longest")
print(max(result, key=by_distance))
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#6
Just brute force :-)

from itertools import groupby

a = [1, 1, 2, 2, 1, 1, 1, 2, 1, 1, 1, 2, 2, 2, 2, 1, 2, 1, 1, 2, 2, 1, 1, 1]

for item in set(a):
    print(f'{item}: {max(len([*streak]) for el, streak in groupby(a) if item == el)}')

# will output something like that (sets are unordered):
1: 3
2: 4
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Question Help to find the largest int number in a file directory SalzmannNicholas 1 1,588 Jan-13-2022, 05:22 PM
Last Post: ndc85430
  find 2 largest equal numbers Frankduc 13 3,443 Jan-11-2022, 07:10 PM
Last Post: Frankduc
  Largest product in a grid (projecteuler problem11) tragical 1 2,243 Sep-14-2020, 01:03 PM
Last Post: Gribouillis
  Extract the largest value from a group without replacement (beginner) preliator 1 2,039 Aug-12-2020, 01:56 PM
Last Post: DPaul
  Sort by the largest number of the same results (frequency) inlovewiththedj 3 2,145 Apr-01-2020, 07:29 AM
Last Post: DPaul
  Find the second largest number DarkCraftPlayz 8 11,129 May-29-2019, 02:46 AM
Last Post: heiner55
  Creating a program to look for the largest prime number of a number Wikki14 4 3,836 Sep-08-2018, 12:30 AM
Last Post: Skaperen
  How to use python to do "for each 365 data, print the largest 18 value? ctliaf 1 2,636 Apr-28-2018, 08:14 PM
Last Post: snippsat
  Finding largest value in a for loop mingchew 1 2,655 Aug-16-2017, 12:02 PM
Last Post: sparkz_alot
  Calculate the fewest zip codes, for the largest coverage nilamo 4 6,934 Mar-23-2017, 01:31 PM
Last Post: Bass

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020