我有一个庞大的列表字典,需要过滤。 这是其输出的示例:
d = {
'hate': [(2310, "Experiencer: 'like hours'", 212, 222),
(2310, "Experiencer: 'two'", 1035, 1038),
(2310, "Experiencer: 'Anakin'", 1560, 1566),
(2310, "Experiencer: ' '", 1619, 1620),
(2310, "Experiencer: 'Tatooine'", 1726, 1734),
(2310, "Experiencer: 'Anakin'", 1775, 1781),
(2310, "Experiencer: 'Master Qui-Gon'", 1863, 1877),
(2310, "Experiencer: 'half'", 1883, 1887),
(2310, "Experiencer: 'One'", 2114, 2117),
(2310, "Experiencer: 'Anakin'", 2180, 2186),
(2310, "Stimulus: 'One'", 2484, 2487),
(2310, "Stimulus: 'Anakin'", 2564, 2570),
(2310, "Stimulus: 'Padme'", 2739, 2744)],
'confirmation': [(4132, "Experiencer: 'like hours'", 212, 222),
(4132, "Experiencer: 'two'", 1035, 1038),
(4132, "Experiencer: 'Anakin'", 1560, 1566),
(4132, "Experiencer: ' '", 1619, 1620),
(4132, "Experiencer: 'Tatooine'", 1726, 1734),
(4132, "Experiencer: 'Anakin'", 1775, 1781),
(4132, "Experiencer: 'Master Qui-Gon'", 1863, 1877),
(4132, "Experiencer: 'half'", 1883, 1887),
(4132, "Experiencer: 'One'", 2114, 2117),
(4132, "Experiencer: 'Anakin'", 2180, 2186),
(4132, "Experiencer: 'One'", 2484, 2487),
(4132, "Experiencer: 'Anakin'", 2564, 2570),
(4132, "Experiencer: 'Padme'", 2739, 2744),
(4132, "Experiencer: 'Anakin'", 2782, 2788),
(4132, "Experiencer: ' '", 2818, 2819),
(4132, "Experiencer: 'centuries'", 3562, 3571),
(4132, "Experiencer: 'one'", 3585, 3588),
(4132, "Experiencer: 'Anakin'", 3679, 3685),
(4132, "Experiencer: 'Anakin Skywalker'", 3789, 3805),
(4132, "Experiencer: 'Obi-Wan'", 4014, 4021),
(4132, "Experiencer: 'Qui-Gon'", 4025, 4032),
(4132, "Experiencer: 'Qui-Gon's'", 4100, 4109),
(4132, "Stimulus: 'Anakin'", 4281, 4287),
(4132, "Stimulus: ' '", 4355, 4356),
(4132, "Stimulus: 'Anakin'", 4436, 4442)]}
每个键(如上所述,其中一个是hate
)在每个列表元素的开头都有一个数字。在这里是:2310
。
我希望能够打印出列表中的两个元素,这些元素的数字与该数字最接近,分别是第二大和第二小。
示例输出:
'hate': [(2310, "Experiencer: 'Anakin'", 2180, 2186),
(2310, "Stimulus: 'One'", 2484, 2487)]
因为
(2310, "Experiencer: 'Anakin'", 2180, 2186)
具有数字2180
,与2310
相比,这是倒数第二个
反过来:
(2310, "Stimulus: 'One'", 2484, 2487)
具有数字2484
,与2310
相比是第二大数字
我想这需要一个for
循环吗?我如何遍历列表字典,将第一个自重复数字与每行的第一个数字进行比较,然后返回最接近的数字,如上所述?
我希望我的问题是可以理解的...
提前谢谢!
编辑:
目标是使浏览字典的过程自动化,并通过过滤对其进行更新。
该词典的期望输出将是这样的:
d = {
'hate': [(2310, "Experiencer: 'Anakin'", 2180, 2186),
(2310, "Stimulus: 'One'", 2484, 2487)],
'confirmation': [(4132, "Experiencer: 'Qui-Gon's'", 4100, 4109),
(4132, "Stimulus: 'Anakin'", 4281, 4287)],
...}
我还编辑了上面到目前为止的输出示例。这是一个列表字典
答案 0 :(得分:2)
使用itertools.groupby
对每个列表中的所有相似元素进行分组,然后对它们进行排序(基于绝对差)并获得前2个元素
>>> from itertools import groupby
>>>
>>> f = lambda t: t[0]
>>> {key:sorted(v, key=lambda t: abs(k-t[3]))[:2] for key,lst in d.items() for k,v in groupby(sorted(lst, key=f), f)}
{'confirmation': [(4132, "Experiencer: 'Qui-Gon's'", 4100, 4109), (4132, "Experiencer: 'Qui-Gon'", 4025, 4032)], 'hate': [(2310, "Experiencer: 'Anakin'", 2180, 2186), (2310, "Stimulus: 'One'", 2484, 2487)]}
答案 1 :(得分:1)
这是最简单,最简单的方法(我能想到)。下面有一个使用itertools
的解决方案,它更优雅,但对于新手来说更难理解。
如果l
是字典中hate
指向的列表:
target_num = l[0][0]
closest_smaller, closest_bigger = 0,0
closest_smaller_diff, closest_bigger_diff = float("inf"), float("-inf")
for element in l:
for num in (l[-2],l[-1]):
diff = target_num - num
if diff > 0 and diff < closest_smaller_diff:
closest_smaller = num
closest_smaller_diff = diff
if diff < 0 and diff > closest_bigger_diff:
closest_bigger = num
closest_bigger_diff = diff
print(closest_smaller, closest_bigger)
答案 2 :(得分:1)
# let big_dict be the big list you start with
output_dict = {}
for key, value in big_dict.items():
# break the list into two lists, for those with third value greater
# and those with third value lesser/equal
higher_tuples = [i for i in value if i[2] > i[0]]
lower_tuples = [i for i in value if i[2] <= i[0]]
# Get the values from that list with
high_closest = min(higher_tuples, key=lambda x: x[2] - x[0])
low_closest = min(lower_tuples, key=lambda x: x[0] - x[2])
# bind them into an output
output_duct[key] = [high_closest, low_closest]
如果您愿意,可以将它们捆绑成一个非常大的单线:
output_dict = {key: [min([i for i in value if i[2] > i[0]], key=lambda a: a[2] - a[0]), min([j for j in value if j[0] <= j[2]], key=lambda b: b[0] - b[2])] for key, value in big_dict.items()}
答案 3 :(得分:1)
如果您的列表已经排序,我们可以使用bisect
查找“ Experiencer”和“ Status”条目之间的位置:
6f7fda9
然后,您可以轻松地处理字典
from bisect import bisect
l=[(2310, "Experiencer: 'like hours'", 212, 222), (2310, "Experiencer: 'two'", 1035,1038), (2310, "Experiencer: 'Anakin'", 1560, 1566), (2310, "Experiencer: ' '", 1619, 1620), (2310, "Experiencer: 'Tatooine'", 1726, 1734), (2310, "Experiencer: 'Anakin'", 1775, 1781), (2310, "Experiencer: 'Master Qui-Gon'", 1863, 1877), (2310, "Experiencer: 'half'", 1883, 1887), (2310, "Experiencer: 'One'", 2114, 2117), (2310, "Experiencer: 'Anakin'", 2180, 2186), (2310, "Stimulus: 'One'", 2484, 2487), (2310, "Stimulus: 'Anakin'", 2564, 2570), (2310, "Stimulus: 'Padme'", 2739, 2744)]
right_index = bisect(l, (2310, "F")) # "F" comes between "Experiencer" and "Status"
lower, higher = l[right_index-1], l[right_index]
print(lower, higher, sep="\n")
# (2310, "Experiencer: 'Anakin'", 2180, 2186)
# (2310, "Stimulus: 'One'", 2484, 2487)
产生
from bisect import bisect
def get_boundary(l): # This assumes all lists in your dict have at least 2 items
if len(l) < 2:
return l
right_index = bisect(l, (l[0][0], "F"))
return [l[right_index-1], l[right_index]]
print({key: get_boundary(value) for key, value in d.items()})
答案 4 :(得分:0)
也许最简单的方法是
2310
在此列表中的位置;您应该介于两个元素之间。这些是您想要的两个元素。