本篇为近期的列表的学习总结:
可以通过remove()或者append()像列表中添加元素,需要注意的是append()和remove没有返回值
offer_list=['Allen','Tom']
for i in offer_list:
print('{}, you have passed our interview and will soon become a member of our company.'.format(i))
offer_list.remove('Tom')
offer_list.append('Andy')
for j in offer_list:
print('{}, welcome to join us!'.format(j))
采用split()拆分列表
str1=input()
print(str1.split()) # 按照空格拆分字符串
关于list的更多笔记,参见:https://blog.nowcoder.net/n/e6f560ccf4294f16bcbd2a27319a9529?f=comment
采用len()函数测量字符串长度:
print(len(input().split()))
使用insert()可以向任何位置插入字符,但是需注意insert()无返回值
a=input().split()
a.insert(0,'Allen')
print(a)
使用列表元素的几种方法
关于sort与sorted的区别,即sort会改变原本列表,而sorted不会改变原本的列表
str1 = ['P','y','t','h','o','n']
print(sorted(str1))#sorted只是临时排序原来的列表并不会改变
print(str1)
str1.sort(reverse=True)#sort会改变原来的列表,同时题目要求降序就还需要reverse
print(str1)
二维列表:列表里面还嵌套了列表
关于二维列表可以参考:https://zhuanlan.zhihu.com/p/430443424
s = []
name = ['Niumei', 'YOLO', 'Niu Ke Le', 'Mona']
food = ['pizza', 'fish', 'potato', 'beef']
number = [3, 6, 0, 3]
s.append(name)
s.append(food)
s.append(number)
print(s)
密码游戏:https://blog.nowcoder.net/n/7af2927a367d444982364ed369efe60b?f=comment
牛牛和牛妹一起玩密码游戏,牛牛作为发送方会发送一个4位数的整数给牛妹,牛妹接收后将对密码进行破解。
破解方案如下:每位数字都要加上3再除以9的余数代替该位数字,然后将第1位和第3位数字交换,第2位和第4位数字交换。
请输出牛妹破解后的密码。
相关知识点总结参考如下:
https://blog.nowcoder.net/n/7af2927a367d444982364ed369efe60b?f=comment