“减少” python 中的函数不适用于“namedtuple”?

发布于 2024-12-08 07:06:41 字数 1384 浏览 0 评论 0原文

我有一个日志文件,其格式如下:

datetimestring \t username \t transactionName \r\n

我正在尝试对此数据集运行一些统计信息。我有以下代码:

import time
import collections
file = open('Log.txt', 'r')

TransactionData = collections.namedtuple('TransactionData', ['transactionDate', 'user', 'transactionName'])
transactions = list()

for line in file:
    fields = line.split('\t')

    transactionDate = time.strptime(fields[0], '%Y-%m-%d %H:%M:%S')
    user = fields[1]
    transactionName = fields[2]

    transdata = TransactionData(transactionDate, user, transactionName)
    transactions.append(transdata)

file.close()

minDate = reduce(lambda x,y: min(x.transactionDate, y.transactionDate), transactions)
print minDate

我不想为这样一个简单的数据集定义一个类,所以我使用了一个名称元组。当我尝试运行时,出现此错误:

Traceback (most recent call last):
  File "inquiriesStat.py", line 20, in <module>
    minDate = reduce(lambda x,y: min(x.transactionDate, y.transactionDate), transactions)
  File "inquiriesStat.py", line 20, in <lambda>
    minDate = reduce(lambda x,y: min(x.transactionDate, y.transactionDate), transactions)
AttributeError: 'time.struct_time' object has no attribute 'transactionDate'

lambda 函数似乎直接对“transactionDate”属性进行操作,而不是传入完整元组。如果我将 lambda 更改为:

lambda x,y: min(x, y)

它会按我的预期工作。有什么想法为什么会出现这种情况吗?

I have a log file that is formatted in the following way:

datetimestring \t username \t transactionName \r\n

I am attempting to run some stats over this dataset. I have the following code:

import time
import collections
file = open('Log.txt', 'r')

TransactionData = collections.namedtuple('TransactionData', ['transactionDate', 'user', 'transactionName'])
transactions = list()

for line in file:
    fields = line.split('\t')

    transactionDate = time.strptime(fields[0], '%Y-%m-%d %H:%M:%S')
    user = fields[1]
    transactionName = fields[2]

    transdata = TransactionData(transactionDate, user, transactionName)
    transactions.append(transdata)

file.close()

minDate = reduce(lambda x,y: min(x.transactionDate, y.transactionDate), transactions)
print minDate

I did not want to define a class for such a simple dataset, so I used a name tuple. When I attempt to run, I get this error:

Traceback (most recent call last):
  File "inquiriesStat.py", line 20, in <module>
    minDate = reduce(lambda x,y: min(x.transactionDate, y.transactionDate), transactions)
  File "inquiriesStat.py", line 20, in <lambda>
    minDate = reduce(lambda x,y: min(x.transactionDate, y.transactionDate), transactions)
AttributeError: 'time.struct_time' object has no attribute 'transactionDate'

It appears that the lambda function is operating on the 'transactionDate' property directly instead of passing in the full tuple. If I change the lambda to:

lambda x,y: min(x, y)

It works as I would expect. Any ideas why this would be the case?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

娇柔作态 2024-12-15 07:06:41

只需使用:

minDate = min(t.transactionDate for t in transactions)

下面是您的代码无法工作的原因的解释。

假设 transactions = [t1, t2, t3] 其中 t1...t3 是三个命名元组。

根据 reduce 的定义,您的代码

reduce(lambda x,y: min(x.transactionDate, y.transactionDate), transactions)

是 :显然

min(min(t1.transactionDate, t2.transactionDate).transactionDate, t3.transactionDate)

,内部 min() 返回 time.struct_time 而不是命名元组,因此当 reduce 尝试应用 . transactionDate 到它,失败了。

有一些方法可以解决这个问题,并使用reduce来解决这个问题。然而,考虑到直接应用 min 就能完成这项工作,而且在我看来,这似乎没有什么意义,比涉及 reduce 的任何事情都清晰得多。

Simply use:

minDate = min(t.transactionDate for t in transactions)

Below is an explanation of why your code isn't working.

Let's say transactions = [t1, t2, t3] where t1...t3 are three named tuples.

By the definition of reduce, your code:

reduce(lambda x,y: min(x.transactionDate, y.transactionDate), transactions)

is equivalent to

min(min(t1.transactionDate, t2.transactionDate).transactionDate, t3.transactionDate)

Clearly, the inner min() returns time.struct_time instead of a named tuple, so when reduce tries to apply .transactionDate to it, that fails.

There are ways to fix this, and to make use of reduce for this problem. However, there seems to be little point given that a direct application of min does the job and to my eye is a lot clearer than anything involving reduce.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文