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

  • SEARCH
  • Home
  • 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 7441449
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T11:00:52+00:00 2026-05-29T11:00:52+00:00

Real world application: categorize bytes into the categories: control, printable, non-printable character (category list

  • 0

Real world application: categorize bytes into the categories: control, printable, non-printable character (category list will be longer)

I have a list of numbers:

numbers = [1, 1, 2, 3, 3, 3, 3, 4]

Now I want to put them into different categories. To do so, I have to define in which category a number belongs. So far I have two approaches, both using a predefined index/value pair.

category_list = ["apple", "apple", "banana", "melon", "melon", "melon"]
category_dict = {1 : "apple", 2 : "apple", 3 : "banana", 4 : "melon", 5 : "melon", 6 : "melon"}
for number in numbers:
    print category_list[number]
    category_dict[number]

Another option would be a list for every category. This is eventually faster to write/implement but forces me to brute-force the dictionary (see one of the answers):

dict_category = {
    apple : [1, 2],
    banana : [3,],
    melon : [4, 5, 6]
}
for number in numbers:
    for key, val in dict_category.iteritems():
        if number in val:
            print key
            break

Is there a better, more pythonic way to do this? Maybe which doesn’t require me to write a list/dict with 256 entries?

  • 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-29T11:00:53+00:00Added an answer on May 29, 2026 at 11:00 am
    # configuration dict, written by the user
    categories = {
        'apple'  : [1, 2],
        'banana' : [3],
        'melon'  : [4, 5, 6]
    }
    
    # dynamically generate lookup table
    lookup = {}
    for cat, nums in categories.iteritems():
        for n in nums: lookup[n] = cat
    
    # ... later:
    from collections import defaultdict
    categorized = defaultdict(list)
    for n in numbers:
        cat = lookup.get(n)
        if not cat:
            continue
        categorized[cat].append(n)
        print "put %d in category %s" % (n, cat)
    

    If your numbers are within a certain range, you could also use a vector for lookup:

    lookup = [None]*256
    for cat, nums in categories.iteritems():
        for n in nums: lookup[n] = cat
    
    # ... later
    categorized = defaultdict(list)
    for n in numbers:
        categorized[lookup[n]].append(n)
        print "put %d in category %s" % (n, lookup[n])
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Has anyone had any experience in building a 'real world' application with the Smart
Has anyone ever used the Bridge pattern in a real world application? If so,
Has anybody gone through the process of converting a real-world business application from ASP.NET
I have a blog-like application with stories and categories: class Category(models.Model): ... class Story(models.Model):
I wish to better understand the real-world application of this new feature that consists
Can any professional please specify some real world application, where he / she has
i am building a real estate application where in it will store the properties
Which dependently typed programming languages could be used for real world application development? These
I am developing a C++ application used to simulate a real world scenario. Based
Who here is using WASP ( http://wasp.sourceforge.net/content/ ) to in real world applications? What

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.