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

如何基于用户输入构建包含对象编号与属性的2D列表

How to Build a 2D List of Object Attributes with User Input

Hey there, let's break down how to create this 2D list where each row holds an object's ID and its attributes (size, weight, shape). The process is straightforward—we'll let the user define how many objects they need, then loop through each one to gather the required details.

Step 1: Set Up the Foundation

First, we start with an empty 2D list to store all our object data. Then we ask the user how many objects they want to add, converting their input to an integer since we'll use it to drive our loop.

Step 2: Loop to Collect Each Object's Details

We’ll use a for loop to iterate once per object. For each iteration, we can either auto-generate the object ID (like Object1, Object2 to match your example) or let the user input a custom ID. Then we ask for the size, weight, and shape, and pack all these values into a sublist that we add to our main 2D list.

Full Working Example

Here's a complete Python script that follows your requirements. It includes an option to auto-generate IDs or let users input custom ones:

# Initialize empty 2D list to store object data
object_attributes = []

# Get number of objects from user (basic version)
num_objects = int(input("Enter the number of objects: "))

# Loop through each object to collect its details
for i in range(num_objects):
    # Option 1: Auto-generate object ID (matches your example format)
    object_id = f"Object{i+1}"
    
    # Uncomment below and comment the line above to let users input custom IDs
    # object_id = input(f"Enter ID for object {i+1}: ")
    
    # Collect each attribute
    size = input(f"Enter size for {object_id} (e.g., big/small): ")
    weight = input(f"Enter weight for {object_id} (e.g., heavy/light): ")
    shape = input(f"Enter shape for {object_id} (e.g., square/round): ")
    
    # Add the object's data as a sublist to the 2D list
    object_attributes.append([object_id, size, weight, shape])

# Print the final 2D list to confirm
print("\nFinal 2D List of Object Attributes:")
for row in object_attributes:
    print(row)

Example Output

If you input 3 when asked for the number of objects, then enter the attributes from your example, you'll get this output:

Final 2D List of Object Attributes:
['Object1', 'big', 'heavy', 'square']
['Object2', 'small', 'heavy', 'round']
['Object3', 'small', 'light', 'round']

Optional: Add Input Validation

To make the script more robust, you can add checks to ensure the user enters valid data (like a positive integer for object count, or expected attribute values). Here's how to add validation for the object count:

# Validate object count input
while True:
    try:
        num_objects = int(input("Enter the number of objects: "))
        if num_objects > 0:
            break
        else:
            print("Please enter a positive whole number.")
    except ValueError:
        print("Invalid input! Please enter an integer.")

You can extend this logic to validate attributes too—for example, checking that the size is either big or small before proceeding.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.25 03:58:06