python - 在Python3中分析和计算列表列表中的新值

标签 python list updating

好吧,我会尽力正确地解释自己:

我正在使用 python3,并在结构中使用列表列表:

[[position, color, counts],...]

结果首先按颜色排序,然后按位置排序。

如果计数和位置的平均值具有相同的颜色并且它们之间的位置最多为 +-2,我需要将它们合并起来。

输入的简短测试示例是:

[ [1, "red", 3],  [2, "red", 2],  [3, "red", 3], [5, "red", 1], [3, "green", 9],  [10, "green", 4] ]

预期的输出:

[ [2.75, "red", 9], [3, "green", 9], [10, "green", 4]

我对像 5“红色”1 这样的情况特别有问题,因为如果我执行平均值,则距离可能会增加,从而导致迭代失败,但我想考虑它,因为它位于 5 个“红色”1 的 2 个位置上一篇...

有办法解决吗?

提前致谢!

最佳答案

我想我已经解决了你的问题。此代码片段应该可以工作,但可能可以优化:

colors = [ [1, "red", 3], [2, "red", 2], [3, "red", 3], [5, "red", 1], [3, "green", 9], [10, "green", 4] ]

def avg(list): 
    return sum(list) / float(len(list))

def process(colors, threshold=2): 
    colors_combined = {}
    colors_processed = []

    # sort colors by their name
    for color in colors: 
        position, color_name, count = color

        if color_name not in colors_combined.keys(): 
            colors_combined[color_name] = []

        colors_combined[color_name].append([position, count])

    # print colors_combined

    # process the data
    for color in colors_combined.keys(): 
        data = colors_combined[color]

        if len(data) == 1: # there can't be a case, where len(data) = 0
            colors_processed.append([data[0], color, data[1]])
        else: # more than 1 positions to check
            last_position = data[0][0]
            positions = [last_position]
            count_combined = data[0][1]

            for element in data[1:]: 
                if abs(last_position - element[0]) <= threshold: # element is inside of the distance
                    positions.append(element[0])
                    count_combined += element[1]
                else: 
                    colors_processed.append([avg(positions), color, count_combined])
                    positions = [element[0]]
                    count_combined = element[1]

                last_position = element[0]

            if len(positions) > 0: # the last processed elements where inside the distance, but not added
                colors_processed.append([avg(positions), color, count_combined])

    return colors_processed

print process(colors)

输出如下所示:

[[3.0, 'green', 9], [10.0, 'green', 4], [2.75, 'red', 9]]

如果您需要排序结果,可以添加颜色排序而不是 colors_combined.keys()

关于python - 在Python3中分析和计算列表列表中的新值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28965235/

相关文章:

适用于 Windows 的 python 启动器 : how to set python executable?

Python .sort() 没有对数字进行完全排序

python - 使用来自列表列表的键/值创建字典

Python 我如何使列表追加/扩展更快?

javascript - ng 类在值更改时未正确更新

mysql - 本地主机和服务器之间mysql的合并设计?

python - 使用 pythonwhois 测试域名可用性

python - dict可以在父进程和子进程之间共享吗?

python - 界定 ModelChoiceField 中的选择

c - 在 C 中运行时打印变量的值