Reputation: 107
i have a tuple which looks like this b (u'3.7', 9023)
. i want to use it in the following statement :
if list(self.ballot_number) == msg.ballot_number and b in waitfor:
print "hello"
i have checked and the ballotnumber section of the if condition is worrking fine. it's the second part that's not returning true. the waitfor set looks like this : set([((u'3.0', 9002), (u'3.1', 9005), (u'3.2', 9008), (u'3.3', 9011), (u'3.4', 9014), (u'3.5', 9017), (u'3.6', 9020), (u'3.7', 9023))])
.
The value of tuple are there in the set but they are not able to match it probably because of different data types. i don't want to split the tuple into individual elements as i have to use it collectively later in the code. How can i run my if statement?
building of set
waitfor = set()
print "in scout"
for a in self.acceptors:
print "acceptor",a
a = tuple(tuple(p) for p in self.acceptors)
waitfor.add(a)
print "waitfor",waitfor
Upvotes: 0
Views: 6845
Reputation: 365657
The problem is that you’re not building the set that it seems you think you’re building, and as a result it can’t be used the way you want to use it.
Your code does this:
waitfor = set()
print "in scout"
for a in self.acceptors:
print "acceptor",a
a = tuple(tuple(p) for p in self.acceptors)
waitfor.add(a)
print "waitfor",waitfor
So, for each acceptor, you’re not adding that acceptor to the set, you’re adding the tuple of all acceptors to the set. You do this over and over, but because it’s a set, and you’re adding the same tuple over and over, you end up with just one element, that big tuple of all of the acceptors. Which is exactly what you see—notice the extra parentheses in your output, and the fact that if you print out len(waitfor)
it’s just 1.
And this means that none of the p
values you later check with p in waitfor
are going to be in waitfor
, because the only thing that’s actually in it is the giant tuple that contains all those pairs, not any of the pairs itself.
It’s like adding “The State of California” to a phonebook millions of times, instead of adding the millions of Californians, and then asking “Is Jerry Brown in the phonebook?” No, he’s not. There’s no bug in how you’re searching the phonebook; the bug was in creating the phonebook. So that’s the part you need to fix.
So, what you want is:
waitfor = set()
print "in scout"
for a in self.acceptors:
print "acceptor",a
waitfor.add(tuple(a))
print "waitfor",waitfor
Or, more simply, this one-liner:
print “in scout”
waitfor = set(tuple(p) for p in self.acceptors)
print “waitfor”, waitfor
Or, if your version of Python is new enough for set comprehensions (I think that means 2.7, but don’t quote me on that), it’s slightly more readable:
print “in scout”
waitfor = {tuple(p) for p in self.acceptors}
print “waitfor”, waitfor
Upvotes: 1
Reputation: 6129
You've got too many brackets in your set, so it's only looking for a single element.
len(waitfor)
# 1
If you try:
waitfor = set([(u'3.0', 9002), (u'3.1', 9005), (u'3.2', 9008), (u'3.3', 9011), (u'3.4', 9014), (u'3.5', 9017), (u'3.6', 9020), (u'3.7', 9023)])
Then your test:
(u'3.7', 9023) in waitfor
# True
Will work!
Upvotes: 0