【算法与数据结构1】 集合,映射,列表,链表

集合,映射,列表

认识数据类型

with open ("txt.txt",'r') as f:
	s=''
	article=f.readlines()
	#print(article)
	for line in article:
		s+=line

	s=s.replace("," ," ")
	s=s.replace("." ," ")
	s=s.replace("!" ," ")
	s=s.replace('"' ," ")
	s=s.replace(":" ," ")
	s=s.replace("?" ," ")
	s=s.replace("\n" ," ")

	word_list=s.split(' ')
	word_set=set(word_list)
	#print(word_set)
	word_dict=dict()

	for word in word_list:
		if word not in word_dict:
			word_dict[word] = 1
		else:
			word_dict[word] +=1
	print(word_dict)

通过分析一个文本的词频,体验不同数据类型的应用和区别
输出

{'I': 15, 'am': 2, 'happy': 1, 'to': 59, 'join': 3, 'with': 13, 'you': 7, 'today': 9, 'in': 20, 'what': 1, 'will': 27, 'go': 8, 'down': 4, 'history': 2, 'as': 20, 'the': 101, 'greatest': 1, 'demonstration': 1, 'for': 9, 'freedom': 20, 'of': 99, 'our': 17, 'nation': 10, '': 184, 'Five': 1, 'score': 1, 'years': 5, 'ago': 1, 'a': 37, 'great': 5, 'American': 4, 'whose': 1, 'symbolic': 1, 'shadow': 1, 'we': 18, 'stand': 3, 'signed': 1, 'Emancipation': 1, 'Proclamation': 1, 'This': 5, 'momentous': 1, 'decree': 1, 'came': 2, 'beacon': 1, 'light': 1, 'hope': 4, 'millions': 1, 'Negro': 13, 'slaves': 2, 'who': 4, 'had': 1, 'been': 2, 'seared': 1, 'flames': 1, 'withering': 1, 'injustice': 3, 'It': 4, 'joyous': 1, 'daybreak': 1, 'end': 2, 'long': 6, 'night': 1, 'bad': 2, 'captivity': 1, 'But': 4, 'one': 10, 'hundred': 4, 'later': 4, 'still': 4, 'is': 23, 'not': 13, 'free': 3, 'One': 3, 'life': 1, 'sadly': 1, 'crippled': 1, 'by': 8, 'manacles': 1, 'segregation': 2, 'and': 41, 'chains': 1, 'discrimination': 1, 'lives': 1, 'on': 5, 'lonely': 1, 'island': 1, 'poverty': 1, 'midst': 1, 'vast': 1, 'ocean': 1, 'material': 1, 'prosperity': 1, 'languished': 1, 'corners': 1, 'society': 1, 'finds': 1, 'himself': 1, 'an': 4, 'exile': 1, 'his': 3, 'own': 1, 'land': 3, 'And': 12, 'so': 4, "we've": 3, 'come': 10, 'here': 3, 'dramatize': 1, 'shameful': 1, 'condition': 1, 'In': 2, 'sense': 1, "nation's": 1, 'capital': 1, 'cash': 2, 'check': 5, 'When': 2, 'architects': 1, 'republic': 1, 'wrote': 1, 'magnificent': 1, 'words': 3, 'Constitution': 1, 'Declaration': 1, 'Independence': 1, 'they': 3, 'were': 1, 'signing': 1, 'promissory': 2, 'note': 3, 'which': 5, 'every': 10, 'was': 2, 'fall': 1, 'heir': 1, 'promise': 1, 'that': 24, 'all': 7, 'men': 5, 'yes': 1, 'black': 4, 'well': 1, 'white': 6, 'would': 2, 'be': 33, 'guaranteed': 1, 'unalienable': 1, 'Rights': 1, 'Life': 1, 'Liberty': 1, 'pursuit': 1, 'Happiness': 1, 'obvious': 1, 'America': 5, 'has': 5, 'defaulted': 1, 'this': 15, 'insofar': 1, 'her': 1, 'citizens': 1, 'color': 2, 'are': 8, 'concerned': 1, 'Instead': 1, 'honoring': 1, 'sacred': 1, 'obligation': 1, 'given': 1, 'people': 3, 'back': 9, 'marked': 1, 'insufficient': 2, 'funds': 2, 'refuse': 2, 'believe': 2, 'bank': 1, 'justice': 8, 'bankrupt': 1, 'We': 12, 'there': 5, 'vaults': 1, 'opportunity': 1, 'give': 1, 'us': 4, 'upon': 1, 'demand': 1, 'riches': 1, 'security': 1, 'have': 17, 'also': 1, 'hallowed': 1, 'spot': 1, 'remind': 1, 'fierce': 1, 'urgency': 2, 'Now': 5, 'no': 2, 'time': 5, 'engage': 1, 'luxury': 1, 'cooling': 1, 'off': 2, 'or': 1, 'take': 1, 'tranquilizing': 1, 'drug': 1, 'gradualism': 1, 'make': 3, 'real': 1, 'promises': 1, 'democracy': 1, 'rise': 3, 'from': 16, 'dark': 1, 'desolate': 1, 'valley': 3, 'sunlit': 1, 'path': 1, 'racial': 2, 'lift': 1, 'quicksands': 1, 'solid': 1, 'rock': 1, 'brotherhood': 3, 'reality': 1, "God's": 3, 'children': 5, 'fatal': 1, 'overlook': 1, 'moment': 1, 'sweltering': 3, 'summer': 1, "Negro's": 2, 'legitimate': 1, 'discontent': 1, 'pass': 1, 'until': 4, 'invigorating': 1, 'autumn': 1, 'equality': 1, 'Nineteen': 1, 'sixty-three': 1, 'but': 2, 'beginning': 1, 'those': 2, 'needed': 1, 'blow': 1, 'steam': 1, 'now': 1, 'content': 2, 'rude': 1, 'awakening': 1, 'if': 2, 'returns': 1, 'business': 1, 'usual': 1, 'neither': 1, 'rest': 1, 'nor': 1, 'tranquility': 1, 'granted': 1, 'citizenship': 1, 'rights': 2, 'The': 2, 'whirlwinds': 1, 'revolt': 1, 'continue': 1, 'shake': 1, 'foundations': 1, 'bright': 1, 'day': 12, 'emerges': 1, 'something': 1, 'must': 8, 'say': 2, 'my': 4, 'warm': 1, 'threshold': 1, 'leads': 1, 'into': 4, 'palace': 1, 'process': 1, 'gaining': 1, 'rightful': 1, 'place': 1, 'guilty': 1, 'wrongful': 1, 'deeds': 1, 'Let': 9, 'seek': 1, 'satisfy': 1, 'thirst': 1, 'drinking': 1, 'cup': 1, 'bitterness': 1, 'hatred': 1, 'forever': 1, 'conduct': 1, 'struggle': 2, 'high': 1, 'plane': 1, 'dignity': 2, 'discipline': 1, 'allow': 2, 'creative': 2, 'protest': 1, 'degenerate': 1, 'physical': 2, 'violence': 1, 'Again': 1, 'again': 1, 'majestic': 1, 'heights': 1, 'meeting': 1, 'force': 2, 'soul': 1, 'marvelous': 1, 'new': 2, 'militancy': 1, 'engulfed': 1, 'community': 1, 'lead': 1, 'distrust': 1, 'many': 1, 'brothers': 2, 'evidenced': 1, 'their': 7, 'presence': 1, 'realize': 2, 'destiny': 2, 'tied': 1, 'up': 4, 'inextricably': 1, 'bound': 1, 'cannot': 6, 'walk': 2, 'alone': 1, 'pledge': 1, 'shall': 5, 'always': 1, 'march': 1, 'ahead': 1, 'turn': 1, 'There': 1, 'asking': 1, 'devotees': 1, 'civil': 1, 'satisfied': 8, 'can': 4, 'never': 3, 'victim': 1, 'unspeakable': 1, 'horrors': 1, 'police': 2, 'brutality': 2, 'bodies': 1, 'heavy': 1, 'fatigue': 1, 'travel': 1, 'gain': 1, 'lodging': 1, 'motels': 1, 'highways': 1, 'hotels': 1, 'cities': 2, 'basic': 1, 'mobility': 1, 'smaller': 1, 'ghetto': 1, 'larger': 1, 'stripped': 1, 'selfhood': 1, 'robbed': 1, 'signs': 1, 'stating': 1, 'whites': 1, 'only': 2, 'Mississippi': 4, 'vote': 2, 'New': 3, 'York': 2, 'believes': 1, 'he': 1, 'nothing': 1, 'No': 1, 'rolls': 1, 'like': 2, 'waters': 1, 'righteousness': 1, 'mighty': 2, 'stream': 1, 'unmindful': 1, 'some': 2, 'out': 3, 'trials': 1, 'tribulations': 1, 'Some': 1, 'fresh': 1, 'narrow': 1, 'jail': 2, 'cells': 1, 'areas': 1, 'where': 3, 'your': 1, 'quest': 2, '--': 3, 'left': 1, 'battered': 1, 'storms': 1, 'persecution': 1, 'staggered': 1, 'winds': 1, 'You': 1, 'veterans': 1, 'suffering': 2, 'Continue': 1, 'work': 2, 'faith': 5, 'unearned': 1, 'redemptive': 1, 'Go': 1, 'Alabama': 3, 'South': 2, 'Carolina': 1, 'Georgia': 3, 'Louisiana': 1, 'slums': 1, 'ghettos': 1, 'northern': 1, 'knowing': 2, 'somehow': 1}

