擅长:python、mysql、java
<p>这里有一种获取第一个唯一datetime的方法,如果您想要随机项,您可以像<a href="https://stackoverflow.com/questions/976882/shuffling-a-list-of-objects">here</a>中那样首先无序排列列表</p>
<pre><code>data = [
{
'id': '16e26a4a9f97fa4f',
'received_on': '2019-11-01 11:05:51',
'customer_group': 'Life-time Buyer'
},
{
'id': '16db0dd4a42673e2',
'received_on': '2019-10-09 14:12:29',
'customer_group': 'Lead'
},
{
'id': '16db0dd4199f5897',
'received_on': '2019-10-09 14:12:29',
'customer_group': 'Lead'
}
]
datetime = set()
result = []
for d in data:
dt = d['received_on']
if dt not in datetime:
result.append(d)
datetime.add(dt)
result
</code></pre>
<p>输出:</p>
<pre><code>[{'id': '16e26a4a9f97fa4f',
'received_on': '2019-11-01 11:05:51',
'customer_group': 'Life-time Buyer'},
{'id': '16db0dd4a42673e2',
'received_on': '2019-10-09 14:12:29',
'customer_group': 'Lead'}]
</code></pre>