将字母对转换为笛卡尔坐标或将 ('A','B') 转换为 (1,2)
我正在编写我的第一个程序。我一直在使用 python3 来做到这一点。我遇到了一个我认为非常基本的问题,我无法在任何地方找到讨论的答案,或者也许我太笨了而看不到它。感谢您对我缺乏经验的耐心。
组合 ('gimp', 2) 返回
[('g', 'i'), ('g', 'm'), ('g', 'p'), ('i', 'm'), ('i', 'p'), ('m', 'p')]
我希望这些是笛卡尔坐标,所以我这样做了...
(a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,x) = range(17)
希望字母能与数字切换,但字母周围讨厌的 (') 阻止了这种情况。
我怎样才能让列表更像?
[(g,i),(g,m)...]
我尝试使用替换功能,但没有运气
combo.replace("'","")
,或者是否有更直接的方法将字母对的组合列表转换为坐标,我很想知道。
预先感谢您的帮助。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
从另一个 Python 新手那里,您可以制作一个字典,其中字母作为键,您选择的数字作为每个字母的值,并为每个字母获取一个数字。
现在,您继续生成笛卡尔坐标的元组。如果您无法从列表中生成对,请查看此答案来自单个列表的对
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.
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
谢谢 stef 和 apalala 的帮助。我无法实现你的解决方案,但它确实帮助我想象了一个我能够工作的不优雅的解决方案。我想在这里发布我的解决方案,以防像我这样缺乏编码头脑的人可能会发现它有用。
我所做的是检查每个可能组合的组合列表,如果存在组合,我将扩展一个带有相应数字的新坐标列表。我认为这就是上述解决方案将以更有效的方式产生的效果。我的目的只包括120种可能的组合,所以虽然繁琐,但这个方法是可行的。
然后我重复了最后三行(a,c)(a,d)等等。
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.
I then repeated those last three lines for (a,c) (a,d) and so forth.
您可能想尝试使用字符的序数值对您有利。如果您使用字符序数值相对于“a”序数值的偏移量,则应该获得适当的坐标。
尝试如下操作:
这会将
coords
设置为:注意:如果您在单词中使用非 ASCII 字符,此操作将无法正常工作。
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:
This will set
coords
to:Note: This won't work properly if you use non-ASCII characters in your word.