Collection & Recommended:

1. CN - 论坛中看到。 - EN 英文原文真的真的很好好好T_T,看得让人感动T_T

总结个人感兴趣的问题(以下部分参照上面):

1. python 的 函数传值by value 和 函数传址by reference.

In Python everything is an object and all variables hold references to objects.

As result you can not change the value of the reference but you can modify the object if it is mutable. 

                                                    - See more at: THIS greet blog

理解:

Python,所有都是 对象。变量是 对象的引用。

不能改变 引用本身的值(如果想想C++的引用,reference是不可以再绑定到其他对象,可变的是引用指向的值),但是,如果对象是可变的(mutable),你可以修改对象。

整理: Python 核心编程 4.8 - 标准类型分类
  存储模型 更新模型 访问模型
数值 原子 不可变immutable 直接
字符串 原子 不可变immutable 顺序
元组 容器 不可变immutable 顺序
列表 容器 可变mutable 顺序
字典 容器 可变mutable 映射

首先,python中 没有 ’传值‘ 和 ’传址‘ 的概念。

其次,我们希望探索的是, ’传值‘ 和 ’传址‘代表的行为,这有点像‘鸭子判别法’(From - <python cookbook>).

  ’传值‘行为 代表: 变量a作为参数传入函数func(x)中,func(x)对x的修改不会影响 a本身。

  ’传址‘行为 代表: 变量a作为参数传入函数func(x)中,func(x)对x的修改   影响 a本身。

最后,python 类似‘传值’行为: immutable类(标准类型中)

      类似‘传址’行为: mutable类(标准类型中)

  我的疑问是,如果我自己创建了一个类class,作为变量传入函数,那么class的行为会是什么?这个行为,我可以控制(’传值‘行为 & ’传址‘行为)吗?

2. def test(*argv1, **argv2): pass

http://www.cnblogs.com/kevin922/p/3157752.html - NO.53 - 可变数量的参数。

  C语言的“可变数量参数”怎么用呢?

