对list中的数值求和(求一个list里面所有数的和)

http://www.itjxue.com  2023-02-13 14:26  来源:未知  点击次数: 

请教一个list中数据求和的问题

常规做法就是遍历list,然后取出studentNoname进行比对,放在map中,like this:

for( student in studentList){

if(map.contains(student[(name+studentNo)])){

map.get(name+studentNo)+student.score

}else{

// put score into the map with namestudentNo key

}

}

this map object is the final result you want

//或者你可以写一个子类继承hashmap,重写put方法,把这个方法包含了你需要的逻辑,然后一个个直接put进去即可

【VB】怎么把list中所有的数相加?

Private Sub Command1_Click()

Dim i As Integer, sum As Long

sum = 0

For i = 0 To List1.ListCount - 1

sum = sum + List1.List(i)

Next

Print sum

End Sub

Private Sub Form_Load()

For i = 1 To 10

List1.AddItem i

Next

End Sub

python中如何让一个list里的数字部分相加

1、创建python文件,testsum.py;

2、编写python代码,初始化list内容;

dt?=?{"0":?"10",?"J":?"11",?"Q":?"12"}

test?=?['2D',?'3S',?'4D',?'QD',?'0S',?'JD']

3、编写函数,进行list内容求和;

def?func(li):

nums?=?[]

result?=?0

for?index,?s?in?enumerate(li):

tp?=?s[0]

if?tp?in?dt:

nums.append(dt.get(tp))

li[index]?=?dt.get(tp)+s[1]

result?+=?int(dt.get(tp))

else:

nums.append(tp)

result?+=?int(tp)

?print('单独出来的数字',nums)

print('转换后的求和值',result)

func(test)

4、右键选择‘在终端中运行Python文件’;

5、执行后查看执行结果,可以发现求和后数据值。

如何将list中的数字部分相加起来

楼主!重要的计算逻辑都写在代码中了,请参考:pre t="code" l="python"# -*- coding: utf-8 -*-

'''数据准备'''

trans_map = {'Q': 12, 'J':11, '0': 10}

target_list = ['2D', '3S', '4D', 'QD', '0S', 'JD']

sum_value = 0

for single_str in target_list:

'''取出列表中数字的第一个'''

operate_num = single_str[0]

'''如果需要转换,则按转换规则取数

否则,直接求和'''

if trans_map.has_key(operate_num):

sum_value += trans_map[operate_num]

else:

sum_value += int(operate_num)

'''打印结果'''

print sum_value如有问题,欢迎追问,,!

用ArrayList存数后,怎么让集合里的数求和?

1、先将两数组排序,然后两数组先分别求和;

2、计算差值的绝对值,设为a,将a除以2后取整数,设为b;

3、然后将和大的数组中值最接近b的数与和小的数组中最小值交换。多次执行这个过程,直到绝对值最小为止。

例:

(1)public?static?void?AverageArray();

(2){;

(3)int[]?values?=?{1,?300,?20,?11,?5,?301};

(4)int?n?=?values.Length?/?2;

(5)int?halfSum?=?values.Sum()?/?2;

(6)ListListListint?heap?=?new?ListListListint(n?+?1);

(7)heap.Add(new?ListListint());

(8)heap[0].Add(new?Listint());

(9)heap[0][0].Add(0);

(10)#region?Seek?the?array;

(11)for?(int?k?=?1;?k?=?2?*?n;?k++);

(12){;

(13)int?i?=?1;

(14)if?(k?=?n);

(15){;

(16)i?=?k;

(17)heap.Add(new?ListListint());

(18)};

(19)else;

(20){;

(21)i?=?n;

(22)};

(23)for?(;?i?=?1;?i--);

(24){;

(25)foreach?(Listint?tempList?in?heap[i?-?1]);

(26){;

(27)if(tempList[0]?+?values[k?-?1]?=?halfSum);

(28){;

(29)Listint?newList?=?new?Listint(tempList);

(30)newList[0]?+=?values[k?-?1];

(31)newList.Add(k?-?1);

(32)heap[i].Add(newList);

(33)};

(34)};

(35)};

(36)};

(37)#endregion;

(38)ListListint?result?=?heap[n];

(39)for?(int?k?=?0;?k?+?1??result.Count;?);

(40){;

(41)if?(result[k][0]??result[k?+?1][0]);

(42){;

(43)result.RemoveAt(k?+?1);

(44)};

(45)else?if?(result[k][0]?==?result[k?+?1][0]);

(46){;

(47)k++;

(48)};

(49)else;

(50){;

(51)result.RemoveRange(0,?k?+?1);

(52)k?=?0;

(53)};

(54)};

(55)}。

(责任编辑:IT教学网)

更多

推荐Flash actionscript文章