Skip to main content

python program get union of two list (program to get A union B ) list method .




 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Fri Mar 16 17:08:52 2018

@author: beast
"""
def version1():
    
    a=['a','b','c','d','e'] # list 1 
    b=['a','b','c','d','e','f','g','h'] # list 2
    c=[k for k in (a)if (k in (a) and k not in (b))] # include unique item from list 1 : items are (list1-list2)(set thoery)
    d=[l for l in (b) if l in (a ) and l in (b) or (l not in (a) and l in (b))] #include all the comman from list 1 and unique from list 2 
    lst=c+d # append above two comprehensed list to get union of list1 U list2
    lst.sort() # not neccessay but makes list easy to understand (sorting in ascending order )
    print("\n\na=",a)
    print("\n\nb=",b)
    print("\n\nunion=",lst)
    return lst # make it easy to import and use e.g import union_beast.py and then call union_beast.version1() it will return this union of list 
    
def version2():

    a=['a','b','c','d','e'] # list 1 
    b=['d','e','f','g','h'] # list 2
    c=[]
    for k in range (max(len(a),len(b))):# max return maximum number among two or more number so we iterate over max of length of both the list 
        if k <len(a) and a[k] not in (b) : # if length of a is greater than current indexing and  element at kth index is not present in list b (ie get List1-list 2 item) 
            c.append(a[k]) # if item is unique to list  1 then append to result list (here c)
        if  k <len(b) and b[k] not in (a) :# same as above logic but here we get item unique to list 2 (here list b)
            c.append(b[k]) # same append to result list (here c)
        if k <len(b) and b[k] in (a) : # now add the comman item to list (this is vital step as we only add only one instance of item that are both present in list 1 and list 2) eg if 2 is present in list 1 and list 2 we add only one 2 to result list 
            c.append(b[k]) # append to result list (here c)
    c.sort()        # not neccessary but make the result more eye catchy 
    print("\n\na=",a) #print list 1 
    print("\n\nb=",b)#print list 2 
    print("\n\nunion=",c) #print the result 
    
if __name__=="__main__": # used to run the function via main loading ...i.e if the module is not imported 
    print("\n----------by version 1------------\n")
    version1()
    print("\n\n------------by version 2------------")
    version2() 
    
NOTE: you can use [list].append([item]) function to take user input to list and then get the union operator

Comments

Popular posts from this blog

13 websites to register your free domain

Register your Free Domain Now!! 1)  .tk Dot TK is a FREE domain registry for websites on the Internet. It has exactly the same power as other domain extensions, but it’s free! Because it’s free, millions of others have been using .TK domains since 2001 – which makes .TK powerful and very recognizable.  Your website will be like www.yourdomainname.tk . It is free for 1 year. It’s a ccTLD domain whixh having the abbreviation  Tokelau. To create a .tk domain, Visit   www.dot.tk 2) co.cc Co.cc is completely free domain which is mostly used by blogspot bloggers because of it’s easy to use DNS system. Creating a co.cc for blogger is simple ( for instructions- “click here”). Your website will be like www.yourdomainname.co.cc . To create a .co.cc domain, visit www.co.cc 3)   co.nr co.nr is too like co.cc. Your website will be like  www.yourdomainname.co.nr . You can add it for blogger also.. To create a .co.cc domain, vi...

Binary Search Tree in C++( dynamic memory based )

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 #include<bits/stdc++.h> using namespace std; struct bst { int val; bst * left, * right; }; bst * root = nullptr; void srch ( int num,bst * head) { if (head == nullptr){ cout << " \n Number is not present \a " << endl; return ; } if (head -> val == num) { cout << " \n Number is present \n\a " ; return ; } else { if (num < head -> val) srch(num,head -> left); else srch(num,head -> right); ...

Binary Search Tree in Java implementation (reference based, dynamic memory)

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 import java.util.Scanner ; class BST { static BST . Node root = null ; public void insert ( int num ) { if ( root == null ) { root = new BST . Node ( num ); } else { // root node is not empty BST . Node temp = root ; while ( temp != null ) { if ( num <= temp . getVal ()) { if ( temp . getLeft () != null ) temp = temp . getLeft (); ...

Streamlining Java Web Application Deployment with React WAR Generator

In the ever-evolving world of web development, managing builds and deployments can often be cumbersome and error-prone. Today, we're excited to introduce a tool designed to simplify and streamline this process: the React WAR Generator . What is the React WAR Generator? The React WAR Generator is a Python-based tool that automates the creation of WAR (Web Application Archive) files for Java web applications. It caters specifically to frontend projects built with React or similar frameworks, making it easier to package and deploy your web applications to a Tomcat server. Key Features Profile-Based Builds : With support for multiple profiles ( dev , test , prod , default ), you can build your application according to different environments and configurations. Version File Generation : Optionally generate a version file that integrates versioning information directly into your TypeScript files, ensuring your build versions are always up-to-date. Tomcat Deployment : Simplify your deploy...

what is LOREM ipsum and why do designers use it

What is Lorem Ipsum? Lorem Ipsum  is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum. Why do we use it? It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English. Many desktop publishing packages and web page editors now...

how to Send a Confirmation Email Upon Form Submission-Woofoo

When someone successfully submits an entry, you can automatically send them a confirmation email to let them know. You can customize the email to include any follow-up info you'd like, and you can choose to include a copy of their entry in the email as well. To set up confirmation emails in Form Settings: Log in and go to  Forms . Hover over  Edit  next to the form you want to edit. Choose  Edit form . Click the  Form Settings  tab. Under Confirmation Options, select  Send Confirmation Email to User . From the  Send To  dropdown, select an Email field from your form. We'll send the confirmation email to the email address the person filling out your form entered into this field. If the dropdown says "No Email Fields Found", add an  Email  field to your form. In the  Reply To  textbox, enter the reply-to email—if someone replies to their confirmation email, this is the email address that their reply will be s...

Is your Nokia Cell Phone Original

Nokia is one of the largest selling phones across the globe. Most of us own a Nokia phone but are unaware of it’s originality. Are you keen to know whether your Nokia mobile phone is original or not? Then you are in the right place and this information is specially meant for you. Your phones IMEI (International Mobile Equipment Identity) number confirms your phone’s originality. Press the following on your mobile *#06# to see your Phone’s IMEI number(serial number).   Then check the 7th and 8th numbers Phone serial no. x x x x x x ? ? x x x x x x x IF the Seventh & Eighth digits of your cell phone are 02 or 20 this means your cell phone was assembled in Emirates which is very Bad quality IF the Seventh & Eighth digits of your cell phone are 08 or 80 this means your cell phone was manufactured in Germany which is fair quality   IF the Seventh & Eighth digits of your cell phone are 01 or 10 this means your cell phone was manufactured in Finland whic...

get free site builder

instruction 1) Download BlueVoda and save to your computers desktop. 2) click the setup.exe installer to start the install. 3) Following the on-screen instructions. 4) Reboot your computer immediate after installation. system Requirement Windows 95/98/ME/NT4/2000/XP/Vista/7