[英]write tuples inside a list instead of another lst
I'm running the code below, and it is not working except with a list inside the list. 我正在运行下面的代码,除了列表中的列表外,它无法正常工作。 What I want my outcome to be is tuples inside the list, as in inside parentheses.
我希望我的结果是列表中的元组,如括号内。 I tried writing parentheses instead of [] but I'm getting a syntax error.
我尝试写括号而不是[],但是出现语法错误。
import xlrd
loc= (r"C:\Users\USER\Documents\MATLAB/results.xlsx")
book = xlrd.open_workbook(loc)
trans_agg= [[sheet.cell_value(r, c) for c in range (1,4)]for r in range(1,sheet.nrows)];
print (trans_agg)
what i'm getting as a result; 结果是我得到了什么;
[[-38.9681738617398, 35.220378959142636, 5.072061633983768], [0.3058390201478544, -32.91896886230741, 5.0755459371576075], [0.16656016242917882, 37.047877143990405, 5.08026752912042]]
what i actually want; 我真正想要的是
[(-38.9681738617398, 35.220378959142636, 5.072061633983768), (0.3058390201478544, -32.91896886230741, 5.0755459371576075), (0.16656016242917882, 37.047877143990405, 5.08026752912042)]
You have to replace in your list comprehension the square brackets for a call to function tuple 您必须在列表理解中替换方括号以调用函数元组
trans_agg= [tuple(sheet.cell_value(r, c) for c in range (1,4))
for r in range(1, sheet.nrows)]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.