代码之家  ›  专栏  ›  技术社区  ›  mpx

用Python理解嵌套列表时出现名称错误

  •  1
  • mpx  · 技术社区  · 4 年前

    目标是将以下for循环转换为嵌套的理解列表。

    txt=['-100:200','-15:0','0:15','30:45']
    
    
    all_t=[]
    for t in txt:
        all_t.append([int ( idx ) for idx in t.split(":")])
    

    进入之内

    all_t=[int(idx) for idx in t.split(":") for t in txt]
    

    名称错误:未定义名称“t”

    1 回复  |  直到 4 年前
        1
  •  2
  •   Mick    4 年前
    txt=['-100:200','-15:0','0:15','30:45']
    all_t=[[int(idx) for idx in t.split(":")] for t in txt]
    print(all_t)