Python: 从字典中附加一个带条件的列表。

问题描述 投票:0回答:1

我有一个字典,我想从其中筛选出结果并将其追加到另一个字典中。条件是,如果字典中的两个第一个元素之间的差值(例如31 - 30 = 1)小于5,则添加字典中相关的第二个元素,并将其追加到一个新的字典中,否则保持相同的第一个元素和相关的第二个元素。

a = {"20" : "1.5", "30" : "2.0", "31" : "1.0", "40" : "1", "50" : "1.5"}
listb = []
listc = []
newdict = {}
for key in a:
    b = key
    c = a[key]
    listb.append(b)
    listc.append(c)

for i in range(len(listb)):
    low = listb[i]
    high = listb[i+1]
    diff = int(high) - int(low)
#     print(low)
    if (diff > 5):
        num = listc[i]
#         print(num)
        num_a = listb[i]
#         print(num_a)
        newdict[[num_a][i]] = num
        print((newdict))
    else:
        num = listc[i] + listc[i+1]
        print(num)
        num_a = listb[i+1]
        print(num_a)
        newdict[[num_a][i]] = num
print(newdict)

这样的输出应该是这样的

a = {"20" : "1.5", "31" : "3.0", "40" : "1", "50" : "1.5"}

python pandas dictionary append
1个回答
0
投票

如果唯一的其他答案需要使用潘达斯,那么我觉得有必要提供一个替代方案(我讨厌潘达斯)。

这应该可以得到你所描述的内容。不过我目前还不能测试。

a = {"20" : "1.5", "30" : "2.0", "31" : "1.0", "40" : "1", "50" : "1.5"}
# your listb and listc are just a.keys() and a.values(). So I'm going to delete all of this listb listc setup stuff.
newdict = {}

skip = False  # This is a pretty brute force way to just check whether we've already accounted for the "next" value. Otherwise you will double count.

for i in range(len(a.keys())):
    if skip:
        skip = False
        continue
    low = a.keys()[i]
    high = a.keys()[i+1]
    diff = abs(int(high) - int(low)) # If "diff" is actually meant to be a diff, then we need to use abs
    if diff > 5:
        newdict[a.keys()[i]] = a.values()[i]
    else:
        newdict[a.keys()[i]] = a.values()[i] + a.values()[i+1]
        skip = True
print(newdict)

请注意,如果你有几个键在一排都是< 5相隔,这可能不会有预期的表现。从描述中也不清楚在键是,例如,40,44,48的情况下,你到底想要的是什么(40和44分组还是把所有3个数字分组?但根据你的描述,上面实现了它。


1
投票

由于你要将每个元素与它的 "之前 "或 "之后 "的元素进行比较,你希望使用一个有序的数据结构。由于字典只是 "插入有序 "的,你不能可靠地检查第一个项目和紧随其后的那个项目。所以,你可能想使用一个tuple的列表。我不太清楚你想做什么,但我试着用这段代码来解释它。我希望这能帮到你 :)

# Creating a as a list of tuples so that they are ordered
a = [(20, 1.5), (30, 2.0), (31, 1.0), (40, 1), (50, 1.5)]
new_list = []

# you looped through len(a), but you should loop through len(a) - 1 so that you don't get an index error
for i in range(len(a) - 1):
    # The first element of each tuple
    low_key = a[i][0]
    high_key = a[i+1][0]

    if high_key - low_key < 5:
        sum = a[i+1][1] + a[i][1]
        new_tuple = (high_key, sum)
        new_list.append(new_tuple)
    else:
        new_list.append((low_key, a[i][1]))
        # need to check if last element, bc only looping through len(a) - 1
        if i == len(a) - 1:
            new_list.append((high_key, a[i+1][1]))
print(new_list)

0
投票

一种方法是将第一项转换为 Pandas dataframe 并在那里进行计算,然后将其转换回字典?

d = {"20" : "1.5", "30" : "2.0", "31" : "1.0", "40" : "1", "50" : "1.5"}
df = pd.Series(d)
df = df.reset_index().astype(float)
df['id']= df['index'].diff().shift(-1).fillna(10).values
df = df[df['id']>5]
df = df.set_index(['index'])
df = df.drop('id', axis=1)
df.to_dict()
{0: {20.0: 1.5, 31.0: 1.0, 40.0: 1.0, 50.0: 1.5}}

0
投票

我不是很清楚你想做什么,但我想通过一些评论,你也许可以修正你的代码来实现你的目标,即使我不完全理解这个目标是什么。

dict本质上是未排序的,但我相信你的算法本质上要求键的顺序是递增的。

我会把第二行和第三行改成。

listb = sorted(a.keys())
listc = [a[k] for k in listb]

下一步你可能想循环到len(listb) -1,否则listb[I+1]就会出界。也许你可以检查出枚举函数,但那样你就需要检查是否在最后一次迭代,并进行相应的处理。

最后,你可以使用一些更好的变量名。a, listb, 和 listc 并没有真正传达出多少意义。即使是a、a_keys和a_values也会更容易理解,但如果能更好地描述a所代表的内容就更好了。

© www.soinside.com 2019 - 2024. All rights reserved.