Python Forum
Turn coordinates in string into a tuple
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Turn coordinates in string into a tuple
I have a dict that stores chunk coordinates and their content in a game I'm making. It looks like this

chunks = {
'0,0' : <content>,
'0,1' : <content>
What I need is to get the keys, and turn them into coordinates, so I get a tuple like this:

'0,0' -> (0, 0)

Is there any way I can acomplish that?

I worked it out, it actually was pretty simple:
a = '1,2'.split(',')
a = tuple(a)
a = (int(a[0]), int(a[1]))
(1, 2)
Thanks for posting back your solution! You can do it in one line too
print(tuple(int(x) for x in '1,2'.split(',')))
print(tuple(map(int, '1,2'.split(','))))
Personally I prefer the first in Python, since comprehensions are more common than using things like map.
Feel like you're not getting the answers you want? Checkout the help/rules for things like what to include/not include in a post, how to use code tags, how to ask smart questions, and more.

Pro-tip - there's an inverse correlation between the number of lines of code posted and my enthusiasm for helping with a question :)
Or you could save all you settings to a JSON file and not have to do any conversion at all. It might be worth investigating.
this looks like XY problem: You have a dict and use str as key. why not use a tuple as key in dict in the first place?
chunks = {
(0, 0): <content>,
(0, 1): <content>
and because these are coordinates, you can go a step further and use namedtuple
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs


Possibly Related Threads…
Thread Author Replies Views Last Post
  TypeError: __str__ returned non-string (type tuple) Anldra12 1 1,448 Apr-13-2021, 07:50 AM
Last Post: Anldra12
  code with no tuple gets : IndexError: tuple index out of range Aggam 4 858 Nov-04-2020, 11:26 AM
Last Post: Aggam
  Import CSV data into array and turn into integers DoctorSmiles 5 1,045 Jul-16-2020, 10:47 AM
Last Post: perfringo
  How do i turn my program into a .exe julio2000 1 771 Feb-14-2020, 08:18 PM
Last Post: snippsat
  How do I turn a directory of HTML files into one CSV? glittergirl 2 829 Sep-21-2019, 05:33 PM
Last Post: glittergirl
  Turn py into exe tester21 4 1,425 Jul-22-2019, 04:31 PM
Last Post: nilamo
  How to get first line of a tuple and the third item in its tuple. Need Help, Anybody? SukhmeetSingh 5 1,378 May-21-2019, 11:39 AM
Last Post: avorane
  How to turn screen output into clickable hyperlinks windros 5 1,333 Jan-22-2019, 05:41 PM
Last Post: windros
  Not sure how to turn this into a loop iamgonge 1 1,096 Dec-05-2018, 11:03 PM
Last Post: anandoracledba
  Line of file from string to list to tuple vvarrior 2 3,575 Jun-01-2018, 08:56 PM
Last Post: vvarrior

Forum Jump:

User Panel Messages

Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020