简体   繁体   English

Python字典按带有时间戳的值对键进行排序

[英]Python Dictionary sort keys by a value with timestamp

I have a dictionary that is populated with wifi probe requests that are collected by an antenna. 我有一本字典,里面装有天线收集的wifi探测请求。 The keys in the dictionary the unique MAC addresses in the probe requests. 词典中的键是探测请求中的唯一MAC地址。 The construction is this: 结构是这样的:

dictionary = {KEY=[MACADDRESS] : FIELDS=0:[FIRSTSEEN] 1:[LASTSEEN] 2:[DECIBEL] 3:[COUNTER] 4:[SSID]}

The dictionary could then look like this: 字典可能看起来像这样:

dictionary = {'00:00:00:00:00:01': ['Aug 18, 2017 18:15:56.081727942 CEST', 'Aug 18, 2017 18:25:37.669160369 CEST', '-60', 18, 'SSID1'], 
'00:00:00:00:00:02': ['Aug 18, 2017 18:19:22.764895209 CEST', 'Aug 18, 2017 18:33:50.052906956 CEST', '-46', 5, 'SSID2'], 
'00:00:00:00:00:03': ['Aug 18, 2017 18:18:53.489779683 CEST', 'Aug 18, 2017 18:45:53.489779683 CEST', '-62', 1, 'SSID3']}

Now let's say there are 100 entries and I want to produce a list ordered by the [LASTSEEN] timestamp value (value 1 of the key) . 现在,假设有100个条目, 我想生成一个按[LASTSEEN]时间戳值(键的值1)排序的列表 How would I go about and do that? 我将如何去做?

I found a "similar" question with an answer , where the use of sorted(dictionary) with a lambda function is used, but I can't wrap my mind around how to apply it here, also it doesn't take the timestamp into account. 我找到了一个带有答案的“类似”问题,其中使用了带lambda函数的sorted(dictionary),但我无法确定如何在此处应用它,也没有将时间戳记进去帐户。 I thought maybe somehow the timestamp could be converted to UNIX time, which could make the sorting process easier. 我想也许可以将时间戳转换为UNIX时间,这可以使排序过程更容易。

UPDATE (In order to clarify further): 更新(为了进一步阐明):

I want to rearrange the keys of the dictionary (if possible) and keep all their values. 我想重新排列字典的键(如果可能)并保留所有值。 I want to be able to run a sort function in the dictionary and then select the 3 newest like: 我希望能够在字典中运行排序功能,然后选择3个最新的像:

#Here I would sort the dictionary
sort_function()

#then I would print eg. 3 entries
count_to_3 = 0
for keys, values in dictionary.items():
    count_to_3 += 1
    print keys,values
    if count_to_3 == 3:
        break

The output would then be (sorted by second timestamp). 然后将输出(按第二个时间戳排序)。 Sorry if it seems confusing with the double timestamps: 抱歉,如果似乎与双重时间戳混淆了:

00:00:00:00:00:03 ['Aug 18, 2017 18:18:53.489779683 CEST','Aug 18, 2017 18:45:53.489779683 CEST', '-62', 1, 'SSID3']
00:00:00:00:00:02 ['Aug 18, 2017 18:19:22.764895209 CEST','Aug 18, 2017 18:33:50.052906956 CEST', '-46', 5, 'SSID2']
00:00:00:00:00:01 ['Aug 18, 2017 18:15:56.081727942 CEST','Aug 18, 2017 18:25:37.669160369 CEST', '-60', 18, 'SSID1']

By nature a dictionary is not sorted, so you can instead create a list of tuples with your key and values. 从本质上讲,字典是不排序的,因此您可以使用键和值创建一个元组列表。

lst = sorted(d.items(), key = lambda kvp: kvp[1][1])

[('00:00:00:00:00:01', ['Aug 18, 2017 18:15:56.081727942 CEST', 'Aug 18, 2017 18:25:37.669160369 CEST', '-60', 18, 'SSID1']), ...]

Now what you need to be aware of is that because your time stamp is a string it may not sort exactly how you want it to. 现在,您需要知道的是,由于时间戳是一个字符串,因此可能无法完全按照您的意愿进行排序。 In that case you would have to convert it. 在这种情况下,您将不得不对其进行转换。 For example some function to_datetime . 例如一些函数to_datetime

lst = sorted(d.items(), key = lambda kvp: to_datetime(kvp[1][1]))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM