Python Forum
Setting maximum value of Range() in For loop
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Setting maximum value of Range() in For loop
#1
Hello all, I am trying to use range() and for loop function to change the dictionary keys and values.
Expected output result is to create the keys and values of t{} according to the values in f_set.
The expected result is e.g I would like to create f_set[0]=(e.g 5) and f_set[1]=e.g 2 values and get the t output as {(1, 2, 1): 1, (1, 2, 2): 1, (1, 2, 3): 1, (1, 2, 4): 1, (1, 2, 5): 1,(1, 3, 1): 1, (1, 3, 2): 1}. However, the current output is always taking the maximum value fro m f_set as final value. How can I get the t output as I want to create? Thanks for any help.
N=[1,2,3]
t={}
f_set=[5,2,1,10,15,1,3,1]
for x in N:
    for y in N:
        if x is not y: 
          for f in f_set:
            for f_no in range(1,f+1):
              t[x,y,f_no]=1
print(t)
Output:
{(1, 2, 1): 1, (1, 2, 2): 1, (1, 2, 3): 1, (1, 2, 4): 1, (1, 2, 5): 1, (1, 2, 6): 1, (1, 2, 7): 1, (1, 2, 8): 1, (1, 2, 9): 1, (1, 2, 10): 1, (1, 2, 11): 1, (1, 2, 12): 1, (1, 2, 13): 1, (1, 2, 14): 1, (1, 2, 15): 1, (1, 3, 1): 1, (1, 3, 2): 1, (1, 3, 3): 1, (1, 3, 4): 1, (1, 3, 5): 1, (1, 3, 6): 1, (1, 3, 7): 1, (1, 3, 8): 1, (1, 3, 9): 1, (1, 3, 10): 1, (1, 3, 11): 1, (1, 3, 12): 1, (1, 3, 13): 1, (1, 3, 14): 1, (1, 3, 15): 1, (2, 1, 1): 1, (2, 1, 2): 1, (2, 1, 3): 1, (2, 1, 4): 1, (2, 1, 5): 1, (2, 1, 6): 1, (2, 1, 7): 1, (2, 1, 8): 1, (2, 1, 9): 1, (2, 1, 10): 1, (2, 1, 11): 1, (2, 1, 12): 1, (2, 1, 13): 1, (2, 1, 14): 1, (2, 1, 15): 1, (2, 3, 1): 1, (2, 3, 2): 1, (2, 3, 3): 1, (2, 3, 4): 1, (2, 3, 5): 1, (2, 3, 6): 1, (2, 3, 7): 1, (2, 3, 8): 1, (2, 3, 9): 1, (2, 3, 10): 1, (2, 3, 11): 1, (2, 3, 12): 1, (2, 3, 13): 1, (2, 3, 14): 1, (2, 3, 15): 1, (3, 1, 1): 1, (3, 1, 2): 1, (3, 1, 3): 1, (3, 1, 4): 1, (3, 1, 5): 1, (3, 1, 6): 1, (3, 1, 7): 1, (3, 1, 8): 1, (3, 1, 9): 1, (3, 1, 10): 1, (3, 1, 11): 1, (3, 1, 12): 1, (3, 1, 13): 1, (3, 1, 14): 1, (3, 1, 15): 1, (3, 2, 1): 1, (3, 2, 2): 1, (3, 2, 3): 1, (3, 2, 4): 1, (3, 2, 5): 1, (3, 2, 6): 1, (3, 2, 7): 1, (3, 2, 8): 1, (3, 2, 9): 1, (3, 2, 10): 1, (3, 2, 11): 1, (3, 2, 12): 1, (3, 2, 13): 1, (3, 2, 14): 1, (3, 2, 15): 1}
Reply
#2
You are always getting the maximum value for f_set because for each pair of initial values, you are going through every value in f_set (line 7). You only want to get one value in f_set per pair of initial values.

N=[1,2,3]
t={}
f_set=[5,2,1,10,15,1,3,1]
f_index = 0
for x in N:
    for y in N:
        if x is not y: 
            for f_no in range(1,f_set[f_index]+1):
                t[x,y,f_no]=1
            f_index += 1
print(t)
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
Thank you very much for your help. It worked and I could get the result as I want.
Reply
#4
With product from itertools you can remove the nested for-loop for x and y.
from itertools import product


N = [1, 2, 3]
t = {}
f_set = [5,2,1,10,15,1,3,1]
f_index = 0
for x,y in product(N, N):
    if x is not y: 
        for f_no in range(1, f_set[f_index] + 1):
            t[x, y, f_no] = 1
        f_index += 1
print(t)
With a card deck it's easier to understand:

from itertools import product
from itertools import chain

# 2 - 10, Jack, Queen, King, Ace
numbers = chain(range(2,11), ['Jack', 'Queen', 'King', 'Ace'])
colors = ['♠', '♡', '♣', '♢']

for color, number in product(colors, numbers):
    print(f'{color} {number}')
andydoc likes this post
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#5
@DeaD_EyE ..Thanks for your suggestion.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [SOLVED] [loop] Exclude ranges in… range? Winfried 2 1,428 May-14-2023, 04:29 PM
Last Post: Winfried
  Setting For Loop Counter rturus 2 783 Dec-07-2022, 01:34 PM
Last Post: deanhystad
  matplotlib x axis range goes over the set range Pedroski55 5 3,162 Nov-21-2021, 08:40 AM
Last Post: paul18fr
  Loop Excel Range Kristenl2784 2 3,251 Jun-18-2020, 04:49 PM
Last Post: Kristenl2784
  Define a range, return all numbers of range that are NOT in csv data KiNeMs 18 7,006 Jan-24-2020, 06:19 AM
Last Post: KiNeMs
  Is 2 a prime number? for loop & range fuction in python docs says yes, mine says no. allusernametaken 4 2,880 Nov-17-2019, 02:56 AM
Last Post: allusernametaken
  loop through range until reach size and exclude specific symbol pino88 3 2,362 Sep-23-2019, 02:32 AM
Last Post: perfringo
  How give an for loop an maximum number, help pleace ? JamieVanCadsand 3 3,538 Aug-09-2017, 08:15 AM
Last Post: JamieVanCadsand
  newbie while loop error: IndexError: list assignment index out of range msa969 3 75,038 Mar-31-2017, 12:24 PM
Last Post: msa969

Forum Jump:

User Panel Messages

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