簡體   English   中英

將整數追加到一個列表中

[英]Appending Co-Ordinates to a list as integers

我的代碼是

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]]

這里有很多變調。 我用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)]

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM