| 
 | 
 
 
 楼主 |
发表于 2021-7-25 15:59:08
|
显示全部楼层
 
 
 
 本帖最后由 Python初学者8号 于 2021-8-9 10:18 编辑  
 
3.4 序列 
 
以上的数据结构我其实还搞混淆了,因为我居然把集合字典与这些放一起了,这个有点区别的吧,好像是二元的数据结构。我现在挖个坑,然后有机会看看数据结构中怎么讲这个集合字典的数据结构的。 
 
大概就是总结一下这些数据结构的要点 
 
 
 
1.创建 
其实要说这些结构还是很简洁的,直接用几个符号就可以表示,包括后面集合。 
所以创建都是通过符号就可以,当然还有函数——list(),tupel()和str()函数 
 
2021年8月9日09:52:53需要提一句的是,一个高级用法——列表表达式。这个用法比较高级,因此在某些地方可以起到简洁的效果,并且有时候还可以兼具可读性强的作用。 
 
一般的结构是[i for i in range(10)]这样的:一元/多元元素表达式    for  元素表达式 in 可迭代对象 限制条件 
 
或者更简洁的来说就是:元素表达式  循环结构 
 
其实应该这么理解:这个语法出现的目的是很显然的——简洁性和可读性。从for的结构本身来理解,这个结构本身就是一个可迭代结构,而for中本来也就包含可迭代。因此用for来作为一种快速得到sequence的想法自然而然就有了 
 
这个回答写的很好很全面,还有这个 
 
当然,鉴于我今年冬天的时候学过可是现在我明显差点记不住了,说明知识不牢固,现在给定一下需求 
(1)一般的表达式 如 
(2)可迭代对象表达式   
- list_c = [7 * c for c in "python"]
 
  复制代码 
 
(3)带条件的表达式 
- list_d = [d for d in range(6) if d % 2 != 0]
 
  复制代码 
(4)多个for循环的表达式- list_e = [(e, f * f) for e in range(3) for f in range(5, 15, 5)]
 
  复制代码 注意,对于多个for循环可以看成是矩阵哦 
*(5)嵌套的 
- list_g = [[x for x in range(g - 3, g)] for g in range(22) if g % 3 == 0 and g != 0]
 
  复制代码 我的需求,主要就是(1)(2)(3)了,其他的次之 
 
 
2.访问 
都是使用index和切片——这点直接体现了序列这个性质。的 
 
其实我认为和字典以及集合来说,这个索引就有很大的区别了吧,就像数学中的数列和离散函数一样,一个是数字对应一个是自变量对应的 
 
3.编辑 
到底能不能改变本身的元素这一点还是很有区别的。 
 
个人理解是,列表就像是套丛书,科幻类的随便放,无所谓,有新人了老人的书也可以 撤掉; 
 
而到了这个元组就像是成套的书一样,比如二十四史,可以加新的,但是已经有的不能动了 
 
字符串是用于处理文本的,和tupel对比的话其实很像,但是呢,两个区别就是形式上没有逗号和功能主要用于文字处理的区别了,可以认为是tupel的兄弟了 
 
4.方法 
第一个很相似的线性运算+和*很有意思,使得他们的相似度很高 
 
具有这些内建方法BIF,这些是py自带的函数,就像print函数一样。 
(1)max和min,用于序列元素同类的时候选出最大值,字符亦可以 
(2)len 度量个数 
(3) sum,求和,只能用于数字(整型和浮点型)的序列 
(4)sorted 直接创建一个新的排序之后的序列,返回值是新的 
- >>> l = [1,5,6,9,3,7,8]
 
 - >>> sorted(l)
 
 - [1, 3, 5, 6, 7, 8, 9]
 
 - >>> l
 
 - [1, 5, 6, 9, 3, 7, 8]
 
 - >>> id(sorted(l))==id(l)
 
 - False
 
  复制代码 千万注意,参数是序列,返回值是列表哦 
