Pythonの簡単な紹介、一方向チェーンテーブルの例
前言:まず1冊の本《head first Python》を推薦して、この本は実践を重視して、文法の地方を話すのは多くなくて、もしいくつかプログラミングの基礎の学生は推薦Pythonエディタpycharm Pythonの中でよく使うタイプの紹介を見ることができます:1、リストのリストはPythonの中でJAVAの中の配列のようにリストして、リストの中で多くのタイプの値を格納することができます各値は、例えば、movie=[‘a’,’b’,’c’]に対応し、最初の要素の下付き文字は0であり、movie[0]を使用して、aがリストで-1のような下付き文字を同時にサポートすることができ、例えばmovie[-1]を得ることができ、練習の下で同時にリストにlen、pop、append、remove、Insertなどのメソッド作成リストのフォーマット:list=[‘1’,’a’,’sdf’]2、メタグループメタグループの要素は、重複が許されず、ソートメソッドリストのソートには2つのメソッドがあります.1つはsortで、1つは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たとえば、def list=[]#はlist変数を定義し、list変数は空のリストです.
简単な绍介を通じて、次の実戦训练をして、これはあなたにPythonのデータ构造とプログラミングの思想に対してとても大きい助けがあります今日の目标はチェーンテーブルです(ps:当初このチェーンテーブルを书いて、私を5日苦しめて、まる5日..)
简単な绍介を通じて、次の実戦训练をして、これはあなたに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()