Python的列表if语句列表

时间:2018-03-17 12:37:29

标签: python list

我有一个类似的列表:

var conditions = new PropertyCondition[3];
conditions[0] = new PropertyCondition(AutomationElement.IsEnabledProperty, true);
conditions[1] = new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Button);
var conditionEnabledButtons = new AndCondition(conditions);

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

[['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

我怎样才能发表声明?

2 个答案:

答案 0 :(得分:3)

此列表理解可以实现,并保留列表结构列表:

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

答案 1 :(得分:1)

这就是你要找的东西:

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