List / Ganso / dictionary / function knowledge summary

// list array corresponding to high-level language C
storing a plurality of data (a plurality may be any type of data)
is defined in the form:

list1=[12,52,36,63,36]
list2=['liuu','xiaohan','zhangsan'

]
Access: access via an index value

names=['liuwei','zhangsan','xiaoqiang']
print(names[0])		//访问的第0个元素,输出结果为:liuwei
print(names[-1])	//访问最后一个元素。

How to print all of the data list
for circulation by way of printing while loop.

for i in range(len(names)):
		print(names[i])
	或
	for name in names :
		print(name)

Operation to the list
by:
the append
Example:

names=[]
		names.append("liuwei")
		names.append("好凉快")		//
		print(names)
	insert	
例:
names=[]
		names.insert("翻学啦")		//会报错,不知道放在哪,必须制定位置
		names.insert(0,"翻学啦")	//原元素将向后移
		print(names)
	extend
例:	
names2=['张丹丹','路人甲']
	names.extend(names2)		//将names2的列表元素逐个加入到names列表中
删:
remove()
例:
	names=['liuu','xiaohan','zhangsan']
	names.remove('liuu')		//只能删除指定的值
del()
例:
	names=['liuu','xiaohan','zhangsan']
	del  names[0]			//删除指定索引的值
	del names			//删除列表,即列表不存在
pop()
例:
	names=['liuu','xiaohan','zhangsan']
	names.pop(0)			//删除指定索引的值
clear()
例:	
	names=['liuu','xiaohan','zhangsan']
	names.clear()			//清空列表元素,但列表仍存在
	
查:

```css
index
	例:
		names=['liuu','xiaohan','zhangsan']
		print(names.index('xiaohan'))	//打印xiaohan在列表中的索引值
	count
	例:	
		names=['liuu','xiaohan','zhangsan']
		print(names.count('xiaohan'))	//打印出现的次数,如果为0,证明列表没有这个元素。
		
	改:
	

```css

步骤:
		1.先判断有没有这个元素.
		if 'xiaohan' in names
		2.找出所在的位置
		index1=names.index('xiaohan')
		3.修改
		names[index1]='王富贵'
列表的排序
	sort()
	默认是自然顺序排序(从小到大) 
		list1=[10,25,32,12,11,96,25]
		list2=list1.sort()
		print(list1)	//会安顺排序,对原数据直接操作,不会产生值
		print(list2)	//是一个空,
	sort(reverse)
	从大到小排序
		

列表的嵌套使用
	list2=[[1,2,3],[5,6],[10,21,32]]
	list2表示一个列表,元素仍然为一个列表。
	遍历列表 
		

```css
list2=[[1,2,3],[5,6],[10,21,32]]
		for list3 in list2:
			if type(list3) == list:
				for i in list3:
					print(i)

Nested list stored procedure:
Here Insert Picture Description
Ganso:

Tuple if only one element, must be a comma tuple representation: (element 1, element 2, ...) can not be changed: the value of storage elements, can not be modified, can not be deleted
tuple1 = ( 'student 1', 99) to access the elements via index tuple1 = ( 'student. 1', 99)
Print (suple1 [-1]) of additions and deletions do not support tuples, but supports queries

.

查找:
print(tuple1.index[1])
print(tuple1.count[99])

将元祖转换成列表
list1=list(tuple1)	//list可以将其转换成列表
list1[1]=100
tuple1= =tuple(list1) 	///将列表转换成元祖,整体在赋值给tuple1

Dictionary:
1. The dictionary definition of the form
{key1: value, key2: value}
Key unique

2.元素的访问
	country = {"CN":"中国","JP":"日本"}
	print(country['CN'])
	打印结果:"中国"
	如果没有"CN"这个key,则会报错

3.元素的常用方法
	students={'胡来':[],'达瓦':[]}
	students['胡来']
修改元素

	可以直接进行修改
		student={'name':'陈独秀','age':99}
		student['age']=100

添加元素
		student['address']='北京市朝阳区'
		如果字典中没有'address'这个key,则会将这个元素条件到字典中,如果已经包含这个key
		则新值会将旧值覆盖

删除元素
	del 删除
	例:
	删除某个key
		del student['age']
	删除所有的键值对,删除之后,将不能在进行访问
	pop('key')
	

4.元素的遍历
	country = {"CN":"中国","JP":"日本","CA":Canada}
	all_keys = countries.keys()		//单独将KEY取出来
	print(all_keys)
	for key in all_keys:
		print(key)
		print(country[key])
	
	print('CN' in all_keys)	//判断是否包含某个值

Exercises:
Here Insert Picture Description

Code:

print("1.添加学生")
print("2.查找学生")
print("3.展示所有")
print("4.删除学生")
print("5.修改学生")
print("6.退出系统")
student_name=['张三','老吴']
student_age=[25,26]
function=int(input("请输入您想要选择的功能,输入对应的数字——》"))
if function == 1:
      name=input("请输入姓名——》")
      age=int(input("请输入年龄"))
      student_name.append(name)
      student_age.append(age)
      print("恭喜添加成功,姓名:{0}\n年龄:{1}".format(student_name[-1],student_age[-1]))
elif function == 2:
      name=input("请输入需要查找的姓名——》")
      if  student_name.count(name)>0 :
             num=student_name.index(name)
             print("姓名:{0}\n年龄:{1}".format(student_name[num],student_age[num]))
      else :
             print("您要查找的姓名不存在")
elif function == 3 :
      for i  in range(len(student_name)):
            print("姓名:{0} ---年龄:{1}".format(student_name[i],student_age[i]))

elif function == 4:
      name=input("请输入要删除的姓名")
      if  student_name.count(name)>0 :
             num=student_name.index(name)
             student_name.pop(num)
             student_age.pop(num)
             print("删除成功")
      else :
             print("您要删除的姓名不存在")

elif function == 5:
      name=input("请输入要修改的姓名")
      if   student_name.count(name) > 0 :
            num=student_name.index(name)
            print("修改姓名请输入1")
            print("修改年龄请输入2")
            num1=int(input("请输入:"))
            if num1==1 :
                   new_name=input("新的姓名")
                   student_name[num]=new_name
            else :
                    new_age=input("请输入修改后的年龄")
                    student_age[num]=new_age
                    print('修改成功:{0}{1}'.format(student_name[num],student_age[num]))
      else :
             print("您要修改的姓名不存在")
else :
      pass

function:

In general, a piece of code needs to be reused many times, and this code also have a specific function, we will organize this code as a separate function module, the function module can be called a function
definition and call functions

Function is defined: def function name (parameter): calling the function name of the function block (arguments)

练习 封装函数打印99乘法表:
def show99():
		i = 1
		while i < 10:
		j = 1
		while j <= i:
			print("%d * %d = %d\t"%(j,i,i*j),end=" ")
			j += 1
		i += 1
		print("")
	函数的调用,只有调用函数,函数中的代码才会被执行
	show99()


	使用help(自定义方法名) 可以查看函数的文档说明
	help(show99)
	显示结果:
	Help on function show99 in module __main__:

Function parameters

Def method names defined function with parameters (parameters ...): a block of code (with code, parameters can be better appreciated as a variable defined) function call with parameters
method name (actual parameters)

Released four original articles · won praise 2 · views 79

Guess you like

Origin blog.csdn.net/weixin_44537595/article/details/104025193