Set intersection problem Solving (Python edition) __python

Source: Internet
Author: User
Description:

You are are given two sorted list of numbers (ascending order). The lists themselves are comma delimited and the two lists, are semicolon. Print out the intersection of the two sets. Input Sample:

File containing two lists of ascending order sorted integers, comma delimited, one per line. e.g.

1,2,3,4;4,5,6
7,8,9;8,9,10,11,12

Output Sample:

Print out the ascending order sorted intersection of the two lists
e.g.

4
8,9
programme I:

#使用python的list自带的函数intersection

Import Sys

if __name__ = = "__main__":
argv = sys.argv
inf = open (Argv[1], ' R ')
While True:
line = Inf.readline ()
If Len (line) = = 0:
Break
ll = Line.split (';')
S1 = Ll[0].split (', ')
S2 = ll[1].split (', ')
RL = List (set (S2). Intersection (set (S1))
print ', '. Join (RL)
Programme II:

Import Sys

if __name__ = = "__main__": argv = sys.argv
inf = open (Argv[1], ' R ')
While True:
line = Inf.readline ()
If Len (line) = = 0:
Break
ll = Line.split (';')
S1 = Ll[0].split (', ')
S2 = ll[1].split (', ')
i = 0
j = 0
RL = []
While I < Len (S1) and J < Len (S2):
Print I,s1[i]
Print J,s2[j]
if Int (s1[i]) = = Int (s2[j]):
Rl.append (S1[i])
i + 1
J + 1
elif Int (s1[i]) < int (S2[j]):
i + 1
Else
J + 1
print ', '. Join (RL)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.