You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python整数列表映射字典键及飞镖盘扇形值匹配技术问询

Hey there! Let's break down your dartboard development problem into two clear parts—mapping angle ranges to those unordered 1-20 scores, and handling integer list to dictionary key mappings in Python.

Dartboard Sector Score Mapping & Python Dictionary Tips

1. Mapping Angle Ranges to Dartboard Scores

First, let's tackle the core problem: linking your theta angle ranges to the unordered 1-20 dartboard scores.

Standard dartboards have a fixed score order (clockwise starting from the top 0° position: 20, 1, 18, 4, 13, 6, 10, 15, 2, 17, 3, 19, 7, 16, 8, 11, 14, 9, 12, 5). You'll need to align your 20 angle ranges with this sequence (or your custom score order if building a non-standard board).

Step-by-Step Implementation

First, define your angle ranges and corresponding score list:

# Your 20 consecutive 18° angle ranges (0° to 360°)
angle_ranges = [(0, 18), (18, 36), (36, 54), (54, 72), (72, 90), 
                (90, 108), (108, 126), (126, 144), (144, 162), (162, 180),
                (180, 198), (198, 216), (216, 234), (234, 252), (252, 270),
                (270, 288), (288, 306), (306, 324), (324, 342), (342, 360)]

# Standard dartboard score order (matches the angle ranges above)
dart_scores = [20, 1, 18, 4, 13, 6, 10, 15, 2, 17, 
               3, 19, 7, 16, 8, 11, 14, 9, 12, 5]

Next, create a function to calculate which sector a given (x,y) coordinate falls into, then return the corresponding score. Note: adjust the angle calculation to match your coordinate system (most dartboards have 0° at the top, increasing clockwise):

import math

def get_dart_sector_score(x, y):
    # Handle bullseye (center) case
    if x == 0 and y == 0:
        return 50  # Or 25 for outer bullseye, adjust as needed
    
    # Calculate angle in radians (default from math.atan2 is counter-clockwise from x-axis)
    theta_rad = math.atan2(y, x)
    # Convert to 0-360° clockwise, with 0° at the top
    theta_deg = (90 - math.degrees(theta_rad)) % 360
    
    # Find the matching angle range and return its score
    for idx, (start, end) in enumerate(angle_ranges):
        if start <= theta_deg < end:
            return dart_scores[idx]
    
    return None  # Should never reach here if ranges cover 0-360°

You can also pre-create a dictionary mapping each angle range to its score for quick lookups:

range_score_map = {range_tuple: score for range_tuple, score in zip(angle_ranges, dart_scores)}
# Example lookup: range_score_map[(0, 18)] returns 20

2. Mapping Integer Lists to Dictionary Keys in Python

Depending on exactly what you need, there are a few common ways to map integer lists to dictionary keys:

Case 1: Use list elements as keys (map integers to custom values)

If you want each integer in your list to be a key pointing to a specific value, use a dictionary comprehension or manual assignment:

integer_list = [1, 2, 3, 4, 5]

# Manual mapping
value_map = {1: "one", 2: "two", 3: "three", 4: "four", 5: "five"}

# Automated mapping (if values follow a pattern)
value_map = {num: str(num) for num in integer_list}

Case 2: Use list indices as keys (map positions to list elements)

If you want to map the index of each integer in the list to the integer itself (useful for quick index-based lookups):

integer_list = [10, 20, 30, 40, 50]

index_to_value = {idx: val for idx, val in enumerate(integer_list)}
# Result: {0: 10, 1: 20, 2: 30, 3: 40, 4: 50}

Case 3: Map integer ranges (like your theta ranges) to keys

Since your angle ranges are tuples of integers (which are hashable), you can directly use them as dictionary keys as shown earlier with range_score_map.


内容的提问来源于stack exchange,提问作者user6510891

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 07:10:18