0

I'm making a program that will evaluate each entry on a vector/list whether it is inside a specific value. I don't know that to do. My Idea is like this:

I have a set of data that looks like this:

data = (0,1) , (0,2), (1,1), (1,2) #where data[0] = (0,1) (or data[0][0] = 0 and data[0][1] = 1), data[1] = (0,2) and so on.

This data is inaccesible, and cannot be edited.

so I want to evaluate whether the first value is inside a range, as well as the second. Like this:

if data[first number >= 10][second number < 1]:
     do something
neiesc
  • 633
  • 1
  • 7
  • 16
Enzo Vidal
  • 249
  • 1
  • 5
  • 13

3 Answers3

2

How is a small expression I would use list comprehensions

>>> data = (0,1) , (0,2), (1,1), (1,2)
>>> if [x for x in data if x[0] >= 1 and x[1] < 3 ]:
...  print("ok")
... 
ok
neiesc
  • 633
  • 1
  • 7
  • 16
0

Not sure if I understand correctly, but it seems you have situation such as this:

data = [(0,1), (0,2), (1,1), (1,2)]

for val_1, val_2 in data:
    if val_1 >= 10 and val_2 < 1:
        print('Do sometning')

Above you do something, if first and second values meet your conditions.

Or maybe you want to do the if statement for the indexes of your 2D data structure (like rows and columns of your 2D data structure), than you can do as follows:

for row, tuple_ in enumerate(data):
    for col, value in  enumerate(tuple_):
        print(row, col, value)
        if row >=10 and col < 1:
            print('Do sometning')
Marcin
  • 215,873
  • 14
  • 235
  • 294
0

Tuples in python are immutable, that's why you can't edit them. and values are zero-indexed, so the first value in the tuple in first item would be data[0][0] and second would be data[0][0]

data = [(0,1) , (0,2), (1,1), (1,2)]
print data[0][3]

if data[0][0]<10 and data[0][4]<=1:
    print "write code.."
Community
  • 1
  • 1