OR the List in Python (zip function)

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

OR the List in Python (zip function)
Get the EDINET code list in Python
Sorted list in Python
Python> list> extend () or + =
Filter List in Python
List find in Python
[Python] Sort the list of pathlib.Path in natural sort
Get the caller of a function in Python
Make a copy of the list in Python
I implemented the inverse gamma function in python
Create a function in Python
Download the file in Python
Find the difference in Python
ntile (decile) function in python
[python] Get the list of classes defined in the module
About the enumerate function (python)
Convert the image in .zip to PDF with Python
Methods available in the list
Nonlinear function modeling in Python
Draw implicit function in python
Immediate function in python (lie)
[Python] Outputs all combinations of elements in the list
Getting list elements in Python
[Python] How to output the list values in order
What does the last () in a function mean in Python?
Make sure all the elements in the list are the same in Python
Try to get the function list of Python> os package
Sort and output the elements in the list as elements and multiples in Python.
Have the equation graph of the linear function drawn in Python
Get the number of specific elements in a python list
How to get the last (last) value in a list in Python
I tried to implement the mail sending function in Python
2015-11-26 python> Display the function list of the module> import math> dir (math)
[Python] What is a zip function?
sort warning in the pd.concat function
Getting the arXiv API in Python
Difference between list () and [] in Python
[python] Manage functions in a list
Output 2017 Premium Friday list in Python
Function argument type definition in python
Save the binary file in Python
Hit the Sesami API in Python
Get the desktop path in Python
Write AWS Lambda function in Python
Get the script path in Python
Measure function execution time in Python
In the python command python points to python3.8
Implement the Singleton pattern in Python
[Python] Make the function a lambda function
Hit the web API in Python
Function synthesis and application in Python
I wrote the queue in Python
Calculate the previous month in Python
Examine the object's class in python
Get the desktop path in Python
How to use python zip function
Get the host name in Python
Delete multiple elements in python list
Access the Twitter API in Python
The first step in Python Matplotlib
About the basics list of Python basics