社区所有版块导航
Python
python开源   Django   Python   DjangoApp   pycharm  
DATA
docker   Elasticsearch  
aigc
aigc   chatgpt  
WEB开发
linux   MongoDB   Redis   DATABASE   NGINX   其他Web框架   web工具   zookeeper   tornado   NoSql   Bootstrap   js   peewee   Git   bottle   IE   MQ   Jquery  
机器学习
机器学习算法  
Python88.com
反馈   公告   社区推广  
产品
短视频  
印度
印度  
Py学习  »  Python

将包含字符串和整数的列表中的元素求和,并将答案放入另一个列表python中

user8760022 • 5 年前 • 1602 次点击  

我必须写一个代码来计算列表中每个学生的成绩之和并返回总数。 我的代码是:

list=['student1',10,20,40,'student2',20,20,40,'student3',20,30,40,'student4',20,10,30]
list2=[]
for i in range(0,len(list1),4):
    list2.append(list1[i])
    for j in range(len(list1)):
        if j%4 == 1:
            sum= list1[j]+list1[j+1]+list1[j+2]
            list2.append(sum)
print(list2)

预期产出应如下:

['student1', 70, 'student2', 80,'student3', 90, 'student4', 60]

但我得到了这个结果:

['student1', 70, 80, 90, 60, 'student2', 70, 80, 90, 60, 'student3', 70, 80, 90, 60, 'student4', 70, 80, 90, 60]

​

那么我的代码怎么了?

​

Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/51412
 
1602 次点击  
文章 [ 6 ]  |  最新文章 5 年前
sahasrara62
Reply   •   1 楼
sahasrara62    6 年前

这是解决办法,你不必担心学生的分数。

list1=['student1',10,20,40,'student2',20,20,40,'student3',20,30,40,'student4',20,10,30]

student_name =[]
student_name_index =[]

for i in range(len(list1)):        
    if type(list1[i]) == int:
        pass
    else:
        student_name.append(list1[i])
        student_name_index.append(i)

student_name_index.append(len(list1)-1)

total_marks=[]

for i in range(1,len(student_name_index)):
    total_marks.append(sum(list1[student_name_index[i-1]+1:student_name_index[i]]))

final_result =[]

for name, mark in zip(student_name,total_marks):
    final_result.append(name)
    final_result.append(mark)

print(final_result)

# output ['student1', 70, 'student2', 80, 'student3', 90, 'student4', 30] 
balderman
Reply   •   2 楼
balderman    6 年前

尝试

in_lst = ['student1', 10, 20, 40, 'student2', 20, 20, 40, 'student3', 20, 30, 40, 'student4', 20, 10, 30]
out_lst = []
for x in range(0, len(in_lst), 4):
    student_entry = in_lst[x:x + 4]
    out_lst.append(student_entry[0])
    out_lst.append(sum(student_entry[1:]))
print(out_lst)

输出

['student1', 70, 'student2', 80, 'student3', 90, 'student4', 60]
Soner Say
Reply   •   3 楼
Soner Say    5 年前

它是这样的:0,4,8,所以不需要第二个for循环。

你已经知道号码在哪里了。(i+1,i+2,i+3)我是学生的名字。

list1=['student1',10,20,40,'student2',20,20,40,'student3',20,30,40,'student4',20,10,30]
list2=[]
for i in range(0, len(list1), 4):
    list2.append(list1[i])
    sum = list1[i+1]+list1[i+2]+list1[i+3]
    list2.append(sum)
print(list2)
Siong Thye Goh
Reply   •   4 楼
Siong Thye Goh    6 年前
list1=['student1',10,20,40,'student2',20,20,40,'student3',20,30,40,'student4',20,10,30]
list2=[]
for i in range(0,len(list1),4):
    list2.append(list1[i])
    sum= list1[i+1]+list1[i+2]+list1[i+3]
    list2.append(sum)
print(list2)

生产

['student1', 70, 'student2', 80, 'student3', 90, 'student4', 60]

代码中的第二个for循环遍历列表中的每个元素,并在索引mod 4为1时执行操作。你不需要两个for循环。

trikPu
Reply   •   5 楼
trikPu    6 年前

在第二个循环中,再次循环整个初始列表1,因此总是附加所有和。如果你的列表总是包含一个名字和三个等级,那么你可以去掉第二个循环,用第一个循环中的“i”作为迭代器来求和,就像你现在做的那样。

Alderven
Reply   •   6 楼
Alderven    6 年前

您可以使用单循环:

lst = ['student1', 10, 20, 40, 'student2', 20, 20, 40, 'student3', 20, 30, 40, 'student4', 20, 10, 30]
result = []
for i in range(0, len(lst), 4):
    result.extend((lst[i], sum(lst[i+1:i+4])))

输出:

['student1', 70, 'student2', 80, 'student3', 90, 'student4', 60]

如果每个学生的分数不同,例如:

lst = ['student1', 10, 20, 'student2', 10, 20, 30, 'student3', 10, 20, 30, 40, 'student4', 10, 20, 30, 40, 50]

然后:

s = 0
result = [lst[0]]
for i in lst[1:]:
    try:
        s += int(i)
    except ValueError:
        result.extend((s, i))
        s = 0
result.append(s)

输出:

['student1', 30, 'student2', 60, 'student3', 100, 'student4', 150]