Reputation: 572
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 [[]]
?
Upvotes: 0
Views: 53
Reputation: 36043
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)))
Upvotes: 2