Python Forum
frequency of largest number group
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
frequency of largest number group
#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


Messages In This Thread
frequency of largest number group - by anshumanmuj - Jun-22-2020, 04:14 AM
RE: frequency of largest number group - by buran - Jun-22-2020, 04:17 AM
RE: frequency of largest number group - by DeaD_EyE - Jun-22-2020, 08:21 AM
largest number group frequency - by anshumanmuj - Jun-22-2020, 04:36 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
Question Help to find the largest int number in a file directory SalzmannNicholas 1 1,728 Jan-13-2022, 05:22 PM
Last Post: ndc85430
  find 2 largest equal numbers Frankduc 13 3,836 Jan-11-2022, 07:10 PM
Last Post: Frankduc
  Largest product in a grid (projecteuler problem11) tragical 1 2,375 Sep-14-2020, 01:03 PM
Last Post: Gribouillis
  Extract the largest value from a group without replacement (beginner) preliator 1 2,162 Aug-12-2020, 01:56 PM
Last Post: DPaul
  Sort by the largest number of the same results (frequency) inlovewiththedj 3 2,315 Apr-01-2020, 07:29 AM
Last Post: DPaul
  Find the second largest number DarkCraftPlayz 8 11,624 May-29-2019, 02:46 AM
Last Post: heiner55
  Creating a program to look for the largest prime number of a number Wikki14 4 4,041 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,825 Apr-28-2018, 08:14 PM
Last Post: snippsat
  Finding largest value in a for loop mingchew 1 2,758 Aug-16-2017, 12:02 PM
Last Post: sparkz_alot
  Calculate the fewest zip codes, for the largest coverage nilamo 4 7,152 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