美文网首页
跟着销售学python系列16-list

跟着销售学python系列16-list

作者: 日月山人 | 来源:发表于2015-06-05 00:22 被阅读43次

原读书:https://github.com/qiwsir/StarterLearningPython/blob/master/111.md

以下为笔记:

这一段时间,很牛没写简书了,到不是自己懒惰了,而是这段学习进入了一个瓶颈了。

第一个例子:
> In [1]: a = [1,2,3,5]

       In [2]: b = [2,1,3,1]

        In [3]: c = a + b

          In [4]: print c
         [1, 2, 3, 5, 2, 1, 3, 1]

列表的相加

第二个例子: 如何让他们元素相加
看过这个例子,自己敲出来,似乎忘了。

In [1]: a = [1,2,3,5]

  In [2]: b = [2,1,3,1]
  
   In [5]: c = []
   lenlist = len(a) if len(a) < len(b) else len(b)
   for i in range(lenlist):
             c.append(a[i]+b[i])
   print  c

[6,6,6,6]

其中之前,自己写过c[i] = a[i] +b[i]
自己的思路 c[0] = 1+2
c[1]= 2+1
发现了indexError错误:
这是为什么呢?
因为c[0] ,在此之前是空的,不存在c[0]。

有一个问题,就是你输入错误,导致的错误,去问别人是很sb。

第三个例子: 利用内置函数zip

Help on built-in function zip in module builtin:

   zip(...)
       zip(seq1 [, seq2 [...]]) -> [(seq1[0], seq2[0] ...), (...)]

      Return a list of tuples, where each tuple contains the i-th element
       from each of the argument sequences.  The returned list is        truncated
       in length to the length of the shortest argument sequence.

~

sequences: 序列。
truncate :截断

    for  i , j  in zip (a,b):
            c.append(i+j)

是不是前面两个更加快了呢?
** 发现用中文阐述,这个函数, 不会表达, 试一下,这个函数的作用就是,返回值是元组组成的列表,每个元祖包含了函数中输入参数的序列对象的对应元素值,返回的列表,截止到序列对象中最短长度。**

**第四个例子: **
dict函数, 把列表转换为字典。

a = { 'c': 2, 'd':1 }

反转一下,In [46]: temp = dict(zip(a.values(),a.keys()))
In [47]: print temp
{2: 'c', 3: 'd'}

我还是发生了一个错误,temp = dict(zip(a.values,a.keys)

help(a) , 看看,都是a.keys(), a.values(). 自己这样是语法错误。

第五个例子:enumerate() 函数

enumerate: 列举

In [49]: week = ['monday','tuesday','wednesday']

In [50]: for i in range(len(week)):
....: print week[i] + ':'+str(i)

用enumerate(),
class enumerate(object)
| enumerate(iterable[, start]) -> iterator for index, value of iterable
|
| Return an enumerate object. iterable must be another object that supports
| 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]), ...

上面的例子: 为序列对象的index,values迭代。
enumerate(week) , 返回一个列举的对象,week必须是另一个可以迭代的对象。对象产生的配对,包括一个计数和值。
yield: 翻译不好

list(iterable).. 返回interable中的iterm(各个元素)的list.

for i,j in list(enumberate(week)):
print j + ':'+str(i)

** 这里面,有一个疑惑,对象返回的是什么。at 0x , 是什么意思? **
enumerate是一种生成器,跟yield有关,至于什么事yield暂时不懂,但是enumrate.next(), 是可以取数据的。具体说明看看这里[http://xianglong.me/article/how-to-code-like-a-pythonista-idiomatic-python/

第六个例子:

raw = "Do you love Canglaoshi? Canglaoshi is a good teacher."

更换canglaoshi.

raw = "Do you love Canglaoshi? Canglaoshi is a good teacher."
raw_item = raw.split(" ")
for (i ,string ) in enumerate(raw_item):
if "Changlaoshi" in string:
raw_item[i]== 'jiezi'

print(raw_item)

其中, 更换 for i ,string in enumrate(raw_item) 为list(enumrate(raw_item))没有关系。
这里我还是犯错了,strint[i] in string

相关文章

网友评论

      本文标题:跟着销售学python系列16-list

      本文链接:https://www.haomeiwen.com/subject/bookqttx.html