Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/296.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何将两个列表中的每个元素与其他元素进行比较,然后将结果合并为字符串_Python_Python 3.x_List - Fatal编程技术网

Python 如何将两个列表中的每个元素与其他元素进行比较,然后将结果合并为字符串

Python 如何将两个列表中的每个元素与其他元素进行比较,然后将结果合并为字符串,python,python-3.x,list,Python,Python 3.x,List,我使用Python 3.7.3 我有两个整数列表,a和b,现在我想把值保存在一个字符串中,它是每个元素和其他元素之间比较的结果。我想对两个列表都这样做,然后合并结果。例如: a = [1, 9 ,4, 5] b = [89, 43, 10, 0] result_list = [9, 5, 89, 10] result_string = 958910 result_sum = 113 警告:事情会变得复杂 a = [1, 9, 4, 5] b = [89, 43, 10, 0] 在某

我使用Python 3.7.3

我有两个整数列表,a和b,现在我想把值保存在一个字符串中,它是每个元素和其他元素之间比较的结果。我想对两个列表都这样做,然后合并结果。例如:

a = [1, 9 ,4, 5]

b = [89, 43, 10, 0]

result_list = [9, 5, 89, 10]

result_string = 958910

result_sum = 113
警告:事情会变得复杂

a = [1, 9, 4, 5]

b = [89, 43, 10, 0]
在某个我不知道的操作之后,从a和b创建了一个字符串(下面只是一个结果的示例,这些值不适合上面显示的列表):

字符串x中的第一个数字和“~”后面的其他数字表示元素的索引,之后(直到下一个“~”)显示与其他每个元素的比较值。让我们调用索引指向“当前元素”的元素(复数,因为它指向两个列表的索引,元素数量相同)

减号后面的数字(中间的空格不重要,可以省略),表示与当前元素进行比较的元素的索引。“:”后面的两个字符用逗号分隔,表示a和b的当前元素(逗号前的字符表示a中的当前元素,逗号后的字符表示b中的当前元素)是否小于或大于与当前元素进行比较的元素。“l”代表“当前元素大于它被比较的元素”,而h(我不太确定为什么选择“h”)代表“当前元素小于它被比较的元素”。请记住,这两个列表之间没有任何操作发生,只有在它们内部。比较值之间的加号仅用于分隔它们。在“~”之后,循环重复,但当前元素向前移动一个索引(直到没有元素可以向前移动)。重要的是,在字符串中,比较值是经过排序的,因此这就像从第一个索引到最后一个索引计数一样,总是忽略当前元素(因为,很明显,将一个元素与自己进行比较是无用的,如果不是这样排序的话,整个字符串将是无用的)(f.e.如果当前元素的索引为2:“2-0:l,h+1:h,l+3:l,h”)

另外:我忘了提到,如果当前元素和它所比较的元素(或“比较”,英语不是我的母语)大小相同,那么应该在其中加一个“s”,而不是放一个“l”或“h”

我的python技能还不足以找到用上述数据创建字符串的方法。有人能告诉我如何从两个整数列表中获得这样的字符串吗


基本上,我想从两个列表中提取出尽可能多的信息,用于AI项目,然后将其放入字符串中,这样我的程序中的某个函数就可以轻松地遍历它并提取信息。

我可能误解了你的问题,但我的看法是这样的

如果要将两个列表作为输入,请对它们进行比较,以便只有大于/小于的值“存活”,例如:

a = [1, 9 ,4, 5]

b = [89, 43, 10, 0]

result_list = [9, 5, 89, 10]

result_string = 958910

result_sum = 113
那么下面的代码(免责声明:我是Python的初学者)可能是合适的:

    # Question by John Plattenborough

    a = [1, 9, 4, 5]
    b = [89, 43, 10, 0]


    def convert_to_string(list):
        result = "".join(map(str, list))
        return result


    def sum_of_list(list):
        sum = 0
        for i in range(len(list)):
            sum += list[i]
        return sum


    def greatest_values(list_1, list_2):
        """
        :param list_1: A list of integers
        :param list_2: A list of integers
        :return: A list that contains the greatest values out of every two pairs in both of the lists
        """
        result = []
        for i in range(0, len(list_1), 2):
            if list_1[i] > list_1[i + 1]:
                result.append(list_1[i])
            elif list_1[i] < list_1[i + 1]:
                result.append(list_1[i + 1])
            else:
                # If they're equal
                result.append(list_1[i])
        for i in range(0, len(list_2), 2):
            if list_2[i] > list_2[i + 1]:
                result.append(list_2[i])
            elif list_2[i] < list_2[i + 1]:
                result.append(list_2[i + 1])
            else:
                # If they're equal
                result.append(list_2[i])
        return result


    result_list = greatest_values(a, b)
    result_string = convert_to_string(result_list)
    result_sum = sum_of_list(result_list)
    print(result_list)  # [9, 5, 89, 10]

    print(result_string)  # 958910

    print(result_sum)  # 113
