When I tried to OR the List when creating the feature matrix for the statistical model, I had a lot of trouble, so make a note of it.
If you write only the result first, you can write as follows
>>> list1 = [1, 1, 0, 0]
>>> list2 = [1, 0, 1, 0]
>>>
>>> #Logical sum
>>> [max(t) for t in zip(list1, list2)]
[1, 1, 1, 0]
>>>
>>> #Logical AND
>>> [min(t) for t in zip(list1, list2)]
[1, 0, 0, 0]
The above method uses the zip function. The zip function tuples multiple lists for each index position.
>>> list1 = [1, 1, 0, 0]
>>> list2 = [1, 0, 1, 0]
>>> zip(list1, list2)
[(1, 1), (1, 0), (0, 1), (0, 0)]
>>>
>>> #Can be created with 3 or more Lists
>>> zip(['a', 'b', 'c', 'd'], ['e', 'f', 'g', 'h'], ['i', 'j', 'k', 'l'])
[('a', 'e', 'i'), ('b', 'f', 'j'), ('c', 'g', 'k'), ('d', 'h', 'l')]
>>>
>>> #When Lists with different lengths are mixed, only the shortest List length can be created.
>>> zip(['a', 'b', 'c', 'd'], ['e', 'f', 'g', 'h'], ['i', 'j'])
[('a', 'e', 'i'), ('b', 'f', 'j')]
Recommended Posts