0
>>> inverseIndex={'p':{1,2,3},'a':{2,3,4},'v':{5,6,7}}
>>> query={'p','a','v'}
>>> s=[inverseIndex[s] for s in query]
>>> s
[{2, 3, 4}, {1, 2, 3}, {5, 6, 7}]
>>> [ s[len(s)-1].update(s[i]) for i in range(len(s)-1) ]
[None, None]

Why does the snippet produce [None,None] as output? I was expecting [{2, 3, 4}, {1, 2, 3}, {1,2,3,4,5,6,7}].

zhangyangyu
  • 8,520
  • 2
  • 33
  • 43
prs11
  • 27
  • 1
  • 6

2 Answers2

2

.update() edits the set in place. It does not return the set being updated. Infact, it does not return anything. A function that does not return anything defaults to returning None. Here, you are better off using a normal for-loop:

for i in range(len(s)-1):
    s[len(s)-1].update(s[i])

Also, you can narrow your for-loop down to:

>>> for i in s[:-1]: 
...     s[-1].update(i)
... 
>>> s
[set([2, 3, 4]), set([1, 2, 3]), set([1, 2, 3, 4, 5, 6, 7])]

This first slices the list so we iterate through every element but the last one. We then update the last one with every other set in the list. This whole syntax is called the Python Slice Notation.

Community
  • 1
  • 1
TerryA
  • 58,805
  • 11
  • 114
  • 143
0

update returns None (Python's way of void function which doesn't have ruturn value). So, the values in your list are all None's.

sashkello
  • 17,306
  • 24
  • 81
  • 109
  • 2
    Why downvote? Explain. This is the first answer and I'm quite sure it is correct. – sashkello Jul 14 '13 at 12:49
  • Maybe because you didn't provide any solution… The OP wrote what he's expecting. – tamasgal Jul 14 '13 at 18:22
  • "Why does the snippet produce [None,None] as output?" is the question. I didn't provide solution because other guys did it first and I didn't want to copy it... – sashkello Jul 14 '13 at 23:22