前言:
首先推荐一本书《head first Python》,这本书注重实践,讲语法的地方不是很多,如果有些编程基础的同学可以去看看
推荐Python编辑器pycharm
Python中常用的类型简介:
1、List列表
在Python中列表像JAVA中的数组,列表中可以存储许多类型的值,每个值都对应一个下标
例如:movie = [‘a’,’b’,’c’],第一个元素的下标是0,使用movie[0]可以得到a
同时在列表中支持像-1这样的下标,例如movie[-1],可以试着练习下
同时列表还提供了:len、pop、append、remove、insert等方法
创建列表的格式:list = [‘1’,’a’,’sdf’]
2、元组
元组中的元素,不允许重复,并且提供了排序方法
列表中的排序有两个方法,一个是sort,一个是sorted
sort()是直接在原来的元组中进行排序
sorted()是把原来的元组创建一个副本,然后再进行排序,对原来的元组不改变
创建元组的格式:tuple = ()访问元组中元素的方法可以参见上方的列表,两者很类似
3、字典
Python中的字典跟groovy中的闭包类似,都是用来存储键值对的容器
创建字典格式:dic = {‘1’:’a’,’two’:’2’}
访问字典中的值写法:dic[‘1’],这么访问的结果是‘a’
4、for循环
在Python中for循环是可以伸缩的,不需要指定大小
循环格式:for t(变量) in movie(列表):执行语句
例如:for t in movie:
print(t)
注:因为在Python中for循环没有方法体的括号,所以都是用制表符进行控制的
5、while循环
在Python中while循环和for循环类似,大多数情况下,都可以实现相同的功能
循环格式:while 条件:
执行语句
循环格式:while t < len(movie):
print(movie[t])
t = t+1
注:咋Python中与for循环相比,while更纯粹,没有过多的包装
6、定义变量
在Python中定义变量很简单
格式:def 变量名 = 类型
通过按的简单介绍,下面来个实战训练,这会让你对Python的数据结构和编程思想有很大帮助
今天的目标就是链表(ps:当初写这个链表,折磨了我5天,整整5天。。。)
# -*- coding: utf-8 -*-
class Element: #elment类是链表中的元素类
def __init__(self,data,age,point):
#Element类有两个属性 data表示数据 point表示指向
self.data = data
self.age = age
self.point = point
class Linkedlist: #Linkedlist类是整个链表的类
def __init__(self):
self.head = None
self.current = self.head
self.tail = self.head
#Linkedlist类有三个属性,的初始值都为空
def add(self,elment):
#增加链表元素的方法:判断增加的元素是否符合链表元素要求
if self.head is None:
#如果是空链表,就把元素添加到链表中
self.head = elment
self.current = elment
self.tail = elment
#元素末尾的指向为空
elif self.head is not None:
#如果不是空链表,链表中原来有元素
self.current.point = elment
#相当于element的point属性变成新添的元素
self.tail = elment
#tail对象等于新天元素
self.current = elment
#current对象等于新添元素
#self.tail.point = None
print("链表元素增加成功:" + self.current.data)
else:
print("链表元素增加失败!")
def edit(self,elment,new_elment):
if self.head is None:
print('链表为空,无法修改')
current = self.head
while current is not None:
if current.data ==elment:
current.data = new_elment
print('修改成功')
return
else:
current = current.point
else:
print('没有找到匹配的值')
def dele(self,delstr):
if self.head is None:
print('链表为空,无法进行删除操作')
now = self.head
new_now = self.head.point
if now.data ==delstr and new_now is None:
self.head = None
self.current =None
self.tail = None
print('删除成功')
elif now.data ==delstr and new_now is not None:
self.head = new_now
print('删除成功')
else:
while new_now is not None:
if new_now.data == delstr:
now.point = new_now.point
print('删除成功')
break
now = new_now
new_now = new_now.point
if new_now is None:
print('未找到要删除的元素')
def insert(self,old_data,new_data,shunxu):
new_element = Element(new_data,None)
present = self.head
new_present = self.head.point
if self.head is None:
self.head = new_element
self.current = new_element
self.point = new_element
if self.head is not None and self.head.point is None:
if self.head.data == old_data:
if shunxu == 'bef':
self.head = new_element
new_element.point = present
self.current = new_element
self.tail = new_element
return
elif self.head.data == old_data:
if shunxu == 'aft':
self.current.point = new_element
self.current = new_element
self.tail = new_element
return
if self.head is not None and self.head.point is not None:
while self.head is not None:
if self.head.data == old_data:
if shunxu == 'bef':
self.head = new_element
new_element.point = present
#new_element.point = present
print('插入成功96')
return
if shunxu == 'aft':
self.head.point = new_element
new_element.point = new_present
print('插入成功101')
return
if new_present.data == old_data:
if shunxu == 'bef':
present.point = new_element
new_element.point = new_present
return
if shunxu == 'aft':
temp = new_present.point
new_present.point = new_element
new_element.point = temp
return
present = present.point
new_present = new_present.point
def displayLinkedList(self):
current = self.head
if current is None:
print("链表为空2")
return
while current is not None:
if current.point is None:
print(current.data + '年龄是: '+current.age)
return
elif current.point is not None:
print(current.data + '年龄是: '+current.age)
current = current.point
# break
def paixu(self,new_list):
new_list = []
if self.head is None:
print('链表为空1')
else:
while self.head is not None:
if self.head.point is not None:
new_list.append(self.head)
self.head = self.head.point
else:
new_list.append(self.head)
break
for i in range(len(new_list)-1):
for j in range(len(new_list)-1):
if new_list[j].age > new_list[j+1].age:
temp = new_list[j]
new_list[j] = new_list[j+1]
new_list[j+1] = temp
i = 0
self.head = new_list[0]
self.head.point = new_list[i]
while self.head is not None and i < len(new_list)-1:
self.head = new_list[0]
self.current = new_list[i]
self.tail = new_list[i]
new_list[i].point = new_list[i+1]
i = i+1
while self.head is not None and self.current.point is None:
self.head = new_list[0]
self.current = new_list[i]
self.tail = new_list[i]
new_list[i].point = None
return new_list
el1 = Element('张三','20',None)
el2 = Element('张四','22',None)
el3 = Element('张五','19',None)
el4 = Element('张六','30',None)
a = []
iList = Linkedlist()
iList.add(el1)
iList.add(el2)
iList.add(el3)
iList.add(el4)
#iList.edit('张六','李五')
#iList.dele('张四')
#iList.insert('张六','张100','bef')
iList.paixu(a)
iList.displayLinkedList()