3. List/Dict comprehensions 列表解析 字典解析 

  3. 1. List Comprehensions  - THIS

  1. # try to create a list like this:
  2. >>> matrix = [
  3. ... [1, 2, 3, 4],
  4. ... [5, 6, 7, 8],
  5. ... [9, 10, 11, 12],
  6. ... ]
  7. >>> vec1 = [[(x+y) for y in range(0, 4)] for x in range(1, 13, 4)]

  另一个有趣的想法

  1. >>> a = [1, 2, 3]
    >>> b = [4, 5, 6]
    >>> zip(a b)
  2. [(1, 4), (2, 5), (3, 6)]
  3. >>> map(list, zip(a,b))
  4. [[1, 4], [2, 5], [3, 6]]

  zip()的实现THIS

  1. def zip(*args):
  2. if not args:
  3. raise TypeError('zip() expects one or more sequence arguments')
  4. ret = []
  5. i = 0
  6. try:
  7. while 1:
  8. item = []
  9. for s in args:
  10. item.append(s[i])
  11. ret.append(tuple(item))
  12. i = i + 1
  13. except IndexError:
  14. return ret

  我的问题是 zip(*argv) 这样可以unzip 但是 *argv 是什么?

    *argv 是什么 - Answer

    zip(*argv) - 析:

  1. >>> zip(*[(1, 4), (2, 5), (3, 6)])
  2. [(1, 2, 3), (4, 5, 6)]
  3. >>> zip((1, 4), (2, 5), (3, 6))
  4. [(1, 2, 3), (4, 5, 6)]

    最后!!!

    -------------------------------   zip(*[iter(s)]*n). 解释 THIS_1, THIS_2

    自己的理解是:

    在一个list创建迭代器iter,然后因为操作符优先级(operator priority)的缘故,首先创建了 列表[iter, iter, iter],注意,是 列表, 然后对列表unpacking操作作为参数传入了zip(iter, iter, iter)函数.

    先暂停,让我们考虑zip(a_list, b_list),假如len(a_list) 等于 len(b_list), zip(a_list, b_list) 的行为是: (a_list[0], b_list[0])组成tuple元组,(a_list[1], b_list[1])组成tuple元组,(a_list[2], b_list[2])组成tuple元组...

    再回到zip(iter, iter, iter):

      1. 跟C++相同,iter作为迭代器, 假想指针p, ++p之后,p位置会后移, 再次调用p时,要注意p所指向的位置.

      2. 同时考虑python对 参数 left-to-right 从左至右 的处理: zip(iter@1, iter@2, iter@3). 注意, iter@1, iter@2, iter@3 就只是一个 iter.

      3. zip(iter@1, iter@2, iter@3) 行为是: 遍历 iter@1(iter[0]), iter[0] -> iter[1]; 遍历 iter@2(iter[1]),  iter[1] -> iter[2]; 遍历 iter@3(iter[2]),  iter[2] -> iter[3]; (iter[0], iter[1], iter[2])组成tuple元组. 依次类推.

  1. # 注意()改变的运算符优先级
  2.  
  3. >>> a = [1, 2, 3, 4, 5, 6, 7, 8, 9]
  4.  
  5. >>> zip(*[iter(a)]*3)
  6. [(1, 2, 3), (4, 5, 6), (7, 8, 9)]
  7.  
  8. >>> zip(*([iter(a)]*3))
  9. [(1, 2, 3), (4, 5, 6), (7, 8, 9)]
  10.  
  11. >>> x = iter(a)
  12. >>> zip(x, x, x)
  13. [(1, 2, 3), (4, 5, 6), (7, 8, 9)]
  14.  
  15. >>> x_list = [x] * 3
  16. >>> x_list
  17. [<listiterator object at 0x99dafec>, <listiterator object at 0x99dafec>, <listiterator object at 0x99dafec>]
  18.  
  19. >>> a
  20. [1, 2, 3, 4, 5, 6, 7, 8, 9]
  21. >>> x = iter(a)
  22. >>> zip(x, x, x)
  23. [(1, 2, 3), (4, 5, 6), (7, 8, 9)]
  24. >>> zip(*[x, x, x])
  25. []
  26. >>> zip(x, x, x)
  27. []
  28. >>> x = iter(a)
  29. >>> zip(*[x, x, x])
  30. [(1, 2, 3), (4, 5, 6), (7, 8, 9)]
  31. >>> x = iter(a)
  32. >>> zip(*[x]*3)
  33. [(1, 2, 3), (4, 5, 6), (7, 8, 9)]

  34. # This is the most interesting one I want to show u
    >>> a = [1, 2, 3, 4, 5, 6, 7, 8, 9]
    >>> zip(*[iter(a)]*3)
    [(1, 2, 3), (4, 5, 6), (7, 8, 9)]
    # 注意到zip()的实际行为了吗?在最后一次循环,无法分组的情况下,我的猜测是发生异常,终止了。
    >>> zip(*[iter(a)]*4)
    [(1, 2, 3, 4), (5, 6, 7, 8)]
  1. # ----啊啊啊 就这样了!!!你们一定能明白了!!我想得头疼了!!!!

  

  3.2. dict comprehension - THIS

  1. >>> [(k, v) for k in range(4) for v in range(4)]
  2. [(0, 0), (0, 1), (0, 2), (0, 3), (1, 0), (1, 1), (1, 2), (1, 3), (2, 0), (2, 1), (2, 2), (2, 3), (3, 0), (3, 1), (3, 2), (3, 3)]
  3.  
  4. >>> # like above, try this:
  5. >>> {k : v for k in range(0, 4) for v in range(0, 4)}
  6. {0: 3, 1: 3, 2: 3, 3: 3}
  7.  
  8. >>> # why above happened??? Try answer.
  9.  
  10. >>> {k: k for k in range(4)}
  11. {0: 0, 1: 1, 2: 2, 3: 3}
  12. >>> {k: 3 for k in range(4)}
  13. {0: 3, 1: 3, 2: 3, 3: 3}
  14.  
  15. # Explain:
  16. dict_test = {}
  17. for k in range(0, 4):
  18. for v in range(0,4):
  19. dict_test[k] = v # finally, dict_test[0] = 3
    # In a word, 不允许一个key对应多个value.

  And, this is interesting: THIS

  And, this is basic part: THIS

    

