Python Agent链权重累加For循环实现及代码优化需求问询
Agent ID转换链权重计算逻辑修正
需求规则
- 相同位置重复出现的链:权重按出现次数累加
- 不同位置出现的链:权重累加自身固定权重 + 对应位置的固定权重
- 链固定权重:chain_1=40、chain_2=30、chain_3=20、chain_4=10
- result[0]为host,不参与权重计算
- 若接收方的排序链存在于最终链中,需为chain_1的权重额外累加对应值
现有代码
alphabet = ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"] def agent(host, receiver): # Check the length of host and receiver if len(host) == 4: if len(receiver) == 4: # Check if host and receiver have valid IDs (capital letters) if host[0] in alphabet and host[1] in alphabet and host[2] in alphabet and host[3] in alphabet: if receiver[0] in alphabet and receiver[1] in alphabet and receiver[2] in alphabet and receiver[3] in alphabet: final_chain = [host] receiver_index_1 = alphabet.index(receiver[0]) # Call for_loop_sort for each position in the host and receiver chain_1 = for_loop_sort(host, receiver, receiver_index_1) + host[1:] final_chain.append(chain_1) receiver_index_2 = alphabet.index(receiver[1]) chain_2 = receiver[0] + for_loop_sort(host, receiver, receiver_index_2) + host[2:] final_chain.append(chain_2) receiver_index_3 = alphabet.index(receiver[2]) chain_3 = receiver[0] + receiver[1] + for_loop_sort(host, receiver, receiver_index_3) + host[3:] final_chain.append(chain_3) receiver_index_4 = alphabet.index(receiver[3]) chain_4 = receiver[0] + receiver[1] + receiver[2] + for_loop_sort(host, receiver, receiver_index_4) final_chain.append(chain_4) return final_chain else: return "Error, receiver agent has an invalid ID; agent should only have capital letters" else: return "Error, host agent has an invalid ID; agent should only have capital letters" else: return "Error, receiver agent has an invalid ID; agent should only have 4 capital letters." else: return "Error, host agent has an invalid ID; agent should only have 4 capital letters." def for_loop_sort(host, receiver, receiver_index): # Iterate through the alphabet list for index in range(len(alphabet)): # Check if the current index matches the receiver_index if index == receiver_index: return alphabet[index] host = "CORE" receivers = ["MBID", "BRKS", "JKIC", "MRRY", "BTLR", "ADBY", "DRNT", "IRVN", "HRDN", "DNCN", "BRNT", "PAUL", "WRTH", "JNSN", "LLRD", "BIRD", "JRDN", "PIPN", "EWNG", "BRKL", "OLJW", "CRRY", "TMPS", "TTUM", "GRNT"] # Process each receiver and print the resulting chain for receiver in receivers: result = agent(host, receiver) chain_1, chain_2, chain_3, chain_4 = result[1], result[2], result[3], result[4] # Assign weights to chain_1, chain_2, chain_3, and chain_4 chain_1_weight = 40 chain_2_weight = 30 chain_3_weight = 20 chain_4_weight = 10 cweights = {1:40, 2:30, 3:20, 4:10} # Check if the chain transformations exist in other final_chains and add their weights if chain_1 in result[2:]: chain_1_weight += chain_1_weight if chain_2 in result[3:]: chain_2_weight += chain_2_weight if chain_3 in result[4:]: chain_3_weight += chain_3_weight if chain_4 in result[5:]: chain_4_weight += chain_4_weight print(f"{chain_1}: {chain_1_weight} -> {chain_2}: {chain_2_weight} -> {chain_3}: {chain_3_weight} -> {chain_4}: {chain_4_weight}")
当前运行输出
MORE: 40 -> MBRE: 30 -> MBIE: 20 -> MBID: 10 BORE: 40 -> BRRE: 30 -> BRKE: 20 -> BRKS: 10 JORE: 40 -> JKRE: 30 -> JKIE: 20 -> JKIC: 10 MORE: 40 -> MRRE: 60 -> MRRE: 20 -> MRRY: 10 BORE: 40 -> BTRE: 30 -> BTLE: 20 -> BTLR: 10 AORE: 40 -> ADRE: 30 -> ADBE: 20 -> ADBY: 10 DORE: 40 -> DRRE: 30 -> DRNE: 20 -> DRNT: 10 IORE: 40 -> IRRE: 30 -> IRVE: 20 -> IRVN: 10 HORE: 40 -> HRRE: 30 -> HRDE: 20 -> HRDN: 10 DORE: 40 -> DNRE: 30 -> DNCE: 20 -> DNCN: 10 BORE: 40 -> BRRE: 30 -> BRNE: 20 -> BRNT: 10 PORE: 40 -> PARE: 30 -> PAUE: 20 -> PAUL: 10 WORE: 40 -> WRRE: 30 -> WRTE: 20 -> WRTH: 10 JORE: 40 -> JNRE: 30 -> JNSE: 20 -> JNSN: 10 LORE: 40 -> LLRE: 60 -> LLRE: 20 -> LLRD: 10 BORE: 40 -> BIRE: 60 -> BIRE: 20 -> BIRD: 10 JORE: 40 -> JRRE: 30 -> JRDE: 20 -> JRDN: 10 PORE: 40 -> PIRE: 30 -> PIPE: 20 -> PIPN: 10 EORE: 40 -> EWRE: 30 -> EWNE: 20 -> EWNG: 10 BORE: 40 -> BRRE: 30 -> BRKE: 20 -> BRKL: 10 OORE: 40 -> OLRE: 30 -> OLJE: 20 -> OLJW: 10 CORE: 40 -> CRRE: 60 -> CRRE: 20 -> CRRY: 10 TORE: 40 -> TMRE: 30 -> TMPE: 20 -> TMPS: 10 TORE: 40 -> TTRE: 30 -> TTUE: 20 -> TTUM: 10 GORE: 40 -> GRRE: 30 -> GRNE: 20 -> GRNT: 10
修改后的代码
alphabet = ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"] def agent(host, receiver): if len(host) == 4 and len(receiver) == 4: if all(c in alphabet for c in host) and all(c in alphabet for c in receiver): final_chain = [host] # 生成转换链,简化原冗余的for_loop_sort逻辑 chain_1 = receiver[0] + host[1:] final_chain.append(chain_1) chain_2 = receiver[0] + receiver[1] + host[2:] final_chain.append(chain_2) chain_3 = receiver[0] + receiver[1] + receiver[2] + host[3] final_chain.append(chain_3) chain_4 = receiver final_chain.append(chain_4) return final_chain else: return "Error: Agent IDs must consist of 4 capital letters only" else: return "Error: Agent IDs must be exactly 4 characters long" host = "CORE" receivers = ["MBID", "BRKS", "JKIC", "MRRY", "BTLR", "ADBY", "DRNT", "IRVN", "HRDN", "DNCN", "BRNT", "PAUL", "WRTH", "JNSN", "LLRD", "BIRD", "JRDN", "PIPN", "EWNG", "BRKL", "OLJW", "CRRY", "TMPS", "TTUM", "GRNT"] # 预计算所有接收方的转换链,用于全局统计 all_chains = [] for receiver in receivers: res = agent(host, receiver) if isinstance(res, list): all_chains.append(res[1:]) # 跳过host,仅保留四个转换链 # 处理每个接收方的权重计算 for idx, receiver in enumerate(receivers): result = agent(host, receiver) if not isinstance(result, list): print(result) continue current_chains = result[1:] fixed_weights = [40, 30, 20, 10] calculated_weights = fixed_weights.copy() # 遍历每个链及其位置,按规则计算权重 for pos, chain in enumerate(current_chains): same_pos_count = 0 diff_pos_total = 0 # 统计全局中该链的所有出现位置 for other_chains in all_chains: for other_pos, other_chain in enumerate(other_chains): if other_chain == chain: if other_pos == pos: same_pos_count += 1 else: diff_pos_total += fixed_weights[other_pos] # 相同位置权重累加:次数*固定权重 calculated_weights[pos] = fixed_weights[pos] * same_pos_count # 不同位置权重累加 calculated_weights[pos] += diff_pos_total # 处理chain1的特殊规则:若接收方最终链(chain4)存在于前面的链中,累加对应值 if current_chains[3] in current_chains[:3]: calculated_weights[0] += fixed_weights[3] # 格式化输出 print(f"{current_chains[0]}: {calculated_weights[0]} -> {current_chains[1]}: {calculated_weights[1]} -> {current_chains[2]}: {calculated_weights[2]} -> {current_chains[3]}: {calculated_weights[3]}")
修改说明
- 简化链生成逻辑:删除冗余的
for_loop_sort函数,直接通过字符串拼接生成转换链,减少无效循环; - 全局统计链出现情况:预计算所有接收方的转换链,方便后续统计每个链在全局所有位置的出现次数;
- 按规则计算权重:
- 统计相同位置的出现次数,权重按次数乘以固定值累加;
- 统计不同位置的出现情况,累加对应位置的固定权重;
- 额外处理chain1的特殊规则:若接收方最终链存在于前面的链中,给chain1权重加上chain4的固定权重;
- 优化输入校验:用
all()函数简化字母校验逻辑,减少嵌套层级,提升代码可读性。
内容的提问来源于stack exchange,提问作者mama b
相关产品推荐
相关产品推荐

