Python 3.x 在循环中添加字典中相同键的值
我只是导入一个csv文件并将其转换为字典,还为不同的列指定了字段名。有人能教我如何在循环中添加字典中相同键的值吗Python 3.x 在循环中添加字典中相同键的值,python-3.x,dictionary,Python 3.x,Dictionary,我只是导入一个csv文件并将其转换为字典,还为不同的列指定了字段名。有人能教我如何在循环中添加字典中相同键的值吗 import csv with open("breast_cancer_v1.csv", newline='') as file: fieldnames = ['1', '2','3','4','5','6','7','8','9','10','11'] print ("1: ID", "\n2: Diagnosis", "\n3: a) radius
import csv
with open("breast_cancer_v1.csv", newline='') as file:
fieldnames = ['1', '2','3','4','5','6','7','8','9','10','11']
print ("1: ID", "\n2: Diagnosis", "\n3: a) radius (mean of distances from center to points on the perimeter)", "\n4: b) texture (standard deviation of gray-scale values)", "\n5: c) perimeter", "\n6: d) area", "\n7: e) smoothness (local variation in radius lengths)", "\n8: f) compactness (perimeter^2 / area - 1.0)", "\n9: g) concavity (severity of concave portions of the contour)", "\n10: h) concave points (number of concave portions of the contour)", "\n11: i) symmetry", "\n12: j) fractal dimension (coastline approximation - 1)")
csv_file = csv.DictReader(file, fieldnames)
for row in csv_file:
print(row)
示例:我希望将“3”列中的所有值相加,或计算“2”列中有多少个“B”。还在学习如何使用字典。导入csv后,我在循环中打印了我的字典,如下所示:
以
行['3']
的形式访问第3列,对于第2列,使用行['2']
如果这不是为了学习字典,您一定要看看它的导入器功能。。。