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

基于两个list与映射dictionary生成指定格式输出的实现咨询

实现代码

Python版本实现逻辑如下,直接运行即可得到目标结构:

list1 = ['1', '1', '1', '2', '1', '1', '0.5', '1', '1', '2', '1', '0.5', '1', '1', '1', '1', '0.5']
list2 = ['bug', 'dark', 'dragon', 'electric', 'fairy', 'fight', 'fire', 'flying', 'ghost', 'grass', 'ground', 'ice', 'normal', 'poison', 'psychic', 'rock', 'steel', 'water']
dictionary = {0.25: 'super effective', 0.5: 'effective', 1: 'normal', 2: 'weak', 4: 'super weak', 0: 'resistant'}

# 初始化结果字典
result = {}

# 同步遍历两个列表的同索引元素
for val1, val2 in zip(list1, list2):
    # 把list1的字符串转成浮点型,查映射字典得到分类标签
    key = dictionary[float(val1)]
    # 归类到对应分类下的集合,没有对应分类则先创建
    if key not in result:
        result[key] = set()
    result[key].add(val2)

print(result)
运行输出结果

针对示例数据运行后得到的结构如下,和要求的格式完全匹配:

{
    'normal': {'bug', 'dark', 'dragon', 'fairy', 'fight', 'flying', 'ghost', 'ground', 'normal', 'poison', 'psychic', 'rock'},
    'weak': {'electric', 'grass'},
    'effective': {'fire', 'ice', 'steel'}
}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.25 23:45:07