查找两个列表中共同的最大数 #python

huangapple go评论66阅读模式
英文:

Find the Biggest Number which is common in BOTH lists #python

问题

我正在制作一个简化程序,用于找到可以被两个给定列表中相同数整除的最大数。我已经找到了部分方法... 我需要一种方法,可以从这两个给定列表中找到一个共同的值,它也是两个列表中的最大公共值。就像应该选择和返回给定列表中存在的最大数,我还没有写代码,我仍然在计划要做的事情。

示例:
list1 = [2,4,6,12]
list2 = [2,4,6,8,14]

我需要的答案是6,因为它是这两个列表中的最大公共数,不应该是2或4,因为它们不是最大值。希望我的解释足够清楚。谢谢。

英文:

So I am making a simplifying program to find the biggest number that can be divisible by the same number for both lists which I already found a way to partially do... I need a way so that I can find a common value from both the given lists which is also the highest common value in both the lists. Like the biggest number present in both the given lists should be selected and returned, I dont have a code yet Im still in the process of planning what to do

Example:
list1 = [2,4,6,12]
list2 = [2,4,6,8,14]

The answer I need is 6 as it is the highest common number from both of these lists, it shouldn't be 2 or 4 since they are not the highest value. I hope my explanation is understandable to write a reply. Thank You

答案1

得分: 2

Another solution without using for loops, which is a little bit faster:

list1 = [2, 4, 6, 12]
list2 = [2, 4, 6, 14]
max_in_both = max(filter(lambda x: x in list2, list1))

英文:

Another solution without using for loops, which is a little bit more faster:

list1=[2,4,6,12]
list2=[2,4,6,14]
max_in_both = max(filter(lambda x: x in list2, list1))

答案2

得分: 2

最简单的方法是使用两个集合的交集,如下所示:

list1 = [2,4,6,12]
list2 = [2,4,6,8,14]

print(max(set(list1).intersection(list2)))

...或者...

print(max(set(list1) & set(list2)))

输出:

6
英文:

Easiest way is to use the intersection of two sets as follows:

list1 = [2,4,6,12]
list2 = [2,4,6,8,14]

print(max(set(list1).intersection(list2)))

...or...

print(max(set(list1) & set(list2)))

Output:

6

答案3

得分: 1

以下是翻译好的代码部分:

def intersection(lst1, lst2):
    lst3 = [value for value in lst1 if value in lst2]
    lst3.sort()
    return lst3

lst1 = [2,4,6,12]
lst2 = [2,4,6,8,14]
var = intersection(lst1, lst2)
print(var[-1])
英文:

It would have been better for you to post what you had tried, but here's a simple solution.

def intersection(lst1, lst2):
    lst3 = [value for value in lst1 if value in lst2]
    lst3.sort()
    return lst3

lst1 = [2,4,6,12]
lst2 = [2,4,6,8,14]
var = intersection(lst1, lst2)
print(var[-1])

答案4

得分: 0

你可以简单地遍历并检查是否满足条件。代码如下:

list1=[9,1,2]
list2=[1,3,4]
import math
s=-math.inf
if len(list1)>len(list2):
 t=list1.copy() 
 list1=list2.copy() 
 list2=t.copy() 
for i in range(0,len(list1)):
 if list1[i] in list2 and list1[i]>=s:
  s=list1[i]
print(s)
英文:

you could have simply traverse and check if tour condition is satisfied
the code simply is

list1=[9,1,2]
list2=[1,3,4]
import math
s=-math.inf
if len(list1)>len(list2):
 t=list1.copy() 
 list1=list2.copy() 
 list2=t.copy() 
for i in range(0,len(list1)):
 if list1[i] in list2 and list1[i]>=s:
  s=list1[i]
print(s)

答案5

得分: 0

  1. 一个新的列表,只包含list1和list2中的共同整数
  2. 新列表中的最高整数被打印出来

看起来像这样:

#声明列表
list1 = [2,4,6,12]
list2 = [2,4,6,8,14]

#创建新列表
list3 = []

#检查共同整数
for i in list1:
    if i in list2:
        #将共同整数添加到新列表中
        list3.append(i)
    else:
        pass

#找到最高整数并打印出来
max_num = max(list3)
print(max_num)
英文:

Try this:

  1. A new list but only that contains the common integers of list1 and list2
  2. The highest integer in the new list is printd

It looks like:

#DECLARING LISTS
list1 = [2,4,6,12]
list2 = [2,4,6,8,14]

#CREATING NEW LIST
list3 = []

#CHECKING FOR COMMON INTEGERS
for i in list1:
    if i in list2:
        #ADDING COMMON INTEGERS TO THE NEW LIST
        list3.append(i)
    else:
        pass

#FINDING THE HIGHEST INTEGER & PRINTING IT
max_num = max(list3)
print(max_num)

huangapple
  • 本文由 发表于 2023年5月13日 22:14:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/76243175.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定