链表

如何自己实现一个类似的结构呢?
可以查询元素、添加元素、插入元素、删除元素

1.定义一个简单的链表

class Intlist(object):
	def __init__(self):
		self.first = None
		self.rest = None

l=Intlist()
l.first = 5
l.rest = Intlist()
l.rest.first = 10
l.rest.rest = Intlist()
l.rest.rest.first = 15

在这里插入图片描述
2.改进:

class Intlist(object):
	def __init__(self,first,rest):
		self.first = first
		self.rest = rest

l=Intlist(5,None)
l=Intlist(10,l)
l=Intlist(15,l)

在这里插入图片描述
添加一个size()方法,
方便用户查询链表的大小

class Intlist(object):
	def __init__(self,first,rest):
		self.first = first
		self.rest = rest

	def size(self):
		if self.rest is None:
			return 1
		else:
			return 1+self.rest.size()

l=Intlist(5,None)
l=Intlist(10,l)
l=Intlist(15,l)
print(l.size())

在这里插入图片描述
不使用递归的方法
利用循环

class Intlist(object):
	def __init__(self,first,rest):
		self.first = first
		self.rest = rest

	def size(self):
		if self.rest is None:
			return 1
		else:
			return 1+self.rest.size()
	def iterative_size(self):
		p=self
		total_size=0
		while p is not None:
			total_size+=1
			p=p.rest
		return total_size

