【问题标题】:Print common elements of two lists with two nested for loops使用两个嵌套的 for 循环打印两个列表的公共元素
【发布时间】:2021-03-20 12:38:50
【问题描述】:

我是 python 新手,我有这段代码可以打印出两个序列之间的交集。

代码运行良好,但我正在尝试将其转换为嵌套循环类型的代码。我尝试了很多东西,但都没有成功,你能帮帮我吗?

这里是代码

print("This program finds the common elements of two lists")

sequenceOne = input("Please enter the space-separated elements of the first list: ")
sequenceTwo = input("Please enter the space-separated elements of the second list: ")

sequenceOne = sequenceOne.split()
sequenceTwo = sequenceTwo.split()
listOfIntersection = []

for i in sequenceOne:
     if i in sequenceTwo:
         sequenceTwo.remove(i)
         listOfIntersection.append(i)

print('The intersection of these two lists is {' +(", ".join(str(i) for i in listOfIntersection))+'}')

输出:

This program finds the intersection of two sets
Please enter the space-separated elements of the first set: 12 k e 34 1.5 12 hi 12 0.2
Please enter the space-separated elements of the second set: 1.5 hi 12 0.1 54 12 hi hi hi
The intersection of these two sets is {12, 1.5, 12, hi}

【问题讨论】:

  • 检查我的新答案,这是你想要的吗?
  • 如果可行,最好不要尝试使用嵌套循环。使用 set(...) 提出的答案是使用 Python 处理此问题的非常好且简单/更好的方法。
  • @Malo 我知道这更干净,但它是一个分配,它是关于嵌套循环的????
  • @MMSS19 您应该编辑并说明这一点。简单的方法是将“if i in sequenceTwo:”替换为第二个 for 循环“for j in sequenceTwo:” ...
  • 输出真的很奇怪,你如何定义是否需要保留重复项?我在第一组中数 3x12,在第二组中数 4x'hi',但答案有 2x 12 和 1x hi .. 你能解释一下吗?

标签: python python-3.x nested-loops


【解决方案1】:

这是一个带有嵌套 for 循环的代码,它保留重复值。

print("This program finds the intersection of two sets")

sequenceOne = input("Please enter the space-separated elements of the first set: ")
sequenceTwo = input("Please enter the space-separated elements of the second set: ")

sequenceOne = sequenceOne.split()
sequenceTwo = sequenceTwo.split()
listOfIntersection = []

for i in sequenceOne:
     for j in sequenceTwo:
         if (i==j):
             listOfIntersection.append(i)

print('The intersection of these two sets is {' +(", ".join(str(i) for i in listOfIntersection))+'}')

这是第二个解决方案,它完全模仿了您的第一个解决方案:

print("This program finds the intersection of two sets")

sequenceOne = input("Please enter the space-separated elements of the first set: ")
sequenceTwo = input("Please enter the space-separated elements of the second set: ")

sequenceOne = sequenceOne.split()
sequenceTwo = sequenceTwo.split()
listOfIntersection = []

for i in sequenceOne:
     for j in sequenceTwo:
         if (i==j):
             listOfIntersection.append(i)
             sequenceTwo.remove(i)
             break
    

print('The intersection of these two sets is {' +(", ".join(str(i) for i in listOfIntersection))+'}')

【讨论】:

  • 谢谢@Malo。非常感谢您的帮助
  • 第二种解决方案为您提供准确的输出......但它真的很遥远的任务;)
  • 我知道,他们对使用他们建议的方法非常讲究。这有点愚蠢。
【解决方案2】:
print("This program finds the intersection of two sets")

sequenceOne = input("Please enter the space-separated elements of the first set: ").split()
sequenceTwo = input("Please enter the space-separated elements of the second set: ").split()

print (set(sequenceOne).intersection(sequenceTwo))

intersection() 方法返回两个集合 sequenceOnesequenceTwo 之间的交集

【讨论】:

  • set() 的问题在于它会删除重复项。正如您从示例中看到的那样,12 是重复的,因为它出现了两次
  • 对不起,我知道的唯一方法是使用setsintersection() 方法,因为交集通常被理解并与集合一起使用,它们会删除重复项,或者在我的另一个中使用list comprehension回答,或者使用您的for loops 方式,并且可能最好的方式来做您正在寻找的事情是我的另一个答案。有一种方法可以绕过集合,即添加键以保持重复,但我猜在这个问题上效率不高。
  • 没关系,感谢您的尝试和帮助。非常感谢
【解决方案3】:
print("This program finds the intersection of two sets")

sequenceOne = input("Please enter the space-separated elements of the first set: ").split()
sequenceTwo = input("Please enter the space-separated elements of the second set: ").split()

listOfIntersection = [x for x in sequenceOne if x in sequenceTwo]

print('The intersection of these two sets is {' +(", ".join(str(i) for i in listOfIntersection))+'}')

此行替换嵌套的 for 循环和您拥有的条件并创建您的交集列表:listOfIntersection = [x for x in sequenceOne if x in sequenceTwo]

【讨论】:

  • 这算是嵌套循环吗?并感谢您的帮助
  • 这是列表理解 .. 问你老师他是否接受这个;)
猜你喜欢
  • 2020-03-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-12-07
  • 2020-01-21
  • 2021-09-15
相关资源
最近更新 更多