python - Creating a list with two different numbers -
i have 2 numbers x
, y
. x
example 1.5, , y
1.5 need create list of lists 8 different values.
[[0.5,0.5],[0.5,1.5],[0.5,2.5], [1.5,0.5],[1.5,1.5],[1.5,2.5], [2.5,0.5],[2.5,1.5],[2.5,2.5]] [1.5,1.5] #needs removed above list.
how can in python 3 using different x , y values? x
, y
numbers between 1 , 10. 1.5 or 2.5 or 3.5 etc.
try using following list comprehension:
items = [[a, b] in items b in items if x != or y != b]
as such:
>>> x = 1.5 >>> y = 1.5 >>> items = [x-1, x, x+1] >>> items = [[a, b] in items b in items if x != or y != b] >>> items [[0.5, 0.5], [0.5, 1.5], [0.5, 2.5], [1.5, 0.5], [1.5, 2.5], [2.5, 0.5], [2.5, 1.5], [2.5, 2.5]] >>>
edit:
or, if list comprehension confusing, can change nested for
loops:
for in items: j in items: if != x or j != y: cp.append([i, j])
this runs as:
>>> x = 1.5 >>> y = 1.5 >>> items = [x-1, x, x+1] >>> cp = [] >>> in items: ... j in items: ... if != x or j != y: ... cp.append([i, j]) ... >>> items = cp >>> items [[0.5, 0.5], [0.5, 1.5], [0.5, 2.5], [1.5, 0.5], [1.5, 2.5], [2.5, 0.5], [2.5, 1.5], [2.5, 2.5]] >>>
Comments
Post a Comment