-1

I'm creating a tuple from a string as demonstrated below.

line = 'StartupGrind , 26\n' #string

w,f=line.strip().split(',') #get a tuple
w,f= (w.strip(),int(f.strip())) #make it string,int

Can it be accomplished in a single line?

midori
  • 4,807
  • 5
  • 34
  • 62
samsamara
  • 4,630
  • 7
  • 36
  • 66
  • 16
    Why do people always want "one line" in Python? You *can* do it in one line, but try to focus on simplicity and readability instead. – Maroun Feb 16 '16 at 06:54
  • simply because i would like to learn how to do it if possible! and i'm still learning list comprehensions in python! – samsamara Feb 16 '16 at 07:00
  • I don't see how a list comprehension could be useful here. You have two elements and you want to treat them differently. You could lose the first strip, as the later strips will do its job. – Paul Rooney Feb 16 '16 at 07:07
  • 3
    @KillBill list comprehension is not useful here. You can do something like `w, f = line.split(',')[0], int(line.split(',')[1].strip())` - I don't really like this because `split` is called twice here. – Maroun Feb 16 '16 at 07:17
  • I don't know the rules but you should consider posting such questions to [Programming Puzzles & Code Golf](http://codegolf.stackexchange.com/). It might be more appropriate there (packing such code into one line is a bad practice). – jfs Mar 04 '16 at 02:25

6 Answers6

2

Don't try to do everything in one line, your task is actually not a good example for one line code, incline to more readable and maintainable code. Though here is a one liner and it's very readable:

w, f = (int(e) if e.strip().isdecimal() else e.strip() for e in line.split(','))
Ethan Furman
  • 63,992
  • 20
  • 159
  • 237
midori
  • 4,807
  • 5
  • 34
  • 62
1

Use a one-liner only if it makes your solution better. Looks like a one-liner makes it more difficult to understand what the algorithm does. Therefore, stay with two lines, but get rid off all unneeded .strip() calls and parentheses:

w, f = line.split(',')
w, f = w.strip(), int(f)

This has the fewest characters and seems to be the most readable.

miradulo
  • 28,857
  • 6
  • 80
  • 93
Mike Müller
  • 82,630
  • 20
  • 166
  • 161
0

It's not one line like you seem to desire, but works well and is readable.

linesplit = line.strip().split(' , ')
w, f = linesplit[0], int(linesplit[1])

If you aren't sure whats going on here, strip removes the \n, split(' , ') will create a list ['StartupGrind', '26'], and the second line sets w to 'StartupGrind and f to 26.

If you really wanted this in one line I suppose this would work:

w, f = line.split(' , ')[0], int(line.strip().split(' , ')[1])
John Howard
  • 61,037
  • 23
  • 50
  • 66
0

You should prefer 2-lines variant from @Mike Müller's answer:

w, f = line.split(',')
w, f = w.strip(), int(f)

Here's yet another one-liner, not recommended:

[(w, f)] = [(word.strip(), int(n)) for word, n in [line.split(',')]]
Community
  • 1
  • 1
jfs
  • 399,953
  • 195
  • 994
  • 1,670
-1

Here is a one line answer

w,f = map(lambda x: int(x) if x.strip().isdigit() else x, 'StartupGrind , 26\n'.strip().split(','))
Ethan Furman
  • 63,992
  • 20
  • 159
  • 237
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
-1

I don't know if this helps. But you could try this.

w,f = line[:line.index(',')], int(line[line.index(',')+1:].strip())
Ray
  • 627
  • 1
  • 7
  • 19
  • Are you redefining the built-in `str()` as a side effect of your solution? – cdlane Feb 16 '16 at 07:54
  • sorry. i am new to Python. I didn't know that str is a reserved word. Why is there no error message when I use a reserved word? – Ray Feb 16 '16 at 08:23
  • @Ray: It isn't reserved, it's a built-in type, and anything that is not a reserved word can be rebound (aka redefined). – Ethan Furman Mar 04 '16 at 00:51