Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3949826
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T01:32:21+00:00 2026-05-20T01:32:21+00:00

Title edit: capitalization fixed and ‘for python’ added. Is there a better or more

  • 0

Title edit: capitalization fixed and ‘for python’ added.

Is there a better or more standard way to do what I’m describing?
I want input like this:

[1, 1, 1, 0, 2, 2, 0, 2, 2, 0, 0, 3, 3, 0, 1, 1, 1, 1, 1, 2, 2, 2]

to be transformed to this:

[0, 1, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 3, 0, 0, 0, 1, 0, 0, 0, 2, 0]

or, even better, something like this (describing similar output differently, but now not limited to integers):

labels: [1, 2, 3, 1, 2]

positions(where 1 identified the first occupiable position, as per my matplotlib plot): [2, 7, 12.5, 17, 21]

The input data is categorical data that classified a plot – in the picture below, grouped plots share a categorical feature which I’d like to label only once for the group. I’ll be using 2 axes for two different variables, but I think that’s besides the point for now.

Note: This image does not reflect either set of sample data – it’s just to get across the idea of grouping together categories. Group a should be labeled at x=5, since there’s a blank space between the first two and second to vertical data groups, and 0 is the line on the right side.

Image demonstrating placement of tick marks in the center of a category of data

Here’s what I’ve got:

data = [1, 1, 1, 2, 2, 2, 2, 2, 3, 4, 3, 2, 2, 1, 1, 1, 1]
last = None
runs = []
labels = []
run = 1
for x in data:
    if x in (last, 0):
        run += 1
    else:
        runs.append(run)
        run = 1
        labels.append(x)
    last = x
runs.append(run)
runs.pop(0)
labels.append(x)
tick_positions = [0]
last_run = 1
for run in runs:
    tick_positions.append(run/2.0+last_run/2.0+tick_positions[-1])
    last_run = run
tick_positions.pop(0)
print tick_positions
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T01:32:22+00:00Added an answer on May 20, 2026 at 1:32 am

    To get the labels you can use itertools groupby:

    >>> import itertools
    >>> numbers = [1, 1, 1, 0, 2, 2, 0, 2, 2, 0, 0, 3, 3, 0, 1, 1, 1, 1, 1, 2, 2, 2]
    >>> list(k for k, g in itertools.groupby(numbers))
    [1, 0, 2, 0, 2, 0, 3, 0, 1, 2]
    

    And to remove the zeros you can use a comprehension:

    >>> list(k for k, g in itertools.groupby(x for x in numbers if x != 0))
    [1, 2, 3, 1, 2]
    

    If you want to get the positions too, then you’ll have to iterate through the list yourself as you are already doing. groupby doesn’t keep track of that for you.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

EDIT: Modified title and added update. UPDATE : We no longer believe this is
Feel free to edit the title if you know how to formulate the question
EDIT: This was formerly more explicitly titled: - Best solution to stop Kontiki's KHOST.EXE
Title says it mostly. I want to add a simple extension method to the
There must be a cleaner way to do what i'm trying to do here...
I've added my edit button self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemEdit target:self action:@selector(editNavButtonPressed)] autorelease]; But
EDIT: changed the title to fit the code below. I'm trying to retrieve a
[EDIT] Hmm. Perhaps this question should be titled what is the default user-input dialog
Title is the entire question. Can someone give me a reason why this happens?
Title says what i'm trying to do. I can successfully generate an assembly if

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.