Python从两个具有相同长度的列表中获取唯一值和最小值对
Posted
技术标签:
【中文标题】Python从两个具有相同长度的列表中获取唯一值和最小值对【英文标题】:Python getting the unique and smallest value pairs from two lists with same length 【发布时间】:2020-02-06 02:28:47 【问题描述】:我有两个列表:
index = [1,1,1,1,2,2,2,2,3,4,5,5,5,6,7,8,9,10,10,10]
value = [2,3,2,1,2,4,6,8,2,1,5,2,7,2,2,2,1,55,1,11]
相同的长度,但我想要的回报是索引列表中的唯一数字和根据索引的值列表的最小值
结果应该是这样的:
index_result = [1,2,3,4,5,6,7,8,9,10]
value_result = [1,2,2,1,2,2,2,2,1,1]
我试过了:
index = [1,1,1,1,2,2,2,2,3,4,5,5,5,6,7,8,9,10,10,10]
value = [2,3,2,1,2,4,6,8,2,1,5,2,7,2,2,2,1,55,1,11]
index_result = []
value_result = []
#global small_value
j = 0
while j < len(index):
if j == 0:
try:
if index[j] == index[j+1]:
small_value = min(value[j],value[j+1])
elif index[j] != index[j+1]:
index_result.append(index[j])
value_result.append(value[j])
except IndexError as e:
print(e)
pass
j = j + 1
print('small value is for index j ==0')
print(small_value)
elif j <len(index) - 1:
try:
# if index[j] == index[j-1]:
# small_value = min(value[j],value[j-1])
if index[j] != index[j+1] and index[j] != index[j-1]:
index_result.append(index[j])
value_result.append(value[j])
elif index[j] != index[j+1] and index[j] == index[j-1]:
index_result.append(index[j])
value_result.append(small_value)
except IndexError as e:
print(e)
pass
j = j + 1
print('small value is for index 0 < j <len(index)')
print(small_value)
elif j == len(index) - 1:
try:
if index[j] == index[j-1]:
small_value = min(value[j],value[j-1])
index_result.append((index[j]))
value_result.append(small_value)
elif index[j] != index[j-1]:
index_result.append(index[j])
value_result.append(value[j])
except IndexError as e:
print(e)
pass
j = j + 1
print('small value is for j = len(index) - 1')
print(small_value)
print (index_result)
print (value_result)
结果接近预期但仍然错误:
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10] [2, 2, 2, 1, 2, 2, 2, 2, 1, 1]
【问题讨论】:
...and smallest value of the value list according to the index
是什么意思?
pd.DataFrame('value': value, 'index':index).groupby('index').min()
?
是的,答案相同,@QuangHoang
@JerryM。对不起我的英语,但这意味着:1.索引列表应该只有唯一的没有多余的数字2.值列表应该从与索引列表配对的值池中返回最小值,例如:对于两个灯:索引[1, 1,2] 和 [3,4,6],结果应该是索引 [1,2] 和 [3,6]
@QuangHoang 我可以拥抱你吗!?非常感谢
【参考方案1】:
既然你标记了熊猫
pd.DataFrame([index,value]).T.sort_values([0,1]).drop_duplicates(0)
0 1
3 1 1
4 2 2
8 3 2
9 4 1
11 5 2
13 6 2
14 7 2
15 8 2
16 9 1
18 10 1
【讨论】:
【参考方案2】:我认为这就是你想要实现的目标
import math
indices = [1,1,1,1,2,2,2,2,3,4,5,5,5,6,7,8,9,10,10,10]
values = [2,3,2,1,2,4,6,8,2,1,5,2,7,2,2,2,1,55,1,11]
result = [math.inf] * max(indices)
for i, v in zip(indices, values):
result[i-1] = min(result[i-1], v) # Apparently 1 based indexing
print(result)
[1, 2, 2, 1, 2, 2, 2, 2, 1, 1]
【讨论】:
【参考方案3】:如果您的index
像示例中一样单调递增,您可以使用itertools.groupby
尝试这个python 解决方案
from itertools import groupby
d = k: min([x[1] for x in g]) for k, g in groupby(zip(index, value),
lambda x: x[0])
In [95]: d
Out[95]: 1: 1, 2: 2, 3: 2, 4: 1, 5: 2, 6: 2, 7: 2, 8: 2, 9: 1, 10: 1
index_result = list(d.keys())
Out[103]: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
value_result = list(d.values())
Out[105]: [1, 2, 2, 1, 2, 2, 2, 2, 1, 1]
【讨论】:
【参考方案4】:Numpy 解决方案:
index = np.array([1,1,1,1,2,2,2,2,3,4,5,5,5,6,7,8,9,10,10,10])
value = np.array([2,3,2,1,2,4,6,8,2,1,5,2,7,2,2,2,1,55,1,11])
[value[index == i].min() for i in np.unique(index)]
# [1, 2, 2, 1, 2, 2, 2, 2, 1, 1]
【讨论】:
你知道如何获取[1,2,3,4,5,6,7,8,9,10]索引列表吗?像 unique.function 一样? @DaweiZhanglist(dict.from_iterable(index))
以上是关于Python从两个具有相同长度的列表中获取唯一值和最小值对的主要内容,如果未能解决你的问题,请参考以下文章