将坐标列表隐藏到元组列表中 [英] Coverting list of Coordinates to list of tuples

查看:166
本文介绍了将坐标列表隐藏到元组列表中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



from:



<$>

p $ p> a_list = ['56,78','72,67','55,66']

到:

  list_of_tuples = [(56,78),(72,67) ,(55,66)] 

我试过做一个for循环来转换每个元素但是输出的格式如下:

  list_of_tuples = [('5','6',' 7','8'),('7','2','6','7'),('5','5','6','6')] 
code>

任何帮助我在做什么错误在这里将不胜感激。

<编辑:修正了预期的输出,坐标和元组之间没有空格。

解决方案

你可以使用list comprehension:

  result = [元组(map(int,element.split(',')))for a_list元素

编辑:由@ Lol4t0缩短版本



如前所述,元素之间的空格来自打印列表。数据结构中没有实际的空间。然而,如果你想打印你的列表没有任何空格,你可以做:

pre $ print $ str(result).replace( ,)


I'm trying to covert a list of coordinates to a list of tuples:

from:

a_list = ['56,78','72,67','55,66']

to:

list_of_tuples = [(56,78),(72,67),(55,66)]

I've tried doing a for, in loop to convert each element in a_list to a tuple however the output is in the form:

list_of_tuples = [('5', '6', '7', '8'), ('7', '2', '6', '7'), ('5', '5', '6', '6')] 

Any help on what I am doing wrong here would be greatly appreciated.

EDIT: Fixed the expected output, no spaces between coordinates and tuples.

解决方案

You can use list comprehension:

result = [tuple(map(int,element.split(','))) for element in a_list]

edit: shorter version by @Lol4t0

As mentioned the spaces between the elements come from printing the list. There are no actual "spaces" in the data structure. However, if you wish to print your list without any spaces you can do:

print str(result).replace(" ","")

这篇关于将坐标列表隐藏到元组列表中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