简体   繁体   中英

loop and branching in python

I am just a beginner in python, I have to do my first exercises base on it and I also have the solutions for them but although I am having problems to understand some of them, statements and functions from some command lines.

This is one of the exercise: Create a new empty list called mixlist. This new list will contain colour blends of two neighbouring colours in the colourlist. The first two colour in colour list are 'red' and 'yellow'. Thus, the first item in mixlist will be 'red-yellow' (a mixture of red and yellow). Notice the '_' character. Make a loop, which fills mixlist automatically. Finally, type mixlist to see the result. If the result is wrong you must correct the program.

this is the answer:

colourList = ['red', 'yellow', 'green', 'white']
mixList = []

count = 0
oldx = " "

for x in colourList:
    if count > 0:
        print oldx + "_" + x
        mixList.append(oldx + "_" + x)
    oldx = x
    count += 1

print mixList

I dont understand most of the things in it, like, what is oldx why it means a blank " " at the begining and then at the end is = x so R can not see the differences between oldx and x so I also dont get this stamment: oldx + "_" + x

I am so confused, I need some help...

Thanks to you all in advance for your help

Note that the "answer" is uselessly complicated. A simpler algorithm with better namings and getting rid of the useless "count" variable:

colourList = ['red', 'yellow', 'green', 'white']
mixList = []
prev_colour = None

for current_colour in colourList:
    if prev_colour is not None:
        mix = prev_colour + "_" + current_colour
        print mix
        mixList.append(mix)

    prev_colour = current_colour

print mixList

Now while this is possibly the best "cs 101" version, the pythonic version is a one-liner:

mixList = ["%s_%s" % pair for pair in zip(colourList, colourList[1:])]
 count = 0

Maintain the count variable as you loop through colourList . It is incremented every time round the loop, so at any stage of the loop its value is the position in the list, starting at zero.

 oldx = " "

oldx is always the value of 'the previous item'. The author has chosen to declare the variable at this point in the program, so it has to have some initial value. They have chosen for some reason. It doesn't matter what, its value is changed before it is used. The if later on says 'only print oldx once count > 0 , so oldx has a value`.

 for x in colourList:

Loop through the list. First time round, count will be zero.

     if count > 0:

If count is greater than zero, ie not the first time round, but every other time round. IE don't do this for the first element.

         print oldx + "_" + x

Print the last element plus the current element.

         mixList.append(oldx + "_" + x)

Also put this on the mix list, so we are building up mixlist as we iterate.

     oldx = x
     count += 1

This element will be come the 'last element' next time round.

 print mixList

Print out your new list.

In general for this kind of question, if you have variables and you are not sure how they change through the program, print them out as you go along. You can then follow the execution of your program as the values change.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM