Re design the intersect and union using tuple parameter passing : Function Tuple Parameters : Function : Python examples (example source code) Organized by topic

Python
1. 2D
2. Application
3. Buildin Function
4. Class
5. Data Structure
6. Data Type
7. Development
8. Dictionary
9. Event
10. Exception
11. File
12. Function
13. GUI Pmw
14. GUI Tk
15. Language Basics
16. List
17. Math
18. Network
19. String
20. System
21. Thread
22. Tuple
23. Utility
24. XML
Java
Java Tutorial
Java Articles
Java Products
Java by API
C# / C Sharp
C# / CSharp Tutorial
ASP.Net
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
PHP
SQL Server / T-SQL
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Python » Function » Function Tuple ParametersScreenshots 
Re design the intersect and union using tuple parameter passing


def intersect(*args):
    res = []
    for x in args[0]:                  # scan first sequence
        for other in args[1:]:         # for all other args
            if x not in other: break   # item in each one?
        else:                          # no:  break out of loop
            res.append(x)              # yes: add items to end
    return res

def union(*args):
    res = []
    for seq in args:                   # for all args
        for x in seq:                  # for all nodes
            if not x in res:
                res.append(x)          # add new items to result
    return res

s1, s2, s3 = "SPAM""SCAM""SLAM"

intersect(s1, s2), union(s1, s2)           operands

intersect([1,2,3](1,4))                  # mixed types 

intersect(s1, s2, s3)                      operands

union(s1, s2, s3)

           
       
Related examples in the same category
1. Any number of parameter: turn parameter into a tupleAny number of parameter: turn parameter into a tuple
2. Unpacking Argument ListsUnpacking Argument Lists
3. Arbitrary Argument Lists
4. Parameter passing: mixed with dictionary and tupleParameter passing: mixed with dictionary and tuple
5. Illustrate parameter passing: tuple, type and tupleIllustrate parameter passing: tuple, type and tuple
Home | Contact Us
Copyright 2003 - 07 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.