简体   繁体   中英

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM