Python的list if语句列表



我有一个列表,如下所示:

[['a', 'b', 'null', 'c'], ['d', 'e', '5', 'f'], ['g' ,'h', 'null' ,'I'] ]

我想将所有字符串大写,但是当我尝试时:

x = 0
for row in range(1, sheet.nrows):
    listx.append(sheet.cell(row, x))
    [x.upper() for x in x in listx]
x += 1

我得到了:

TypeError: 'bool' object is not iterable

我该如何为它发表声明?

这个列表理解可以做到这一点,并保留你的列表结构:

listx = [['a', 'b', 'null', 'c'], ['d', 'e', '5', 'f'], ['g' ,'h', 'null' ,'I'] ]
[[x.upper() for x in sublist] for sublist in listx]

返回:

[['A', 'B', 'NULL', 'C'], ['D', 'E', '5', 'F'], ['G', 'H', 'NULL', 'I']]
这是

你要找的:

l = [['a', 'b', 'null', 'c'], ['d', 'e', '5', 'f'], ['g' ,'h', 'null' ,'I']]
new_list = []
for row in l:
    new_list.append([x.upper() for x in row])

最新更新