String loops in Python
Posted
by
Steve Hunter
on Stack Overflow
See other posts from Stack Overflow
or by Steve Hunter
Published on 2013-11-01T15:28:41Z
Indexed on
2013/11/01
15:54 UTC
Read the original article
Hit count: 172
I have two pools of strings and I would like to do a loop over both. For example, if I want to put two labeled apples in one plate I'll write:
basket1 = ['apple#1', 'apple#2', 'apple#3', 'apple#4']
for fruit1 in basket1:
basket2 = ['apple#1', 'apple#2', 'apple#3', 'apple#4']
for fruit2 in basket2:
if fruit1 == fruit2:
print 'Oops!'
else:
print "New Plate = %s and %s" % (fruit1, fruit2)
However, I don't want order to matter -- for example I am considering apple#1-apple#2 equivalent to apple#2-apple#1. What's the easiest way to code this?
I'm thinking about making a counter in the second loop to track the second basket and not starting from the point-zero in the second loop every time.
© Stack Overflow or respective owner