根据其他列表的索引打印列表元素的最小整数
Print minimum integer of list's elements based on indices from other list
我有两个列表 a 和 b(都具有相同的长度),其中列表 b 只有 1、2 和 3 作为其元素(允许随机重复)。现在,对于列表中 1 的所有索引 'b' 我想从列表 a.And 中打印一个最小整数,与列表 'b' 中的 2 和 3 相同。例如:
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 我想从列表 [=25= 中打印最小整数(例如答案是 2) ] 在这样的指数。对列表 'b' 中的 2 和 3 也应执行相同的操作。另外,如果可以从列表 'b' 中计算出 1、2 和 3,那么请告诉我。
预期输出(根据示例):
The min integer for 1, 2 and 3 are 2, 5 and 10 respectively.
这里的a和b只是一个例子。请回答是否将 a 和 b 作为用户输入并且 b 具有相同的约束,即 b 只能包含 1、2 和 3(随机重复)。
你想要这样的东西:
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)
按照b
中的对应项对元素进行分组更容易理解。以下是不硬编码的通用算法。您可以为 b
传入任何值,它将 return 对应于 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])
你可以试试这个方法:
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}
我有两个列表 a 和 b(都具有相同的长度),其中列表 b 只有 1、2 和 3 作为其元素(允许随机重复)。现在,对于列表中 1 的所有索引 'b' 我想从列表 a.And 中打印一个最小整数,与列表 'b' 中的 2 和 3 相同。例如:
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 我想从列表 [=25= 中打印最小整数(例如答案是 2) ] 在这样的指数。对列表 'b' 中的 2 和 3 也应执行相同的操作。另外,如果可以从列表 'b' 中计算出 1、2 和 3,那么请告诉我。
预期输出(根据示例):
The min integer for 1, 2 and 3 are 2, 5 and 10 respectively.
这里的a和b只是一个例子。请回答是否将 a 和 b 作为用户输入并且 b 具有相同的约束,即 b 只能包含 1、2 和 3(随机重复)。
你想要这样的东西:
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)
按照b
中的对应项对元素进行分组更容易理解。以下是不硬编码的通用算法。您可以为 b
传入任何值,它将 return 对应于 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])
你可以试试这个方法:
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}