This post is a Japanese translation of Mathematics to code: ∈-by Kohei Shingai.
https://www.instagram.com/p/BQvOfRygJCA
Use ʻis.element (x, A)`,
A <- c(1:5)
#Set A is[1, 2, 3, 4, 5]
is.element(1, A) #result: TRUE
#That is, 1 ∈ A- {1, 2, 3, 4, 5}whether?
is.element(6, A) #result: FALSE
#That is, 6 ∈ A- {1, 2, 3, 4, 5}whether?
Use x in A
,
A = set([1, 2, 3, 4, 5]) #Set A
1 in A #result: True
#That is, 1 ∈ A- {1, 2, 3, 4, 5}whether?
6 in A #result: False
#That is, 6 ∈ A- {1, 2, 3, 4, 5}whether?
Use ʻA.include? X`,
A = (1..5).to_a
#Set A is[1, 2, 3, 4, 5]
A.include?1 #result: true
#That is, 1 ∈ A- {1, 2, 3, 4, 5}whether?
A.include?6 #result: false
#That is, 6 ∈ A- {1, 2, 3, 4, 5}whether?
Use ʻA.indexOf ()! = -1 or ʻA.includes (x)
,
A = [1, 2, 3, 4, 5] //Set A
A.indexOf(1) != -1 //result: true
//That is, 1 ∈ A- {1, 2, 3, 4, 5}whether?
A.indexOf(6) != -1 //result: false
//That is, 6 ∈ A- {1, 2, 3, 4, 5}whether?
A.includes(1) //result: true -ES2016 or later
//That is, 1 ∈ A- {1, 2, 3, 4, 5}whether?
A.includes(6) //result: false -ES2016 or later
//That is, 6 ∈ A- {1, 2, 3, 4, 5}whether?
Recommended Posts