goldraker34
goldraker34

Reputation: 55

Someone please explain what the for loop does / its meaning for this section of code?

I'm learning python and I'm trying to understand what the for loop means. My assignment is to understand and comment the full code out. I know that the first two lines of code take an input. Can someone tell me what the next lines of code mean? The for loop is extremely confusing. By the way, let's say the input for j_t is 1, 3, 5 and the input for d_t is 2, 4, 6. Hopefully that helps.

# to take inputs
j_t = list(map(int, input("Process times: ").split(",")))
d_t = list(map(int, input("Job Due Dates: ").split(",")))

dict_spt = {}
dict_edd = {}
for i in range(len(j_t)):
    dict_spt[int(j_t[i])] = int(d_t[i])
    dict_edd[int(d_t[i])] = int(j_t[i])

The above is a segment of the full code. I ran the full code and it allows me to put in input but I need to understand what the for loop actually does/means in plain english.

Upvotes: 1

Views: 45

Answers (3)

IneptusMechanicus
IneptusMechanicus

Reputation: 508

The for loop is your standard construct for iterating through arrays of data. I'll walk you through the whole code.

So in python you have multiple types of, let's call them "array like" structures. In this example you've shown, you have lists and dictionaries. Lists are very similar to standard arrays, where as dictionaries are an "array" of key value pairs.

Now the first two lines take your comma separated inputs and turn them into lists.

Then you create two empty dictionaries - that's indicated by the curly brackets, those are used for defining dictionaries.

Then you have the actual for loop - the best way to read the for line would be:

"For each element i in the range() from zero to the length of thej_t list, do the following"

Important to note that i or whatever you put after the for keyword is a variable that is created on the fly simply for the purpose of iterating through the piece of data you're iterating.

And then in the for loop what you are doing is adding a new record to each dictionary where the key and the value are the integer value of each list element.

Arrays and lists have indexes for every element inside of them so all the standard for loop needs is a numerical range which would represent every index in the structure you're iterating through.

Hope this was clear enough.

Upvotes: 1

Claude Shannon
Claude Shannon

Reputation: 69

Python input give you a string input, j_t and d_t are lists made from your inputs but it is still as string in the lists. for loop is actually iterating on your inputs to create dictionnaries which allow you to get j_t from a d_t value and vice versa and in a same time is casting all inputs to integers to make it easier to use i guess.

Hope it is clear enough

Upvotes: 2

Leon
Leon

Reputation: 31

The first 2 lines of code take in an input and creates a list out of them (j_t and d_t).

The next 2 lines initializes a dictionary object (dict_spt and dict_edd).

The for loop iterates from 0 to the length of j_t - 1 = 2 which means that it iterates over all the indices of the items in j_t (1,3,5). The first line in the dictionary puts the key-value pair of int(j_t[0]) = 1 , int(d_t[0]) = 2 into the dict_spt dictionary. And it also adds in the key-value pair of int(d_t[0]) = 2 , int(j_t[0]) = 1 (which is same like the first one but reversed) into the dict_edd dictionary. It does this until the last element (5) is reached.

Hopefully this explanation is clear.

Upvotes: 1

Related Questions