l=Intlist(5,None)
l=Intlist(10,l)
l=Intlist(15,l)
#print(l.size())\
print(l.iterative_size())

易错点,把while p 写成 while p.rest

class Intlist(object):
	def __init__(self,first,rest):
		self.first = first
		self.rest = rest

	def size(self):
		if self.rest is None:
			return 1
		else:
			return 1+self.rest.size()
	def iterative_size(self):
		p=self
		total_size=0
		while p.self is not None:
			total_size+=1
			p=p.rest
		return total_size

l=Intlist(5,None)
l=Intlist(10,l)
l=Intlist(15,l)
#print(l.size())\
print(l.iterative_size())

可执行过程演示:
错误
正确
不理解的读者可以通过该执行过程理解

添加一个get()方法,
方便用户查询某个元素

class Intlist(object):
	def __init__(self,first,rest):
		self.first = first
		self.rest = rest

	def size(self):
		if self.rest is None:
			return 1
		else:
			return 1+self.rest.size()
	def iterative_size(self):
		p=self
		total_size=0
		while p is not None:
			total_size+=1
			p=p.rest
		return total_size

	def get(self,i):
		if i==0:
			return self.first
		else:
			return self.rest.get(i-1)

l=Intlist(5,None)
l=Intlist(10,l)
l=Intlist(15,l)
#print(l.size())\
#print(l.iterative_size())
print(l.get(0))

现在的链表更像是一个“没穿衣服的”数据结构
内部数据是直接暴露出来的,有些地方也是看起来很奇怪

l=Intlist(5,None)
l=Intlist(10,l)
l=Intlist(15,l)

新增加一个类叫IntNode()
SLList新增加一一个方法叫add_ first(), 用来向链表好头位置添加一个元素

class IntNote(object):
    """docstring for IntNote"""
    def __init__(self,i,n):
        self.item=i
        self.next=n
        
class SLList(object):
    
    def __init__(self, x):
        self.first=IntNote(x,None)
        
    def add_first(self,x):
        self.first=IntNote(x,self.first)



l=SLList(5)
l.add_first(15)
l.add_first(20)

比较:
在这里插入图片描述
然而如果我破解出了SLList里面的变量名称,一样可以修改,比如

l.first.next.next=8

我们可以将first变量改为私有的变量

class SLList(object):
	
	def __init__(self, x):
		self.__first=IntNote(x,None)
		
	def add_first(self,x):
		self.__first=IntNote(x,self.__first)

注: 为什么要设计私有变量?

将类的内部细节隐藏起来 用户不需要了解太多类的细节
设计者可以拥有更为安全的对于程序的控制权

以汽车来类比 公共的方法或变量:油门、方向盘
私有的方法或变量,油管管道,施转阀

SLList新增加一一个方法叫add. _last()用来让用户向链表末尾添加一个元素

	def add_last(self,x):
		p=self.__first
		while p.next is not None:
			p=p.next
		p.next = IntNote(x,None)

SLList新增加一一个方法叫size()用来让用户获取当前链表的长度

	def __size(self,p):
		if p.rest is None:
			return 1
		else:
			return 1+self.__size(p.next)

	def size(self):
		return self.__size(self.__first)

每次查询size(都要把整个链表遍历一遍,是不是低效了?
利用缓存机制

class SLList(object):
	
	def __init__(self, x):
		self.__first = IntNote(x,None)
		self.__size = 1
		
	def add_first(self,x):
		self.__size+=1
		self.__first=IntNote(x,self.__first)

	def add_last(self,x):
		self.__size+=1
		p=self.__first
		while p.next is not None:
			p=p.next
		p.next = IntNote(x,None)


	def size(self):
		return self.__size
发布了41 篇原创文章 · 获赞 39 · 访问量 3708

猜你喜欢

转载自blog.csdn.net/AI_LINNGLONG/article/details/104714549