Reputation: 1245
The below line works fine for me in python3. How can I fix it for python 2.
word, *vector = line.split()
Error in Python 2:
word, *vector = line.split() ^ SyntaxError: invalid syntax
Upvotes: 0
Views: 693
Reputation: 1245
I found another solution:
import re
word, vector = re.split('', line)[0], re.split('', line)[1:]
Upvotes: 0
Reputation: 114539
This does the trick without polluting the namespace...
word, vector = (lambda x,*y:(x, y))(*line.split())
however I don't think many Python programmers would love it
Upvotes: 1
Reputation: 6236
Why not:
arr = line.split()
word = arr[0]
vector = arr[1:]
?
Upvotes: 2