0

I'm writing my first program. I've been using python3 to do so. I've run into a problem that I think is so basic I can't find the answer discussed anywhere, or maybe I'm just too dense to see it. thank you for your patience with my inexperience.

combinations ('gimp', 2) returns

[('g', 'i'), ('g', 'm'), ('g', 'p'), ('i', 'm'), ('i', 'p'), ('m', 'p')]

I want these to be Cartesian coordinates, so I've done this...

(a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,x) = range(17)

in hopes that the letters will be switched with numbers, but the pesky (')'s around the letters prevent that.

How can I get the list to be more like?

[(g,i),(g,m)...]

I've tried using the replace function without luck

combo.replace("'","")

or if there's a more straight forward way to turn a list of the combinations off lettered pairs into coordinates I'd love to know it.

Thank you in advance for your help.

Apalala
  • 9,017
  • 3
  • 30
  • 48
Eli
  • 11
  • 1
  • 3

3 Answers3

0

From another newbie in Python, you could make a dictionary with the letters as keys and the numbers of your choice as values for each letter and the fetch a number for each letter.

#1 make a dict
prefValues = {'g' : 1, 'i' : 2, 'm' : 3, 'p' : 4}

#2 make a list 
word = []
for char in 'gimp':
    #if the letter is in the dict
    if char in prefValues:
        #append in the list the corresponding value
        word.append(prefValues[char])   

Now you continue to produce the tuples for the cartesian coordinates. If you have trouble producing the pairs from the list check out this answer Pairs from single list

Community
  • 1
  • 1
Stef K
  • 469
  • 8
  • 13
  • Strings (enclosed in single or double quotation marks) are not the same as identifiers (at least in most languages). You don't tell how the program should behave, and that would be required to help with an answer. If all you care about is how the output looks, it is easy. – Apalala Mar 11 '11 at 01:37
  • For Python "word" and 'word' are both strings, sorry if messed up. – Stef K Mar 11 '11 at 02:02
0

Thank You stef and apalala for your help. I wasn't able to actualize your solution, but it did help me visualize an inelegant solution that I was able to make work. I wanted to post my solution here in case someone with as little coding acumen as me may find it useful.

What I did was check the list of combinations that I had for every possible combination and if a combination was present I extended a new list of coordinates with the corresponding numbers. I think this is what the solution above would yield in a much more effective way. My purposes only included 120 possible combination, so while tedious, this method was workable.

 combo = (list (combinations (abcdefghijlmnop, 2)) 

 cords = []
 if ('a','b') in combo:
      cords.append ((0,1))
 else:quit

I then repeated those last three lines for (a,c) (a,d) and so forth.

Eli
  • 11
  • 1
  • 3
0

You might want to try using the ordinal values of the characters to your advantage. If you use the offset of the ordinal value of the character from the ordinal value of 'a', you should get the appropriate coordinate.

Try something like:

from itertools import combinations

word = "gimp"

coords = [(ord(x) - ord('a'), ord(y) - ord('a')) for x, y in combinations(word.lower(), 2)]

This will set coords to:

[(6, 8), (6, 12), (6, 15), (8, 12), (8, 15), (12, 15)]

Note: This won't work properly if you use non-ASCII characters in your word.

mthjones
  • 26
  • 1
  • 3