0

I have a list of lists. Each element of the list is made in this way [id1, id2, timex, value]. Given two external numbers ex and lx (ex < lx), I want to: 1. Check each element of the list, and see if timex < ex. If I find an element in which timex < ex, then I can end everything. 2. If I don't find it, then I want to make another check, starting for the first element of the list, and see if I can find an element in which ex < timex < lx. 3. If I don't find what said in point 2, I want to make another control and check whether there is an element with timex > lx.

What I did is this. But I want to know if there is a better way to do it.

f1 = 0
f2 = 0
found = False       
                    # 1         
                    count = 0
                    while (found == False) and (count < len(Listx)):
                        if Listx[count][2] <= ex:
                            print "Found - 1"
                            f1 = Listx[count][0]
                            f2 = Listx[count][1]
                            Listx[count][2] = Listx[count][2] + dx
                            found = True
                        count = count + 1

                    # 2 
                    count = 0
                    while (found == False) and (count < len(Listx)):
                        if (Listx[count][2] > ex) and (Listx[count][2] <= lx):
                            print "Found - 2"
                            Listx[count][2] = Listx[count][2] + ex
                            f1 = Listx[count][0]
                            f2 = Listx[count][1]
                            found = True
                        count = count + 1
                    #3
                    count = 0   
                    while (found == False) and (count < len(Listx)):
                        if (Listx[count][1] < lx):
                            f1 = Listx[count][0]
                            f2 = Listx[count][1]
                            found = True
                        count = count + 1
Jus
  • 11
  • 1
  • 3
    Welcome to So. Please take the time to read [ask]. If your code works and you want a critique, you should post it at https://codereview.stackexchange.com/. It helps if you post a minimal example of the data you are working with. – wwii Oct 20 '17 at 20:24
  • You didn't say anything about modifying the list, but your code changes the 'timex' values. That complicates the easy answer of keeping the list sorted by 'timex'. But it does seem more of a codereview question. – Kenny Ostrom Oct 20 '17 at 20:48

2 Answers2

0

You don't necessarily need to index your list with 'count' every time, you can use for loops to iterate through. If you need that index for something else, you can use a for loop in the style of

for index, value in enumerate(lst):
    # code

There are also some other modules that could help, depending on the nature of your data. See test if list contains a number in some range for examples of a few.

That said, here's my solution.

f1 = 0
f2 = 0
found = False
for element in Listx :
    if element[2] <= ex:
        print "Found - 1"
        f1 = element[0]
        f2 = element[1]
        element[2] = element[2] + dx
        found = True
        break
if found = False:
    for element in Listx :
        if (element[2] > ex) and (element[2] <= lx):
            print "Found - 2"
            element[2] = element[2] + ex
            f1 = element[0]
            f2 = element[1]
            found = True
            break
if found = False:
    for element in Listx :
        if (element[1] < lx):
            print "Found - 3"
            f1 = element[0]
            f2 = element[1]
            found = True 
            break
ividito
  • 321
  • 3
  • 14
0

You can write things a lot more compactly using for loops and for...else statements.

# 1
for item in Listx:
    if item[2] <= ex:
        print ("Found - 1")
        f1, f2 = item[:2]
        item[2] += dx
        break
else:
    # begin # 2 here...

The other loops can be similarly condensed. To describe the syntax going on here:

for x in y:
    // ...

is essentially

i = 0
while i < len(y):
    x = y[i]
    // ...
    i = i + 1

except that you don't get the i variable.

item[:1] uses list slicing to give us item[0], item[1].

Now something like a, b = x, y sets the variables respectively. It's shorthand for

a = x
b = y

Finally, break will break you out of the current loop immediately. The else is another fancy Python thing. The else code triggers when the previous loop was not broken out of.

I'll admit I used some "Pythonic" shortcuts above that use some of the features that I think makes Python great. :)

dtauxe
  • 153
  • 11