Python Forum

Full Version: Using values from a list of tuples within a loop
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello Python forum!

I've been trying to solve this issue long enough to make want to seek help Smile

I've basically created a function that contains a loop that goes through a range of values (among other things) and uses the first values of tuples stored in a list as a condition to continue with the next iteration of the loop. What I haven't been able to figure out is how to use or assign the second values of the tuples to each iteration of the output.

Here's the code:

srcs = 100

srcids = []

src_prefix = '2/9/1/'

jtrange = lambda start, end: range(start, end + 1)


def srcidrange(srctart, srcend, idstart, idend):
    src = []
    for i in jtrange(srctart, srcend):
        src.append(i)
    ids = []
    for i in jtrange(idstart, idend):
        ids.append(i)
    srcids.extend(zip(src, ids))


srcidrange(10, 25, 1115, 1130)
srcidrange(49, 59, 115, 125)


def gen_map(srctrg, prefix):
    for i in range(srctrg):
        port_numb = str(i // 768)
        port_i8s = str((i//8) % 96)
        chan_numb_gr = str(i % 8)
        if i not in ([x[0] for x in srcids]):
            continue
        print(prefix + port_numb + '/' + port_i8s + '/' + chan_numb_gr + ',' + str(i) + ',' + str(0) + ',' + str(0) + ',\n')

gen_map(srcs, src_prefix)

print([y[1] for y in srcids]) # values that I want to have come out (in order) with the output of the gen_map function where currently str(i) is being used.
Thank you for any help you could provide with this! Wink

Regards,

jtpy
Make gen_map to return what you want (in addition to print), although I would leave out or move print out of the function altogether
Also note that the code you posted has number of issues that need to be addressed - using global variables, obfuscating use of built-in functions, etc.
Hello buran, and thanks for your message.

I have stripped down the code a bit to post it here and have used the print function just to make it easier for people to see what's happening. The actual script simply writes the function's output to a file.
I'm aware that other things could be improved but this is a work in progress anyway.

But anyway, do you have an idea about how to achieve what I explained initially? Would really appreciate any help.

Best regards,

jtpy
can you be more specific what you mean by how to use or assign the second values of the tuples to each iteration of the output and # values that I want to have come out (in order) with the output of the gen_map function where currently str(i) is being used.?
i.e. your line 35 already prints thevalues, can you show an example
Hello buran,

(Mar-09-2018, 08:17 AM)buran Wrote: [ -> ]can you be more specific what you mean by how to use or assign the second values of the tuples to each iteration of the output and # values that I want to have come out (in order) with the output of the gen_map function where currently str(i) is being used.?

Sure, I'm storing tuples in the srcids list. I'm using the first element of the tuples in that list to have the loop within the gen_map function continue to the next iteration. That's working fine. In addition to that, I want the second value of each tuple to be output as part of the string that's built up by concatenating what you see in the print function.

So right now the output looks like this:

2/9/1/0/1/2,10,0,0
2/9/1/0/1/3,11,0,0
2/9/1/0/1/4,12,0,0
2/9/1/0/1/5,13,0,0
2/9/1/0/1/6,14,0,0
2/9/1/0/1/7,15,0,0
2/9/1/0/2/0,16,0,0
2/9/1/0/2/1,17,0,0
2/9/1/0/2/2,18,0,0
2/9/1/0/2/3,19,0,0
2/9/1/0/2/4,20,0,0
2/9/1/0/2/5,21,0,0
2/9/1/0/2/6,22,0,0
2/9/1/0/2/7,23,0,0
2/9/1/0/3/0,24,0,0
2/9/1/0/3/1,25,0,0
2/9/1/0/6/1,49,0,0
2/9/1/0/6/2,50,0,0
2/9/1/0/6/3,51,0,0
2/9/1/0/6/4,52,0,0
2/9/1/0/6/5,53,0,0
2/9/1/0/6/6,54,0,0
2/9/1/0/6/7,55,0,0
2/9/1/0/7/0,56,0,0
2/9/1/0/7/1,57,0,0
2/9/1/0/7/2,58,0,0
2/9/1/0/7/3,59,0,0
And I want it to come out like this:

2/9/1/0/1/2,1115,0,0
2/9/1/0/1/3,1116,0,0
2/9/1/0/1/4,1117,0,0
2/9/1/0/1/5,1118,0,0
2/9/1/0/1/6,1119,0,0
2/9/1/0/1/7,1120,0,0
2/9/1/0/2/0,1121,0,0
2/9/1/0/2/1,1122,0,0
2/9/1/0/2/2,1123,0,0
2/9/1/0/2/3,1124,0,0
2/9/1/0/2/4,1125,0,0
2/9/1/0/2/5,1126,0,0
2/9/1/0/2/6,1127,0,0
2/9/1/0/2/7,1128,0,0
2/9/1/0/3/0,1129,0,0
2/9/1/0/3/1,1130,0,0
2/9/1/0/6/1,115,0,0
2/9/1/0/6/2,116,0,0
2/9/1/0/6/3,117,0,0
2/9/1/0/6/4,118,0,0
2/9/1/0/6/5,119,0,0
2/9/1/0/6/6,120,0,0
2/9/1/0/6/7,121,0,0
2/9/1/0/7/0,122,0,0
2/9/1/0/7/1,123,0,0
2/9/1/0/7/2,124,0,0
2/9/1/0/7/3,125,0,0
Thanks again for your help.

jtpy
Hi,

Figured out. I was overthinking it (as usual).

This gives the desired result:

srcids = []

src_prefix = '2/9/1/'

def jtrange(start, end):
    return range(start, end + 1)


def srcidrange(srctart, srcend, idstart, idend):
    src = []
    for i in jtrange(srctart, srcend):
        src.append(i)
    ids = []
    for i in jtrange(idstart, idend):
        ids.append(i)
    srcids.extend(zip(src, ids))


srcidrange(10, 25, 1115, 1130)
srcidrange(49, 59, 115, 125)


def gen_map(srctrg, prefix):
    for i, j in srctrg:
        port_numb = str(i // 768)
        port_i8s = str((i//8) % 96)
        chan_numb_gr = str(i % 8)
        print(prefix + port_numb + '/' + port_i8s + '/' + chan_numb_gr + ',' + str(j) + ',' + str(0) + ',' + str(0)) # + ',\n')

gen_map(srcids, src_prefix)
Now I can move on to improving the code.

Best regards,

jtpy