4. Python 中 global的使用,产生的问题。

http://www.douban.com/group/topic/40864587/http://www.douban.com/group/topic/40864587/

  1. #! /usr/bin/python
  2. # Filename: global_test.py
  3.  
  4. x = 1
  5. print x
  6.  
  7. def change():
  8. global x
  9. x += 1
  10. print 'change:', x
  11.  
  12. change()
  13. print 'x:', x

 5. Python - generator

  offical docs - THIS - 感觉看得不够

  offical docs - THIS - search 'Generators'

generator

A function which returns an iterator. It looks like a normal function except that it contains yield statements for producing a series a values usable in a for-loop or that can be retrieved one at a time with the next() function. Each yield temporarily suspends processing, remembering the location execution state (including local variables and pending try-statements). When the generator resumes, it picks-up where it left-off (in contrast to functions which start fresh on every invocation).

-----

Generators are a simple and powerful tool for creating iterators. They are written like regular functions but use the yield statement whenever they want to return data. Each time next() is called, the generator resumes where it left-off (it remembers all the data values and which statement was last executed). An example shows that generators can be trivially easy to create.

  1. # a generator that yields items instead of returning a list
  2. def firstn(n):
  3. num = 0
  4. while num < n:
  5. yield num
  6. num += 1

  看完上面,我的第一感受是- -!

  yield 和 return!如果在for-loop中,不就是 continue 和 break 的区别吗!?!?

  再一次深刻知道了自己的俗,是怎么的俗- -!明明是 Such a elegant language~ 我都能解释得这么俗- -!果然是吊丝编程啊- -!

  1. # yield_test.py
  2. def reverse_yield(data):
  3. for index in range(len(data)-1, -1, -1):
  4. yield data[index]
  5.  
  6. for char in reverse_yield('test1'):
  7. print char

  8. # interpretor
  9. kevin922@MyWorld:~/pyShell$ python yield_test.py
  10. 1
  11. t
  12. s
  13. e
  14. t

  try again:

  1. # yield_test.py
  2. def reverse_yield(data):
  3. for index in range(len(data)-1, -1, -1):
  4. data[index]
  5.  
  6. for char in reverse_yield('test1'):
  7. print char
  1. # interpretor
  1. kevin922@MyWorld:~/pyShell$ python yield_test.py
  2. Traceback (most recent call last):
  3. File "yield_test.py", line 5, in <module>
  4. for char in reverse_yield('test1'):
  5. TypeError: 'NoneType' object is not iterable

  again, try:

  1. # yield_test.py
  2. def reverse_yield(data):
  3. for index in range(len(data)-1, -1, -1):
  4. print data[index]
  5.  
  6. reverse_yield('test1')
  7.  
  8. # interpreter
  9. kevin922@MyWorld:~/pyShell$ python yield_test.py
  10. 1
  11. t
  12. s
  13. e
  14. t

 6. Python - decorator

  涉及‘元编程’的概念NO.9, 讲解

7. Python - with

  NO.10, 讲解

 8. Python里面如何拷贝一个对象?

  python cookbook 4.1

 9. python内存管理

 10. and-or技巧,说实话,这个技巧很糟糕,我不想使用。

11. python的pass语句作用

  空语句。

12、如何在一个function里面设置一个全局的变量?
  解决方法是在function的开始插入一个global声明

13. 参考NO.4的解答

  1. a = 1
  2.  
  3. def change():
  4. a = a + 1 # why is this wrong?
  5. print a
  6.  
  7. pirnt a
  8.  
  9. # --------------------
  10.  
  11. a = 1
  12.  
  13. def change():
  14. # without that, why is this right?
  15. print a
  16.  
  17. pirnt a

