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

如何将输入文件中的字符串转换为指定格式的元组列表?

Convert Input String to Target Tuple List (Python Solution)

Here's a straightforward way to achieve your desired output using Python. The approach breaks down the problem into simple, manageable steps:

Step-by-Step Explanation

  1. Split the input string into individual entries: Each item in your input is separated by a space, so we'll split on spaces first to get each entry (like TEST.JOB or TEST.LAST_LOGIN,10).
  2. Process each entry:
    • For entries with a comma (e.g., TEST.LAST_LOGIN,10): Split the entry into two parts at the comma, then split the first part at the dot to get the prefix and suffix. Combine these with the numeric value to form a 3-element tuple.
    • For entries without a comma (e.g., TEST.JOB): Split directly at the dot to form a 2-element tuple.
  3. Collect all tuples into a list: Gather all processed tuples into the final result list.

Complete Code Example

# Your input string
input_str = "TEST.JOB TEST.LAST_LOGIN,10 TEST.DATA,12 TEST.QUEUE_PARAM,09 TEST.ENTITY_PROFILE"

# Split into individual entries
entries = input_str.split()

# Initialize the result list
result = []

for entry in entries:
    if ',' in entry:
        # Split into the dot-separated part and the number
        dot_part, num = entry.split(',', 1)
        # Split the dot part into prefix and suffix
        prefix, suffix = dot_part.split('.', 1)
        # Add 3-element tuple to result
        result.append((prefix, suffix, num))
    else:
        # Split directly at the dot for 2-element tuple
        prefix, suffix = entry.split('.', 1)
        result.append((prefix, suffix))

# Print the final result
print(result)

Output

Running this code will produce exactly the list you're looking for:

[('TEST', 'JOB'), ('TEST', 'LAST_LOGIN', '10'), ('TEST', 'DATA', '12'), ('TEST', 'QUEUE_PARAM', '09'), ('TEST', 'ENTITY_PROFILE')]

This solution is flexible and handles both types of entries (with or without commas) cleanly. Using split(',', 1) and split('.', 1) ensures we only split once, which is a robust practice even if there were additional dots or commas in future inputs.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.25 07:59:51