标签:lex for 判断 int 去掉 索引 for 循环 div lower
for i in li: li[li.index(i)] = i.strip() #li[根据i找到对应索引 ] 为原来的str,等于,新的去掉空白的str;更新了列表。 print(li)
打印结果为:
[‘alec‘, ‘aric‘, ‘Alex‘, ‘Tony‘, ‘rain‘]
for i in li: li[li.index(i)] = i.strip() if i.strip().upper().startswith(‘A‘) and i.strip().endswith(‘c‘): print(i.strip()) print(li)
打印结果为:
alec aric [‘alec‘, ‘aric‘, ‘Alex‘, ‘Tony‘, ‘rain‘]
3、同理可得
字典:
dic = {‘k1‘: "alex", ‘k2‘: ‘ aric‘, "k3": "Alex", "k4": "Tony"} for k in dic: dic[k] = dic[k].strip() if dic[k].upper().startswith(‘A‘) and dic[k].endswith(‘c‘): print(dic[k]) print(dic) # 打印结果: aric {‘k1‘: ‘alex‘, ‘k2‘: ‘aric‘, ‘k3‘: ‘Alex‘, ‘k4‘: ‘Tony‘}
元组:
tu = ("alec", " aric", "Alex", "Tony", "rain") tu = list(tu) for i in tu: tu[tu.index(i)] = i.strip() if i.strip().upper().startswith(‘A‘) and i.strip().endswith(‘c‘): print(i.strip()) print(tu) 打印结果: alec aric [‘alec‘, ‘aric‘, ‘Alex‘, ‘Tony‘, ‘rain‘]
标签:lex for 判断 int 去掉 索引 for 循环 div lower
原文地址:https://www.cnblogs.com/hexiaorui123/p/9940201.html