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

Python如何从分号分隔字符串组成的列表中提取整数并计算总和

实现代码

直接基于给定的studlist完成计算,不需要提前拆分独立的姓名、分数列表,和你原有代码的输出效果完全一致:

studlist = ['mario;90;80', 'denis;80;70', 'fabio;60;70']
for student in studlist:
    info_list = student.split(';')
    stu_name = info_list[0]
    sum_score = int(info_list[1]) + int(info_list[2])
    print('the total score of', stu_name, 'is', sum_score)
逻辑说明
  • 逐次取出studlist中的单条学生记录字符串
  • 用split(';')方法按分号拆分字符串,得到的列表第一个元素为姓名,第二、三个元素为字符串格式的分数
  • 将两个分数转为整数后求和,直接输出对应结果

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.28 01:30:00