截断字典列表值

Truncate dictionary list values

我正在尝试在字典中查找匹配值的键。但是要获得任何类型的有效匹配,我需要截断列表中的值。

我想截断小数点(例如“2.21”到“2.2”)。

dict1 = {'red':[1.98,2.95,3.83],'blue':[2.21,3.23,4.2333],'orange':[3.14,4.1,5.22]}

dict2 = {'green':[3.11,4.12,5.2],'yellow':[2.2,3.2,4.2],'red':[5,2,6]}

matches = []
for key1 in dict1:
    for key2 in dict2:
        if dict1[key1] == dict2[key2]:
            matches.append((key1, key2))

print(matches)

我正在尝试让 "green""orange" 以及 "blue""yellow" 应该匹配。但我不确定是否需要先解析每个值列表,进行更改,然后继续。如果我可以对比较本身进行更改,那将是理想的。

d = {'green': [3.11, 4.12, 5.2]}

>>> map(int, d['green'])
[3, 4, 5]

您需要在比较之前将列表项映射为整数

for key2 in dict2:
    if map(int, dict1[key1]) == map(int, dict2[key2]):
        matches.append((key1, key2))

我假设你想四舍五入。如果要四舍五入到最接近的整数,请使用 round 而不是 int

您可以在列表上使用 zip 并比较值,但您应该设置一个公差 tol,两个列表中的一对值将被视为相同:

dict1 = {'red':[1.98,2.95,3.83],'blue':[2.21,3.23,4.2333],'orange':[3.14,4.1,5.22]}
dict2 = {'green':[3.11,4.12,5.2],'yellow':[2.2,3.2,4.2],'red':[5,2,6]}

matches = []
tol = 0.1 # change the tolerance to make comparison more/less strict
for k1 in dict1:
    for k2 in dict2:
        if len(dict1[k1]) != len(dict2[k2]):
            continue
        if all(abs(i-j) < tol for i, j in zip(dict1[k1], dict2[k2])):
            matches.append((k1, k2))

print(matches)
# [('blue', 'yellow'), ('orange', 'green')]

如果您的列表长度始终相同,您可以删除跳过不匹配长度的部分。

您可以在循环之间添加一些列表理解,将浮点数转换为整数,如下所示:

dict1 = {'red':[1.98,2.95,3.83],'blue':[2.21,3.23,4.2333],'orange':[3.14,4.1,5.22]}

dict2 = {'green':[3.11,4.12,5.2],'yellow':[2.2,3.2,4.2],'red':[5,2,6]}

matches = []
for key1 in dict1:
    for key2 in dict2:
        dict1[key1] = [int(i) for i in dict1[key1]]
        dict2[key2] = [int(i) for i in dict2[key2]]

        if dict1[key1] == dict2[key2]:
            matches.append((key1, key2))
print(matches)

输出:

[('blue', 'yellow'), ('orange', 'green')]

希望对您有所帮助:)

如果您只关心截断而不是实际舍入,则可以将值转换为字符串并切掉多余部分:

for key1 in dict1:
    for key2 in dict2:
        # I'm pulling these values out so you can look at them:
        a = str(dict1[key1])
        b = str(dict1[key2])
        # find the decimal and slice it off
        if a[:a.index('.')] == b[:b.index('.')]:
            matches.append((key1, key2))

如果你想真正舍入,使用内置的round(float, digits)(https://docs.python.org/2/library/functions.html):

for key1 in dict1:
    for key2 in dict2:
        if round(dict1[key1],0) == round(dict2[key2], 0):
            matches.append((key1, key2))

(另外,注意你的缩进!)