#约翰·普拉滕伯勒的问题
a=[1,9,4,5]
b=[89,43,10,0]
def转换为字符串(列表):
结果=“加入(映射(str,列表))
返回结果
定义列表的总和(列表):
总和=0
对于范围内的i(len(列表)):
总和+=列表[i]
回报金额
def最大_值(列表_1、列表_2):
"""
:param list_1:整数列表
:param list_2:整数列表
:return:两个列表中每两对中包含最大值的列表
"""
结果=[]
对于范围内的i(0,len(列表1),2):
如果列表_1[i]>列表_1[i+1]:
result.append(列表_1[i])
elif list_1[i]列表_2[i+1]:
result.append(列表_2[i])
elif list_2[i]

如果我没有完全理解你的问题,约翰,请让我知道。

我可能误解了你的问题,但以下是我的看法

如果要将两个列表作为输入,请对它们进行比较,以便只有大于/小于的值“存活”,例如:

a = [1, 9 ,4, 5]

b = [89, 43, 10, 0]

result_list = [9, 5, 89, 10]

result_string = 958910

result_sum = 113
那么下面的代码(免责声明:我是Python的初学者)可能是合适的:

    # Question by John Plattenborough

    a = [1, 9, 4, 5]
    b = [89, 43, 10, 0]


    def convert_to_string(list):
        result = "".join(map(str, list))
        return result


    def sum_of_list(list):
        sum = 0
        for i in range(len(list)):
            sum += list[i]
        return sum


    def greatest_values(list_1, list_2):
        """
        :param list_1: A list of integers
        :param list_2: A list of integers
        :return: A list that contains the greatest values out of every two pairs in both of the lists
        """
        result = []
        for i in range(0, len(list_1), 2):
            if list_1[i] > list_1[i + 1]:
                result.append(list_1[i])
            elif list_1[i] < list_1[i + 1]:
                result.append(list_1[i + 1])
            else:
                # If they're equal
                result.append(list_1[i])
        for i in range(0, len(list_2), 2):
            if list_2[i] > list_2[i + 1]:
                result.append(list_2[i])
            elif list_2[i] < list_2[i + 1]:
                result.append(list_2[i + 1])
            else:
                # If they're equal
                result.append(list_2[i])
        return result


    result_list = greatest_values(a, b)
    result_string = convert_to_string(result_list)
    result_sum = sum_of_list(result_list)
    print(result_list)  # [9, 5, 89, 10]

    print(result_string)  # 958910

    print(result_sum)  # 113
#约翰·普拉滕伯勒的问题
a=[1,9,4,5]
b=[89,43,10,0]
def转换为字符串(列表):
结果=“加入(映射(str,列表))
返回结果
定义列表的总和(列表):
总和=0
对于范围内的i(len(列表)):
总和+=列表[i]
回报金额
def最大_值(列表_1、列表_2):
"""
:param list_1:整数列表
:param list_2:整数列表
:return:两个列表中每两对中包含最大值的列表
"""
结果=[]
对于范围内的i(0,len(列表1),2):
如果列表_1[i]>列表_1[i+1]:
result.append(列表_1[i])
['0 - 1:l,l + 2:l,l + 3:l,h',
 '1 - 0:h,l + 2:h,l + 3:h,h',
 '2 - 0:h,l + 1:l,l + 3:l,h',
 '3 - 0:h,l + 1:l,l + 2:h,l']
' ~ '.join(comps)
'0 - 1:l,l + 2:l,l + 3:l,h ~ 1 - 0:h,l + 2:h,l + 3:h,h ~ 2 - 0:h,l + 1:l,l + 3:l,h ~ 3 - 0:h,l + 1:l,l + 2:h,l'