- >>> t
 
 - (1, 5, 6, 9, 3, 7, 8)
 
 - >>> sorted(t)
 
 - [1, 3, 5, 6, 7, 8, 9]
 
 - >>> s=str(t)
 
 - >>> s
 
 - '(1, 5, 6, 9, 3, 7, 8)'
 
 - >>> sorted(s)
 
 - [' ', ' ', ' ', ' ', ' ', ' ', '(', ')', ',', ',', ',', ',', ',', ',', '1', '3', '5', '6', '7', '8', '9']
 
  复制代码 (5)reversed 参数 序列,返回一个迭代器 
- >>> t
 
 - (1, 5, 6, 9, 3, 7, 8)
 
 - >>> s
 
 - '(1, 5, 6, 9, 3, 7, 8)'
 
 - >>> l
 
 - [1, 5, 6, 9, 3, 7, 8]
 
 - >>> reversed(t)
 
 - <reversed object at 0x0000020E4358C280>
 
 - >>> reversed(s)
 
 - <reversed object at 0x0000020E4358C160>
 
 - >>> reversed(l)
 
 - <list_reverseiterator object at 0x0000020E4358C250>
 
 - >>> type(reversed(t))==type(reversed(s))
 
 - True
 
  复制代码 (6)enumerate 
单词的中文意思是列举枚举 
产生的是一系列的tuple组成的迭代对象 
- >>> str1="abcd"
 
 - >>> for i in enumerate(str1):
 
 -         print(i,type(i))
 
  
 
- (0, 'a') <class 'tuple'>
 
 - (1, 'b') <class 'tuple'>
 
 - (2, 'c') <class 'tuple'>
 
 - (3, 'd') <class 'tuple'>
 
  复制代码 (7)zip 
现在发现zip这个名字很形象啊,因为zip就是拉链啊——将两排东西对应上就是zip的作用 
 
参数是两个序列,当然个数要对上啊,然后缝合起来,每个元素是二院的tuple,这个和enumerate很像 
- >>> z1 = ['传','统','美','德']
 
 - >>> z2 = ('儒','雅','随','和')
 
 - >>> for i in zip(z1,z2):
 
 -         print(i,type(i))
 
  
-         
 
 - ('传', '儒') <class 'tuple'>
 
 - ('统', '雅') <class 'tuple'>
 
 - ('美', '随') <class 'tuple'>
 
 - ('德', '和') <class 'tuple'>
 
  
- >>>z1 = ['传','统','美','德']
 
 - >>> z2 = ('儒','雅','随','和','nmsl')
 
 - >>> z3=('儒','雅','随')
 
 - >>> for i in zip(z1,z2):
 
 -         print(i,type(i))
 
  
-         
 
 - ('传', '儒') <class 'tuple'>
 
 - ('统', '雅') <class 'tuple'>
 
 - ('美', '随') <class 'tuple'>
 
 - ('德', '和') <class 'tuple'>
 
 - >>> for i in zip(z1,z3):
 
 -         print(i,type(i))
 
  
-         
 
 - ('传', '儒') <class 'tuple'>
 
 - ('统', '雅') <class 'tuple'>
 
 - ('美', '随') <class 'tuple'>
 
 
  复制代码 给出这几个不熟悉的help文档: 
- <font color="#ff0000">>>> help(enumerate)</font>
 
 - Help on class enumerate in module builtins:
 
  
