Hack Forums
[Python] Iterating indexes of global values - Printable Version

+- Hack Forums (https://hackforums.net)
+-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444)
+--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151)
+---- Forum: Software Development (https://hackforums.net/forumdisplay.php?fid=118)
+---- Thread: [Python] Iterating indexes of global values (/showthread.php?tid=6155125)



Iterating indexes of global values - simmer - May 22, 2021

Code
global x
global i
x.index(i) >= x.index(i+1)

I need x to include something other than i (i+1)

I need i to have an infinite number of iterations (probably sets)

I need both of those to be comparable size wise


RE: Iterating indexes of global values - Flash7475 - May 23, 2021

I'm not quite sure what your trying to accomplish with this or why you need a global scope variable when you don't have any functions. Your code also does not make sense because x and i are not defined with anything. If your looking for an infinite number of iterations I would use a "while(True)" statement.

If your looking for something sort of similar to what you posted, maybe this is what your looking for?

Code
def main():

    #define your data
    x = [10,2,30,4,5]
    i = [2,4,6,8,10]

    #Iterate through every index of the first list
    for number in x:
        #Iterate through every index of the second list
        for other_number in i:
            #Comparison statement
            if number >= other_number:
                print(number, " : " ,other_number)
            else:
                pass
           
if __name__ == "__main__":
    main()



RE: Iterating indexes of global values - simmer - May 26, 2021

Cool. Now I have a type error:

Code
p = [set(), set(set())]
deter_g = ever_p[1]
print(deter_g)
#brings output: set()

add_set_inc = set()
ever_p = [add_set_inc]
add_set_count = 0

for add_set_count in range(len(ever_p)):
    add_set_inc = set(add_set_inc)
    ever_p.append(add_set_inc)
    add_set_count = add_set_count + 1
    if add_set_count == float('inf'):
        break
        
deter_g = ever_p[float('inf')]
print(deter_g)
#TypeError: list indices must be integers or slices, not float

How do I reference the end of the list?


(May 23, 2021 12:14 AM)Flash7475 Wrote:  I'm not quite sure what your trying to accomplish with this or why you need a global scope variable when you don't have any functions. Your code also does not make sense because x and i are not defined with anything. If your looking for an infinite number of iterations I would use a "while(True)"

The actual code (which is fire-walled by hack forums)
Does in fact have functions

The fact that the global variables are undefined confuses me... but it’s best to just stick to what the debugger is doing so I’m waiting for the NameError to pop up before I test that out and/or ask online.


I figured out the problem above

Use -1 to reference the end of a list