将Co-Ordinates作为整数附加到列表中

时间:2017-10-16 17:35:13

标签: python python-3.x

我的代码是,

n = int(input("Enter the Number of Lines : "))
s = []
c = []

for i in range(2): #n=2
    m = int(input("Line's Number : "))
    s.append(m)
    c.append(input("Enter Co-ordinates : ").split())

输入行1:(1,2),(3,4)
输入行2:(6,7),(9,0)

输出应为:[[1,3,6,9],[2,4,7,0]]

1 个答案:

答案 0 :(得分:0)

这里有很多换位。我使用ast.literal_eval(以评估元组)然后进行一些zip调用,但也有一些更简单的东西:

import ast

for m,i in enumerate(["(1,2),(3,4)","(6,7),(9,0)"]):  # hardcoded the input
    s.append(m)
    c.append(zip(*ast.literal_eval(i)))

result = [a+b for a,b in zip(*c)]

结果:

[(1, 3, 6, 9), (2, 4, 7, 0)]