Python 在字典中的值列表中选择元素
我有一个字典,其中的值是包含2个或更多元素的列表。 我想根据列表中只有2个元素的其他值,在那些包含2个以上元素的值中进行切片 我知道我可以将所有的值元素附加到一个列表中,然后只计算最常见的值,但是我需要保留有关键和字典格式的信息,所以它实际上不起作用。我似乎不知道如何解决这个问题 我的字典是这样的Python 在字典中的值列表中选择元素,python,list,dictionary,Python,List,Dictionary,我有一个字典,其中的值是包含2个或更多元素的列表。 我想根据列表中只有2个元素的其他值,在那些包含2个以上元素的值中进行切片 我知道我可以将所有的值元素附加到一个列表中,然后只计算最常见的值,但是我需要保留有关键和字典格式的信息,所以它实际上不起作用。我似乎不知道如何解决这个问题 我的字典是这样的 start_dict = { 'Key1': [243928620, 243938319], 'Key2': [243928620, 243938319], 'Key3': [
start_dict = {
'Key1': [243928620, 243938319],
'Key2': [243928620, 243938319],
'Key3': [243928620, 243931757, 243938319],
'Key4': [243928620, 243938319, 243938323],
'Key5': [243928634, 243938316],
'Key6': [243928620, 243938319],
'Key7': [243928634, 243938317],
'Key8': [243928620, 243938329,243938387]
}
我想保持所有值列表中的元素1不变,因为它是一个开始坐标,其余的是给定间隔的潜在结束坐标
然后,对于那些列表中有2个以上元素的值(键3、4和8),我希望将该元素保留在其他键的其他值列表中最频繁的值列表中,这是键3和4的情况,因为它们都包含最频繁的端点坐标243938319
如果它们在其他任何一个中都不存在,我将保留它们,这是键8的情况
所有按键中最常用的值是起始位置243928620和结束位置243938319。所以理想的输出是
start_dict = {
'Key1': [243928620, 243938319],
'Key2': [243928620, 243938319],
'Key3': [243928620, 243938319],
'Key4': [243928620, 243938319],
'Key5': [243928634, 243938316],
'Key6': [243928620, 243938319],
'Key7': [243928634, 243938317],
'Key8': [243928620, 243938329,243938387]
}
我似乎不知道如何才能做到这一点,如果它能以一种聪明的方式做到的话
你们谁能帮忙?谢谢您的时间。这是一种方法:
from collections import Counter
from pprint import pprint
def reduce_coords(data):
# Counter of second list element for 2-element lists
count = Counter(v[1] for v in data.values() if len(v) == 2)
# Result dict
result = {}
# Iterate data entries
for k, v in data.items():
# Modify lists longer than two with at least one element in the counter
if len(v) > 2 and any(elem in count for elem in v[1:]):
# Replace list with first element and following element with max count
v = [v[0], max(v[1:], key=lambda elem: count.get(elem, 0))]
# Add to result
result[k] = v
return result
start_dict = {
'Key1': [243928620, 243938319],
'Key2': [243928620, 243938319],
'Key3': [243928620, 243931757, 243938319],
'Key4': [243928620, 243938319, 243938323],
'Key5': [243928634, 243938316],
'Key6': [243928620, 243938319],
'Key7': [243928634, 243938317],
'Key8': [243928620, 243938329,243938387]
}
pprint(reduce_coords(start_dict))
# {'Key1': [243928620, 243938319],
# 'Key2': [243928620, 243938319],
# 'Key3': [243928620, 243938319],
# 'Key4': [243928620, 243938319],
# 'Key5': [243928634, 243938316],
# 'Key6': [243928620, 243938319],
# 'Key7': [243928634, 243938317],
# 'Key8': [243928620, 243938329, 243938387]}
我更喜欢另一个答案,但这仍然可以教你一些关于列表理解的东西 # 创建所有端点的dic列表:
startpoints = {k:v[0] for k,v in start_dict.items()}
endpoints = {k:v[1:] for k,v in start_dict.items()}
from collections import Counter
c = Counter(endpoints_flatten)
然后将其展平:
endpoints_flatten = [value for list in endpoints.values() for value in list]
创建包含所有端点的计数器:
startpoints = {k:v[0] for k,v in start_dict.items()}
endpoints = {k:v[1:] for k,v in start_dict.items()}
from collections import Counter
c = Counter(endpoints_flatten)
创建一个函数,为您提供列表中最常见的功能:
def most_com(list_endpoint,c):
return max(list_endpoint, key=lambda l : c[l])
现在浏览端点列表,只保留最常见的端点:
common_endpoint = {k:most_com(list_endpoint,c) for k,list_endpoint in endpoints.items()}
现在输出所有信息:
output = {k:v + common_endpoint[k] for k,v in startpoints.items()}
关于存储此信息的不同结构:
start_dict = {
'Key1': [243928620, 243938319],
'Key2': [243928620, 243938319],
'Key3': [243928620, 243931757, 243938319],
'Key4': [243928620, 243938319, 243938323],
'Key5': [243928634, 243938316],
'Key6': [243928620, 243938319],
'Key7': [243928634, 243938317],
'Key8': [243928620, 243938329,243938387]
}
modified_dict = {k:{"start":v[0], "end":v[1:]} for k, v in start_dict.items()}
print(modified_dict)
#Output:
{'Key1': {'start': 243928620, 'end': [243938319]},
'Key2': {'start': 243928620, 'end': [243938319]},
'Key3': {'start': 243928620, 'end': [243931757, 243938319]},
'Key4': {'start': 243928620, 'end': [243938319, 243938323]},
'Key5': {'start': 243928634, 'end': [243938316]},
'Key6': {'start': 243928620, 'end': [243938319]},
'Key7': {'start': 243928634, 'end': [243938317]},
'Key8': {'start': 243928620, 'end': [243938329, 243938387]}}
#collect all possible end points for every key, and combine in a list
end_points = []
for k, v in modified_dict.items():
end_points.extend(v["end"])
#find the most common end point
from collections import Counter
most_common = Counter(end_points).most_common(1)[0][0]
#Adjust the end points if the most common end point is found
for k, v in modified_dict.items():
if most_common in v["end"]:
modified_dict[k]["end"] = [most_common]
print(modified_dict)
#Output:
{'Key1': {'start': 243928620, 'end': [243938319]},
'Key2': {'start': 243928620, 'end': [243938319]},
'Key3': {'start': 243928620, 'end': [243938319]},
'Key4': {'start': 243928620, 'end': [243938319]},
'Key5': {'start': 243928634, 'end': [243938316]},
'Key6': {'start': 243928620, 'end': [243938319]},
'Key7': {'start': 243928634, 'end': [243938317]},
'Key8': {'start': 243928620, 'end': [243938329, 243938387]}}
事实上,如果多个值是唯一的,我的就不会保留它,尽管它不会保留多个唯一的值。这仍然是展示如何结合列表理解和词典的一种有用的方式。谢谢。谢谢。这是一个非常好的解决方案。有一个很好的柜台和lambda合并。谢谢你的建议。我从来没有想过有一个dicts的dicts,我可以看到它可以派上用场,因为我有很多不同的坐标,所以这提供了一个很好的概述!