python二分法查找程序_Python | 程序查找两个列表的差异

python二分法查找程序

Given two lists of integers, we have to find the differences i.e. the elements which are not exists in second lists.

给定两个整数列表,我们必须找到差异,即第二个列表中不存在的元素。

Example:

例:

    Input:
    List1 = [10, 20, 30, 40, 50]
    List2 = [10, 20, 30, 60, 70]

    Output:
    Different elements:
    [40, 50]

Logic:

逻辑:

To find the differences of the lists, we are using set() Method, in this way, we have to explicitly convert lists into sets and then subtract the set converted lists, the result will be the elements which are not exist in the second.

为了找到列表的差异 ,我们使用set()方法,这样,我们必须将列表显式转换为集合,然后减去转换后的列表,结果将是第二个中不存在的元素。

Program to find difference of two lists in Python

程序以查找Python中两个列表的差异

# list1 - first list of the integers
# lists2 - second list of the integers
list1 = [10, 20, 30, 40, 50]
list2 = [10, 20, 30, 60, 70]

# printing lists 
print "list1:", list1
print "list2:", list2

# finding and printing differences of the lists
print "Difference elements:"
print (list (set(list1) - set (list2)))

Output

输出量

    list1: [10, 20, 30, 40, 50]
    list2: [10, 20, 30, 60, 70]
    Difference elements:
    [40, 50]


Program 2: with mixed type of elements, printing 1) the elements which are not exist in list2 and 2) the elements which are not exists in list1.

程序2:使用混合类型的元素,打印1)列表2中不存在的元素和2)列表1中不存在的元素。

# list1 - first list with mixed type elements
# lists2 - second list with mixed type elements
list1 = ["Amit", "Shukla", 21, "New Delhi"]
list2 = ["Aman", "Shukla", 21, "Mumbai"]

# printing lists 
print "list1:", list1
print "list2:", list2

# finding and printing differences of the lists
print "Elements not exists in list2:"
print (list (set(list1) - set (list2)))

print "Elements not exists in list1:"
print (list (set(list2) - set (list1)))

Output

输出量

    list1: ['Amit', 'Shukla', 21, 'New Delhi']
    list2: ['Aman', 'Shukla', 21, 'Mumbai']
    Elements not exists in list2:
    ['Amit', 'New Delhi']
    Elements not exists in list1:
    ['Aman', 'Mumbai']


翻译自: https://www.includehelp.com/python/find-the-differences-of-two-lists.aspx

python二分法查找程序

你可能感兴趣的:(列表,python,java,算法,数据结构)