- class enumerate(object)
 
 -  |  enumerate(iterable, start=0)
 
 -  |  
 
 -  |  Return an enumerate object.
 
 -  |  
 
 -  |    iterable
 
 -  |      an object supporting iteration
 
 -  |  
 
 -  |  The enumerate object yields pairs containing a count (from start, which
 
 -  |  defaults to zero) and a value yielded by the iterable argument.
 
 -  |  
 
 -  |  enumerate is useful for obtaining an indexed list:
 
 -  |      (0, seq[0]), (1, seq[1]), (2, seq[2]), ...
 
 -  |  
 
 -  |  Methods defined here:
 
 -  |  
 
 -  |  __getattribute__(self, name, /)
 
 -  |      Return getattr(self, name).
 
 -  |  
 
 -  |  __iter__(self, /)
 
 -  |      Implement iter(self).
 
 -  |  
 
 -  |  __next__(self, /)
 
 -  |      Implement next(self).
 
 -  |  
 
 -  |  __reduce__(...)
 
 -  |      Return state information for pickling.
 
 -  |  
 
 -  |  ----------------------------------------------------------------------
 
 -  |  Static methods defined here:
 
 -  |  
 
 -  |  __new__(*args, **kwargs) from builtins.type
 
 -  |      Create and return a new object.  See help(type) for accurate signature.
 
  
- <font color="#ff0000">>>> help(reversed)</font>
 
 - Help on class reversed in module builtins:
 
  
- class reversed(object)
 
 -  |  reversed(sequence, /)
 
 -  |  
 
 -  |  Return a reverse iterator over the values of the given sequence.
 
 -  |  
 
 -  |  Methods defined here:
 
 -  |  
 
 -  |  __getattribute__(self, name, /)
 
 -  |      Return getattr(self, name).
 
 -  |  
 
 -  |  __iter__(self, /)
 
 -  |      Implement iter(self).
 
 -  |  
 
 -  |  __length_hint__(...)
 
 -  |      Private method returning an estimate of len(list(it)).
 
 -  |  
 
 -  |  __next__(self, /)
 
 -  |      Implement next(self).
 
 -  |  
 
 -  |  __reduce__(...)
 
 -  |      Return state information for pickling.
 
 -  |  
 
 -  |  __setstate__(...)
 
 -  |      Set state information for unpickling.
 
 -  |  
 
 -  |  ----------------------------------------------------------------------
 
 -  |  Static methods defined here:
 
 -  |  
 
 -  |  __new__(*args, **kwargs) from builtins.type
 
 -  |      Create and return a new object.  See help(type) for accurate signature.
 
  
- <font color="#ff0000">>>> help(sorted)</font>
 
 - Help on built-in function sorted in module builtins:
 
  
- sorted(iterable, /, *, key=None, reverse=False)
 
 -     Return a new list containing all items from the iterable in ascending order.
 
 -     
 
 -     A custom key function can be supplied to customize the sort order, and the
 
 -     reverse flag can be set to request the result in descending order.
 
  
- <font color="#ff0000">>>> help(zip)</font>
 
 - Help on class zip in module builtins:
 
  
- class zip(object)
 
 -  |  zip(*iterables) --> zip object
 
 -  |  
 
 -  |  Return a zip object whose .__next__() method returns a tuple where
 
 -  |  the i-th element comes from the i-th iterable argument.  The .__next__()
 
 -  |  method continues until the shortest iterable in the argument sequence
 
 -  |  is exhausted and then it raises StopIteration.
 
 -  |  
 
 -  |  Methods defined here:
 
 -  |  
 
 -  |  __getattribute__(self, name, /)
 
 -  |      Return getattr(self, name).
 
 -  |  
 
 -  |  __iter__(self, /)
 
 -  |      Implement iter(self).
 
 -  |  
 
 -  |  __next__(self, /)
 
 -  |      Implement next(self).
 
 -  |  
 
 -  |  __reduce__(...)
 
 -  |      Return state information for pickling.
 
 -  |  
 
 -  |  ----------------------------------------------------------------------
 
 -  |  Static methods defined here:
 
 -  |  
 
 -  |  __new__(*args, **kwargs) from builtins.type
 
 -  |      Create and return a new object.  See help(type) for accurate signature.
 
  
  复制代码 
 
 
 
 
 
 |   
 
 
 
 |