“减少” python 中的函数不适用于“namedtuple”?
我有一个日志文件,其格式如下:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
只需使用:
下面是您的代码无法工作的原因的解释。
假设
transactions = [t1, t2, t3]
其中t1
...t3
是三个命名元组。根据
reduce
的定义,您的代码是 :显然
,内部
min()
返回time.struct_time
而不是命名元组,因此当reduce
尝试应用. transactionDate
到它,失败了。有一些方法可以解决这个问题,并使用
reduce
来解决这个问题。然而,考虑到直接应用min
就能完成这项工作,而且在我看来,这似乎没有什么意义,比涉及reduce
的任何事情都清晰得多。Simply use:
Below is an explanation of why your code isn't working.
Let's say
transactions = [t1, t2, t3]
wheret1
...t3
are three named tuples.By the definition of
reduce
, your code:is equivalent to
Clearly, the inner
min()
returnstime.struct_time
instead of a named tuple, so whenreduce
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 ofmin
does the job and to my eye is a lot clearer than anything involvingreduce
.