I have a simple question.
I have a dictionary: table = collections.defaultdict(set), and a previously defined grammar consisting of rules like the following:
Rule(('Noun', ('money',)))
Rule(('Noun', ('book',)))
Rule(('S', ('book',)))
Now, when I type this, nothing happens.
for rule in grammar:
if rule.symbols == ("book"):
table[col - 1, col].add(rule.head)
When I type this, the entry is added.
for rule in grammar:
if rule.symbols == ("book",):
table[col - 1, col].add(rule.head)
The only difference between the two is the comma behind "book". What does this comma do and why is it necessary?
In the first case, ("book") the parens are just a way of grouping the expression. The value of that expression is just the string "book".
In the second case, it's creating a tuple, with one element in it.
You need add comma to make it a tuple, otherwise it's just a string.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With