0

This is probably something trivial but I can't seem to find a decent solution and/or I'm searching for the wrong thing.

When I call this:

validation_data = validation.map(tuple)

I get the following output:

[(61.0, 3864.0), (61.0, 3889.0)]

I actually want the result to be Integers, so I changed the code to this instead:

validation_data = validation.map(lambda xs: [int(x) for x in xs])

But now the format of the output changes (notice it's now square brackets like an array instead of smooth brackets)

[[61, 3864], [61, 3889]]

I'm not sure what happens here. Why does the output format change? And how do I keep the original [()] format instead of [[]]?

2
  • 1
    What is validation? Commented May 22, 2016 at 20:31
  • Those are square brackets. Angular brackets would be <>. Commented May 22, 2016 at 20:37

1 Answer 1

2
validation_data = validation.map(lambda xs: tuple(int(x) for x in xs))

This is essentially just combining the first statement in your question with the second. Either the only reason you had round brackets the first time was precisely because you used tuple, or the first statement was redundant.

Note that tuple(int(x) for x in xs) is a special feature of Python syntax. It's short for tuple((int(x) for x in xs)). That argument is a generator comprehension, and when a function has a single argument which is a generator comprehension you can remove the extra round brackets. tuple([int(x) for x in xs]) would also work and would be closer to your code.

You could also do:

validation_data = validation.map(lambda xs: tuple(map(int, xs)))
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.