﻿

# Python script to find intersection between list of numbers.

Jul 16th, 2018
162
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
1. #!/usr/bin/env python
2. import re
3. import sys
4.
5. def get_numbers(string):
6.     """
7.
8.    Get numbers from string. Works if separated by commas...
9.
10.    >>> get_numbers('1, 2, 3,4')
11.    [1, 2, 3, 4]
12.
13.    ...and hyphens:
14.
15.    >>> get_numbers('1-2 - 3-4')
16.    [1, 2, 3, 4]
17.
18.    Ignores numbers in parentheses:
19.
20.    >>> get_numbers('1-2 (8) - 3 (10) -11.')
21.    [1, 2, 3, 11]
22.
23.    """
24.     string = re.sub('\(\d+\)', '', string)
25.     parts = [s for s in re.split('\D', string) if s]
26.     return [int(s) for s in parts]
27.
28. def get_repeated(list1, list2):
29.     """
30.
31.    Get the repeating numbers from two lists:
32.
33.    >>> get_repeated([1, 2, 3, 4, 5, 6], [2, 4, 6, 8, 10])
34.    [2, 4, 6]
35.
36.    REturns sorted:
37.
38.    >>> get_repeated([6, 5, 2, 3, 4, 1], [10, 4, 2, 8, 6])
39.    [2, 4, 6]
40.
41.    """
42.     s1 = set(list1)
43.     s2 = set(list2)
44.     return sorted(s1 & s2)
45.
46. def join_numbers(l):
47.     """
48.
49.    Join the numbers with hyphens:
50.
51.    >>> join_numbers([1, 4, 2, 3])
52.    '1 - 4 - 2 - 3'
53.
54.    """
55.     return ' - '.join(str(v) for v in l)
56.
57. if __name__ == "__main__":
58.     f1 = open(sys.argv[1])
59.     f2 = open(sys.argv[2])
60.