0

此代码来自《Learning python》一书,它用于对以逗号分隔的文本文件中的列进行求和。我真的无法理解第 7、8 和 9 行。谢谢您的帮助。这是代码:

filename='data.txt'
sums={}
for line in open(filename):
    cols=line.split(',')
    nums=[int(col) for col in cols]
    for(ix, num) in enumerate(nums):
        sums[ix]=sums.get(ix, 0)+num
for key in sorted(sums):
    print(key, '=', sums[key])
4

2 回答 2

2

看起来输入文件包含逗号分隔的整数行。该程序打印出每列的总和。

您混淆了缩进,这改变了程序的含义,而且它一开始就写得不是很好。这里有很多评论:

filename='data.txt'    # name of the text file

sums = {}              # dictionary of { column: sum }
                       #   not initialized, because you don't know how many columns there are

# for each line in the input file,
for line in open(filename):
    # split the line by commas, resulting in a list of strings
    cols = line.split(',')
    # convert each string to an integer, resulting in a list of integers
    nums = [int(col) for col in cols]

    # Enumerating a list numbers the items - ie,
    #   enumerate([7,8,9]) -> [(0,7), (1,8), (2,9)]
    # It's used here to figure out which column each value gets added to
    for ix, num in enumerate(nums):
        # sums.get(index, defaultvalue) is the same as sums[index] IF sums already has a value for index
        # if not, sums[index] throws an error but .get returns defaultvalue
        # So this gets a running sum for the column if it exists, else 0;
        # then we add the new value and store it back to sums.
        sums[ix] = sums.get(ix, 0) + num

# Go through the sums in ascending order by column -
#   this is necessary because dictionaries have no inherent ordering
for key in sorted(sums):                    
    # and for each, print the column# and sum
    print(key, '=', sums[key])

我会写得有点不同;就像是

from collections import Counter
sums = Counter()

with open('data.txt') as inf:
    for line in inf:
        values = [int(v) for v in line.split(',')]
        sums.update(enumerate(values))

for col,sum in sorted(sums.iteritems()):
    print("{}: {}".format(col, sum))
于 2013-01-31T02:47:34.880 回答
1

假设你理解第 1-6 行……</p>

第 7 行:

sums[ix]=sums.get(ix, 0)+num

sums.get(ix, 0)与 相同sums[ix],只是如果ix not in sums它返回0。所以,这就像sums[ix] += num,除了它首先将值设置为0if 这是你第一次看到ix

因此,应该清楚的是,在这个循环结束时,sums[ix]将得到 column 中所有值的总和ix

这是一种愚蠢的做法。正如 mgilson 指出的那样,您可以直接使用defaultdict,这样就不需要额外的逻辑。或者,更简单地说,您可以只使用 alist而不是 a dict,因为这(按连续的小数字索引)正是lists 的用途......</p>

第 8 行:

for key in sorted(sums):

首先,您可以迭代任何对象dict,就好像它是 alist或其他可迭代对象一样,它与迭代 具有相同的效果sums.keys()。因此,如果sums看起来像{ 0: 4, 1: 6, 2: 3 },您将迭代0, 1, 2.

除了dicts 没有任何固有的顺序。您可能会得到0, 1, 2,或者您可能会得到1, 0, 2,或任何其他命令。

因此,sorted(sums)只需按排序顺序返回键的副本list,保证您将按0, 1, 2该顺序获得。

再一次,这很愚蠢,因为如果你一开始就使用 a list,你就会把事情整理好。

第 9 行:

print(key, '=', sums[key])

这应该是显而易见的。如果key迭代0, 1, 2, 那么这将打印0 = 4, 1 = 6, 2 = 3.

因此,换句话说,它打印出每个列号,以及该列中所有值的总和。

于 2013-01-31T03:03:04.757 回答