pythonnew at gmail.com
Tue Nov 15 10:39:47 CET 2005
On 11/15/05, Brian van den Broek <broek at cc.umanitoba.ca> wrote:
> Ben Bush said unto the world upon 2005-11-15 01:24:
> <top posting corrected and post trimmed>
> > Unfortunately, the indents got screwed up along the way. But the part
> >>of my code you asked about was:
> >>for item in list1:
> >> if item in list2:
> >> if item + 1 in list1 and item + 1 in list2:
> >> return True
> >>In some detail:
> <snip my detailed explanation>
> >>Does that clarify it?
> >>Finally, your response to Alex would have been much more useful if
> >>you'd quoted the error rather than just asserting that you got an
> >>error :-)
> >>Brian vdB
> Hi Ben,
> first, while there are those on the list/n.g. who differ, the
> majoritarian view is that top posting isn't a good thing. At minimum,
> if someone bothered to correct it, it would be nice if in a further
> follow-up you didn't top-post again :-)
> Second, you might find the tutor list really helpful. It is where I
> learned most of what I know, and I still read it more than c.l.p. It
> is very newbie friendly.
> As for your question:
> >> Hi Brian,
> > regarding "if item + 1 in list1 and item + 1 in list2:",
> > my understanding is this will check whether the following item in
> each list
> > is the same. How does the code permit the situation that the order
> does not
> > matter?
> > For example, for lisA and lisB, the comparison is true and the two
> > have 5 and 6 but different order.
> > lisA=[1,2,3,4,5,6,9]
> > lisB=[1,6,5]
> > Many Thanks!
> There are two distinct issues that might be the source of your
> confusion. I will be explicit about both; pardon if only one applied.
> >>> num_list = [1, 42, 451]
> >>> for item in num_list: print item, type(item)
> 1 <type 'int'>
> 42 <type 'int'>
> 451 <type 'int'>
> Iterating over a list as I did makes item refer to each list member in
> turn. So, item + 1 doesn't refer to the next item (except by accident
> as it were when two items are sequential ints). Rather, it refers to
> int that results from adding 1 to the int that is item.
> You might be thinking of list index notation instead:
> >>> index = 1
> >>> num_list[index], num_list[index + 1]
> (42, 451)
> (General tip: putting a print or two in my original code would have
> cleared that up very quickly.)
> So, that cleared up, are you wondering why item + 1 suffices, instead
> of both item + 1 and item - 1?
> If so, consider that it the list1 has both n and n - 1 in it and we
> iterate over list1 checking each case, eventually item will refer to n
> - 1. In that case, item + 1 = n and we are covered after all. I did as
> I did in my original code thinking it was probably quicker to have
> only one test and pay the cost that there might be a quicker exit from
> the iteration were I to test both item + 1 and item - 1. f it
> mattered, I'd test for speed. Of course, if speed mattered and I could
> ever remember to use sets :-) I'd follow Alex and Duncan's suggestions
> If that doesn't clear it up, give yourself a few short lists and run
> test cases having inserted print statements to see what is going on.
> To stop it all from whizzing by too fast, put in
> raw_input("Hit enter and I'll keep working")
> somewhere in the loops to slow things down.
> Brian vdB
Really appreciate your help!!!
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the Python-list