简体   繁体   English

包含整数和浮点数的元组列表上的数学运算

[英]Mathematical operations on list of tuples containing integers and floats

This is a follow up of my previous question where I forgot one part of my problem.这是我之前的问题的后续,我忘记了我的问题的一部分。 I have the function below which aims to add elements from one list into another until the list is only 0's.我有下面的函数,旨在将一个列表中的元素添加到另一个列表中,直到列表只有 0。 The function works as intended and could probably use some optimisations but that's something I'll try later.该函数按预期工作,可能会使用一些优化,但我稍后会尝试这样做。

My current problem is that I have to somehow keep the function working as it does but instead of calculating values with the floating point numbers in the list, I need to do so with the floating point numbers in the tuples in the lists.我目前的问题是我必须以某种方式保持函数正常工作,而不是使用列表中的浮点数计算值,我需要使用列表中元组中的浮点数来计算值。 For some background information, the int in the tuple is the processID (this code will run in a multiprocessing project) and the second value is a simple float calculated from various functions.对于一些背景信息,元组中的 int 是 processID(此代码将在多处理项目中运行),第二个值是从各种函数计算的简单浮点数。

The lists should look something like this as an example:例如,列表应如下所示:

example_list = [(12345, -0.561432), (23456, -0.861423)]

Here is the function as a working code with example lists containing only floating point numbers (not tuples like I need).这是作为工作代码的函数,示例列表仅包含浮点数(不是我需要的元组)。

def cancelOut(deficit_list, trade_1_list, trade_2_list):

    lengths = [len(deficit_list), len(trade_1_list), len(trade_2_list)]
    # List of the lists of positive values
    positive_lists = [trade_1_list, trade_2_list]

    if(len(deficit_list) != 0): # Check deficit_list isn't empty

        total_positive = lengths[1] + lengths[2]
        current_positive = 0

        # Set all indexes to 0 to start
        deficit_list_index = 0
        trade_index = 0
        trade_lists_index = 0

        # While new_deficit_list contains a value different from 0 do the following
        while not all(value == 0 for value in deficit_list):
            # Determine the difference between the current deficit_list value and current positive value of the current list
            value = deficit_list[deficit_list_index] + positive_lists[trade_lists_index][trade_index]
            if(value > 0):
                positive_lists[trade_lists_index][trade_index] = value
                deficit_list[deficit_list_index] = 0
                deficit_list_index += 1
            elif(value == 0):
                deficit_list[deficit_list_index] = 0
                positive_lists[trade_lists_index][trade_index] = 0
                deficit_list_index += 1
                trade_index += 1
                current_positive += 1
            elif(value < 0):
                deficit_list[deficit_list_index] = value
                positive_lists[trade_lists_index][trade_index] = 0
                current_positive += 1
                if(trade_index == (lengths[trade_lists_index + 1] - 1)):
                    trade_index = 0
                    trade_lists_index = 1
                else:
                    trade_index += 1

            if(trade_lists_index == 1 and current_positive == total_positive):
                break

    return [deficit_list, trade_1_list, trade_2_list]

if __name__ == "__main__":
    deficit_list_values = [-0.246497, -0.341068]
    positive_values_1 = [0.022148, 0.212573, 0.100531]
    positive_values_2 = [0.281474]
    lists = cancelOut(deficit_list_values, positive_values_1, positive_values_2)
    for i in range(len(lists)):
        print(lists[i])

I feel that the issue I'm running into is the fact that the tuples contain floats and ints.我觉得我遇到的问题是元组包含浮点数和整数。

使用以下命令(python 3 原生)

list1, list2 = map(list, zip(*list_of_tuples))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM