简体   繁体   English

使用两个嵌套的 for 循环打印两个列表的公共元素

[英]Print common elements of two lists with two nested for loops

I'm new to python and I have this code that programs to print out the intersection between two sequences.我是 python 的新手,我有这段代码可以打印出两个序列之间的交集。

the code works fine, but I'm trying to convert it into nested loops kind of code.该代码工作正常,但我正在尝试将其转换为嵌套循环类型的代码。 I tried many things, but non worked, could you please help me?我尝试了很多东西,但没有奏效,你能帮帮我吗?

here is the code这是代码

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))+'}')

Output: Output:

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}
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))

the intersection() method returns the intersection between the two sets sequenceOne and sequenceTwo intersection()方法返回两个集合sequenceOnesequenceTwo之间的交集

This is a code with nested for loops, which keeps duplicated values.这是一个带有嵌套 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))+'}')

Here is second solution, which mimics exactly what you did first:这是第二种解决方案,它完全模仿了您首先所做的事情:

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))+'}')
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))+'}')

this line replaces the nested for loop and condition that you have and creates your list of intersection: listOfIntersection = [x for x in sequenceOne if x in sequenceTwo]此行替换您拥有的嵌套for循环和条件并创建您的交集列表: listOfIntersection = [x for x in sequenceOne if x in sequenceTwo]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM