joydeba
joydeba

Reputation: 1245

Invalid syntax error of Python 3's str.split in Python 2

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

Answers (3)

joydeba
joydeba

Reputation: 1245

I found another solution:

import re
word, vector = re.split('', line)[0], re.split('', line)[1:]

Upvotes: 0

6502
6502

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

James Shapiro
James Shapiro

Reputation: 6236

Why not:

arr = line.split()
word = arr[0]
vector = arr[1:]

?

Upvotes: 2

Related Questions