ich habe zwei Listen oder Strings etc und möchte diese vereinigen. Mathematisch also die Vereinigungsmenge bilden. Momentan mach ich das so:
Code: Alles auswählen
def vereinige(a, b):
### create an empty list for intersection elements
l = []
### iterate through sequence a and add all elements to the list
for z in a :
l.append(z)
### iterate through b and add all elements which are not already in the list
for z in b:
if (z not in l) : l.append(z)
return l
Code: Alles auswählen
>>> a = "Hallo"
>>> b = "Hello"
>>> vereinige(a,b)
['H', 'a', 'l', 'l', 'o', 'e']
Code: Alles auswählen
>>> a = 'Hallo Ballo'
>>> b = "HelloBello "
>>> vereinige(a,b)
['H', 'a', 'l', 'l', 'o', ' ', 'B', 'a', 'l', 'l', 'o', 'e']
Grüße