根据其他列表中的索引打印列表元素的最小整数

时间:2018-04-08 20:22:41

标签: python arrays list integer python-3.5

我有两个列表a和b(两者都有相同的长度),其中列表b只有1,2和3作为其元素(允许随机重复)。现在,对于列表中的1的所有索引' b'我想从列表a中打印一个最小整数。对于列表中的2和3,它是相同的' b'。例如:

      a = [10,33,42,17,9,32,33,43,12,2,5,22]
      b = [3,1,3,2,1,3,2,1,3,1,2,2]

由于1(在列表' b')中的索引分别是1,4,7,9我想从列表&#39打印最小整数(例如答案为2) ;一个'在这些指数。对于列表' b'中的2和3也应该这样做。此外,如果可以从列表中计算出1,2和3的全部内容' b'那么请告诉。

预期输出(根据示例):

The min integer for 1, 2 and 3 are 2, 5 and 10 respectively.

这里的a和b只是一个例子。请回答a和b是否作为用户输入,b具有相同的约束,即b只能包含1,2和3(随机重复)。

3 个答案:

答案 0 :(得分:0)

你想要这样的东西:

a = [10,33,42,17,9,32,33,43,12,2,5,22]
b = [3,1,3,2,1,3,2,1,3,1,2,2]

low = min(b)
high = max(b)

minimum = [min([a[i] for i in range(len(b)) if b[i]==n]) for n in range(low,high+1)]
output = "".join(["The min integer for ", ", ".join(map(str,range(low,high-1))), " and ", str(high), " are ", ", ".join(map(str,minimum[:-1])), " and ", str(minimum[-1]), " respectively."])
print(output)

输出:

The min integer for 1 and 3 are 2, 5 and 10 respectively.

<强>更新

如果您希望用户输入b的数字并检查它们是1,2或3,您可以这样做:

def enterInput(b, length):
    print("List length: ", length)
    for i in range(length):
        while True:
            elem = int(input("Enter element " + str(i) + ": "))
            if not elem in [1,2,3]:
                print("Invalid input. Please try again.")
            else:
                b.append(elem)
                break

a = [10,33,42,17,9,32,33,43,12,2,5,22]
b = []

length = len(a)
enterInput(b, length)

然后进行上述计算。

<强>更新

对于列表a,您可以执行以下操作:

def enterInputNumbers(a):
    while True:
        length = int(input("Enter list's length: "))
        if length > 0:
            break
        else:
            print("Invalid list length. Please try again.")

    for i in range(length):
        elem = int(input("Enter element " + str(i) + ": "))
        a.append(elem)

所以,整个代码将是:

def enterInputNumbers(a):
    while True:
        length = int(input("Enter list's length: "))
        if length > 0:
            break
        else:
            print("Invalid list length. Please try again.")

    for i in range(length):
        elem = int(input("Enter element " + str(i) + ": "))
        a.append(elem)

def enterInput(b, length):
    print("List length: ", length)
    for i in range(length):
        while True:
            elem = int(input("Enter element " + str(i) + ": "))
            if not elem in [1,2,3]:
                print("Invalid input. Please try again.")
            else:
                b.append(elem)
                break

#a = [10,33,42,17,9,32,33,43,12,2,5,22]
#b = [3,1,3,2,1,3,2,1,3,1,2,2]
a = []
b = []

enterInputNumbers(a)
length = len(a)
enterInput(b, length)

low = min(b)
high = max(b)

minimum = [min([a[i] for i in range(len(b)) if b[i]==n]) for n in range(low,high+1)]
output = "".join(["The min integer for ", ", ".join(map(str,range(low,high-1))), " and ", str(high), " are ", ", ".join(map(str,minimum[:-1])), " and ", str(minimum[-1]), " respectively."])
print(output)

答案 1 :(得分:0)

如果根据b中的相应项目对元素进行分组,则更容易理解。以下是不进行硬编码的通用算法。您可以传递b的任何值,它将返回a中的最小值。

from collections import defaultdict

def min_el(a, b):
  groups = defaultdict(list)
  for el_a, el_b in zip(a, b):
    groups[el_b].append(el_a)
  return groups.keys(), [min(groups[key]) for key in groups.keys()]

a = [10,33,42,17,9,32,33,43,12,2,5,22]
b = [3,1,3,2,1,3,2,1,3,1,2,2]
print(min_el(a, b)) # ([1, 2, 3], [2, 5, 10])

答案 2 :(得分:0)

你可以尝试这种方法:

a = [10, 33, 42, 17, 9, 32, 33, 43, 12, 2, 5, 22]
b = [3, 1, 3, 2, 1, 3, 2, 1, 3, 1, 2, 2]

min_value={}
for j,i in enumerate(b):
    if i not in min_value:
        min_value[i]=[a[j]]
    else:
        min_value[i].append(a[j])

print({i:min(j) for i,j in min_value.items()})

输出:

{1: 2, 2: 5, 3: 10}