Python Forum
Using values from a list of tuples within a loop
Thread Rating:
  • 1 Vote(s) - 5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Using values from a list of tuples within a loop
#1
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
Reply
#2
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.
Reply
#3
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
Reply
#4
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
Reply
#5
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
Reply
#6
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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Copying the order of another list with identical values gohanhango 7 1,062 Nov-29-2023, 09:17 PM
Last Post: Pedroski55
  Search Excel File with a list of values huzzug 4 1,147 Nov-03-2023, 05:35 PM
Last Post: huzzug
  Loop through values and compare edroche3rd 6 628 Oct-18-2023, 04:04 PM
Last Post: edroche3rd
  Comparing List values to get indexes Edward_ 7 1,083 Jun-09-2023, 04:57 PM
Last Post: deanhystad
  Loop through json file and reset values [SOLVED] AlphaInc 2 1,962 Apr-06-2023, 11:15 AM
Last Post: AlphaInc
  Adding values with reduce() function from the list of tuples kinimod 10 2,514 Jan-24-2023, 08:22 AM
Last Post: perfringo
  user input values into list of lists tauros73 3 1,025 Dec-29-2022, 05:54 PM
Last Post: deanhystad
  Creating a loop with dynamic variables instead of hardcoded values FugaziRocks 3 1,430 Jul-27-2022, 08:50 PM
Last Post: rob101
  How do loop over curl and 'put' different values in API call? onenessboy 0 1,197 Jun-05-2022, 05:24 AM
Last Post: onenessboy
  Loop through values in dictrionary and find the same as in previous row Paqqno 5 1,839 Mar-27-2022, 07:58 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020