Python interview preparing的更多相关文章

  1. python interview questions

    referce:python interview questions top 50 refercence:python interview questions top 15 summary Q: wh ...

  2. [译]Python面试中8个必考问题

    1.下面这段代码的输出结果是什么?请解释. def extendList(val, list=[]): list.append(val) return list list1 = extendList( ...

  3. Python面试常见的问题

    So if you are looking forward to a Python Interview, here are some most probable questions to be ask ...

  4. python进阶资源

    本文为不同阶段的Python学习者从不同角度量身定制了49个学习资源. 初学者 Welcome to Python.org https://www.python.org/ 官方Python站点提供了一 ...

  5. 我的 2019 年 Python 文章榜单

    现在是 2020 年的第一天,我相信从昨天开始,各位的信息流里肯定充斥了各式各样的年度盘点/回顾/总结/记录之类的内容.虽然来得稍晚了,但我还是想给诸位送上这一篇文章. 我将在本文中列出自己于 201 ...

  6. Python Coding Interview

    Python Coding Interview Python Advanced Use enumerate() to iterate over both indices and values Debu ...

  7. Some Interview Questions About Python

    一大波超链接即将袭来 Django认证流程 Python实现阶乘 Python文件处理 Python统计日志文件IP出现次数 JSON数据解析 JSON数据解析2 买卖股票的最佳时期 读取一个大文件比 ...

  8. 《Craking the Coding interview》python实现---01

    ###题目:给定一个字符串,判断其中是否有重复字母###思路:将重复的字符放入到list中,并进行计数统计###实现:伪代码.函数.类实现###伪代码:string=s #给定的字符串list=[] ...

  9. 《Craking the Coding interview》python实现---02

    ###题目:翻转一个字符串###思路:从字符串的最后一位开始,依次取###实现:伪代码.函数.类实现#伪代码: #01string=sNew_s=""for i in range( ...

随机推荐

  1. 解决java写入xml报错org.w3c.dom.DOMException:DOM002 Illeg

    Exception is -- > org.w3c.dom.DOMException: DOM002 Illegal character 字符不被允许 org.w3c.dom.DOMExcept ...

  2. Sql注入中连接字符串常用函数

    在select数据时,我们往往需要将数据进行连接后进行回显.很多的时候想将多个数据或者多行数据进行输出的时候,需要使用字符串连接函数.在sqli中,常见的字符串连接函数有concat(),group_ ...

  3. POJ 1062 昂贵的聘礼(Dijkstra)

    题意 : 真真是做POJ第一次遇到中文题,好吧,虽然语言通了,我一开始也没看懂样例什么意思,题意的话就是说这个探险家想娶酋长的女儿,但是没有钱,酋长说他可以用祭司的水晶球或者皮袄来换取少花一部分钱,同 ...

  4. MYSQL日常操作命令再熟悉

    1,创建用户及密码: CREATE USER 'user'@'%' IDENTIFIED BY 'password'; 2,创建数据库: create database PDB_chengang de ...

  5. [SQL Server 系] T-SQL数据库的创建与修改

    创建数据库 USE master; GO CREATE DATABASE ToyUniverse ON ( NAME = ToyUniverse_Data, FILENAME = 'F:\Projec ...

  6. Android 使用MediaRecorder录音

    package com.example.HyyRecord; import android.app.Activity; import android.content.Intent; import an ...

  7. SaaS系列介绍之十一: SaaS商业模式分析

    1 配置模式 中国企业很多是人治,管理弹性非常大,公司的政策经常变化,管理流程.业务变化也非常大,发展也非常快;一个公司今年是10个人,明年是100个人,后年可能是1000人.管理机制.方法处于经常变 ...

  8. [hackerrank]Manasa and Stones

    https://www.hackerrank.com/contests/w2/challenges/manasa-and-stones 简单题. #include<iostream> us ...

  9. Linux进程管理知识整理

    Linux进程管理知识整理 1.进程有哪些状态?什么是进程的可中断等待状态?进程退出后为什么要等待调度器删除其task_struct结构?进程的退出状态有哪些? TASK_RUNNING(可运行状态) ...

  10. IntelliJ IDEA集成svn

    IntelliJ IDEA如何集成svn呢? 1. 首先配置下载并配置svn软件,推荐使用SlikSvn. 下载地址https://sliksvn.com/download/,下载最近版本