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 8468967
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T16:05:43+00:00 2026-06-10T16:05:43+00:00

I have a dictionary that provides a mapping from real number tuples to an

  • 0

I have a dictionary that provides a mapping from real number tuples to an identifying integer. Given a list of tuples containing numbers that are within a tolerance of, but not exactly equal to those in the dictionary, I would like to produce a list of the corresponding integers.

Example:

tdict = {(0.334, 0.333, 0.333):1, (0.167, 0.666, 0.167):2, (0.5, 0.5, 0):3}
tlist = [(0.333, 0.333, 0.333), (0.16667, 0.6666667, 0.17), (0.34, 0.33, 0.33), (0.5001, 0.4999, 0.0)]
tol = 0.01

Running the code I want should produce the result

ilist = [1,2,1,3]

since all numbers in each of the tuples are within the given tolerance of those in the corresponding tuples in tdict. I could do this by iterating over tdict.keys() and comparing to each one individually, but I feel like there should be a better way. What is the most efficient way to get the corresponding integers to these tuples? It doesn’t have to involve a dictionary, that just seemed most natural to me. I’m using Python 3.

  • 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-06-10T16:05:44+00:00Added an answer on June 10, 2026 at 4:05 pm

    You obviously want to project points in 3D-space on a 3D grid with a certain grid spacing (which is directly related to your tolerance value) and create some kind of histogram. Write yourself a projection function: It takes an arbitrary 3-element list/tuple (a vector describing a point in space) as argument and projects it onto a certain grid point. You do this for filling up your dictionary as well as for reading it out. Furthermore, regarding the keys in your dictionary, I think you should go with tuples of integers instead of floats, because I am not sure if floats can ever be identical.

    This is an implementation example:

    from collections import defaultdict
    from random import random as rn
    
    class Grid(object):
        def __init__(self, spacing):
            self.spacing = spacing
            self.griddict = defaultdict(int)
    
        def add_point(self, coords):
            """
            `vid`, a voxel id, is a tuple of indices, indicating one grid
            bin for each dimension, e.g. (1, 5, 2)
            rule: i_x = int(floor(x_coord / spacing))
            """
            vid = tuple([int(c//self.spacing) for c in coords])
            self.griddict[vid] += 1
    
        def get_point(self, coords):
            vid = tuple([int(c//self.spacing) for c in coords])
            return self.griddict[vid]
    
        def vid_centercoords(self, vid):
            """
            Return the real coordinates in space for a certain voxel,
            which is identified by its voxel id `vid` (a tuple of indices).
            """
            return tuple([(i-1)*self.spacing + self.spacing/2 for i in vid])
    
    
    
    N = 20
    fillpoints = [(rn(),rn(),rn()) for _ in xrange(N)]
    testpoints = [(rn(),rn(),rn()) for _ in xrange(N)]
    
    grid = Grid(spacing=0.3)
    
    for p in fillpoints:
        grid.add_point(p)
    
    print [grid.get_point(p) for p in testpoints]
    

    What it does: it creates 20 random vectors in 3D space (all coordinates between 0 and 1). It populates a 3D grid using these points in space. The grid has a spacing of 0.3 in each dimension. Each of these 20 points in space is assigned to a certain voxel (just a word for a 3D pixel) in the grid. Each assignment increased the counter of the corresponding voxel by 1 (rendering the grid to be a histogram). Then, another random set of 20 vectors is used to read out the voxels. These points are again projected onto voxels, but this time the counter is just returned instead of increased. Execution test:

    $ python gridtest.py 
    [2, 1, 0, 1, 0, 0, 0, 2, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0]
    

    Execution with your data:

    fillpoints = [(0.334, 0.333, 0.333), (0.167, 0.666, 0.167), (0.167, 0.666, 0.167), (0.5, 0.5, 0), (0.5, 0.5, 0), (0.5, 0.5, 0)]
    testpoints = [(0.333, 0.333, 0.333), (0.16667, 0.6666667, 0.17), (0.34, 0.33, 0.33), (0.5001, 0.4999, 0.0)]
    
    grid = Grid(spacing=0.03)
    for p in fillpoints:
        grid.add_point(p)
    print [grid.get_point(p) for p in testpoints]
    

    It prints [1, 2, 1, 3] as desired. I haven’t thought deeply about the relation spacing=3*tolerance. It likely is wrong. I only know that there is a deterministic relation. Proving/finding this formula is left for you as an exercise 🙂

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

Sidebar

Related Questions

I have a dictionary structure that maps an id (integer) into a number (double).
Suppose you have a dictionary that contains valid words. Given an input string with
Suppose you have a dictionary that contains valid words. Given an input string with
I have a dictionary containing the necessary info that needs to be sent to
I have a dictionary that has keys associated with lists. mydict = {'fruits': ['banana',
I have a dictionary that hosts diameter(in mm) and worth of euro coins: CoinsDiameters
Is it possible to clear a Flex flash.utils.Dictionary ? I have a Dictionary that
I'm newish to Python (and stackoverflow)...bear with me! I have a dictionary that looks
I have a Dictionary object that is formed using a double as its key.
I have a dictionary of strings that i want the user to be able

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.