[转]Python 的列表解析式,集合解析式,字典解析式

Python 的列表解析式,集合解析式,字典解析式 这三种都是 python 里面的语法糖。 语法糖,Syntactic Sugar,就是为了写程序时候少出错,发明的一些简便的方法,但不影响这个语法的功能。 (我第一反应是 HP 里面的 chocolate frog,哈哈哈) 1. 列表解析式 list comprehension 通常我们定义有内容 list 时,想对内容进行一些计算再放进去,除了使用 for 循环迭代出列表内的元素,进行计算再放进去,还可以在列表内直接写解析式计算。 1.1 普通版:[expression for i in iterable] 比如,要求 1-10 的每个数字的算术平方根组成的集合。 用 for 循环的话: lst = [] for i in range(1, 11): i = i ** 0.5 #不用pow()是因为这样计算比较快 lst.append(i) 这样看起来就比较繁琐。 用列表解析式的话,就相当于把上面的内容都浓缩起来: lst = [ i**0.5 for i in range(1,11) ] 这样看起来就很清爽,前面是要对 i 做的处理,后面是 i 从哪里迭代,这些都用中括号 [ ] 括起来,是生成一个列表。 1.2 进阶版 [expression for i in iterable if… for j in iterable if… …] 前面还是表达式,但是后面写的是双循环,还有判断条件,就是符合条件的再进前面的表达式。

READ MORE

由《把兴趣当职业到底靠不靠谱》想到的

由《把兴趣当职业到底靠不靠谱》想到的 2018年,梁宏达在他的《梁知》节目中讨论了一期关于职业发展相关的话题,“把兴趣当职业到底靠不靠谱”。 节目中他举了几个把兴趣当职业的例子,都是失败的案例。他总结的原因是:对某个事情产生的兴趣往往来自于做这些事情的人们身上所披的外在的光环。而当你真正了解到、接触到这些事情以后,往往会被其中的艰辛、困难等等吓跑或者吃不消。 他举了名企高管辞职开咖啡馆遇到经营难题的例子、自己爱好乒乓球接触到专业训练时体力不支的例子、郎朗练习钢琴对高强度练习的反感最终坚持下来的例子。等。 他说,人们真正对工作有兴趣,不应该是“爱一行,干一行”, 而是“干一行,爱一行”。意思是只有将工作干好了,得到同行和社会的肯定了,兴趣自然就来了。评论区里网友就说了,说我能做出别人做不出的数学题,自己对数学的兴趣就萌生了。 总结起来,职业选择时,考虑个人兴趣时,老梁认为切忌因为只看到某个职业的外在而忽略了其背后的艰辛和对个人能力天赋的要求。如果根据这样产生的兴趣而选择自己一生的职业,往往会入行以后不满意自己的职业。而很多由兴趣出发的职业选择都会遇到这样的问题。所以,多数从兴趣出发的职业选择往往是不靠谱的。 我想,对绝大多数人来说,职业选择应该首先是个人谋生的手段。如果还需要谋生,即需要社会对个人的认可,而不是个人对社会的挑选。中心在于环境和社会,不是个人。 这牵涉的问题应该是如何看待个人和世界的关系问题。如果一个人以个人为中心,那么他会更倾向于满足自己内心的诉求去做出行为选择,包括职业选择。反过来,以社会为中心和主体,他的选择会迎合社会发展需要,比如选择普遍高薪和人才相对紧缺的行业。 节目中透露出来的一个推论就是,谋生的职业往往是不舒适的,而兴趣往往是带来舒适感的。这二者存在着天生的矛盾。个人没有能力改变社会让谋生变成一个舒适的过程,工作中总有各种因素导致不如意。那么问题就成了工作不如意时,如何进行个人心态调适。 顺境中成长的人,往往太关心自我的感受,牺牲和奉献为其忽略。而这对于职业发展来说,往往是不可或缺的。

READ MORE

Mac 启用NTFS

How to Enable NTFS Write Support in Mac OS X http://osxdaily.com/2013/10/02/enable-ntfs-write-support-mac-os-x/

READ MORE

python3+2 不换行打印,多用于进度条 process bar

用tqdm包实现进度条: tqdm基本用法: from time import sleep from tqdm import tqdm for i in tqdm(range(10000)): sleep(0.01) tqdm更多用法:https://blog.csdn.net/langb2014/article/details/54798823 python3 不换行打印,多用于进度条 process bar process = 0 # process bar for i in user: process += 1 print("\rProcess: %f " % (process/len(user)), end='') process_code() 更多样式: proc += 1 print('\r loading... %.2f %%' % (process/len(user)*100), end='') python2: 对于py2来讲,主要起作用的是末尾的逗号: print '\r loading... {:.3f}' .format( (float(proces))/len(user)*100 ) , print('%.3f' % 1.12345678) # Let's take 3 decimal places print'{:.3f}'.format(3.14159)

READ MORE

面向的phthon2+3 的场景,Anaconda 安装+环境配置+管理

standard procedure in pyCharm for creating environment when Anaconda installed Create a conda env via pyCharm open pyCharm create new project – select Conda as New env using. Locations: ~\Anaconda_x\envs\py3env PythonVersion: python3.6 (python2.x as alternate) Conda Excutable: ~\Anaconda_x\Scripts\conda.exe ☑️⊙Make available to all projects you can open terminal tab(Alt + F12) in pyCharm and manage the packages using conda commands. For instance, run the commands below for installing the numpy package: activate py3env conda list numpy conda install numpy=1.15.1 Test python 3 code:

READ MORE

How can I manage the modules for python2 when python3 installed as well. In OSX

ref: https://stackoverflow.com/questions/53385448/how-can-i-manage-the-modules-for-python2-when-python3-installed-as-well-in-osx I’m using OSX and I installed python3 with anaconda installed. In my OSX, there exists two versions of python, i.e. python2 and python3. I managed the modules in anaconda which only affect modules in python3. But how can I manage(install, delete, update) the modules for python2? I’ve checked some posts about ‘python2 is at /usr/bin/python’ . So it’s ok to use python2 by ‘/usr/bin/python’ without configuring alias. But, how can I manage(install, delete, update) the modules for python2 when python3 installed as well.

READ MORE

Google Colab Notebook 的外部文件引用配置

Google Colab Notebook 的外部文件引用配置 Reference: How to upload the file and read Google Colab 先装工具:google-drive-ocamlfuse !apt-get install -y -qq software-properties-common python-software-properties module-init-tools !add-apt-repository -y ppa:alessandro-strada/ppa 2>&1 > /dev/null !apt-get update -qq 2>&1 > /dev/null !apt-get -y install -qq google-drive-ocamlfuse fuse from google.colab import auth auth.authenticate_user() from oauth2client.client import GoogleCredentials creds = GoogleCredentials.get_application_default() import getpass !google-drive-ocamlfuse -headless -id={creds.client_id} -secret={creds.client_secret} < /dev/null 2>&1 | grep URL vcode = getpass.getpass() !echo {vcode} | google-drive-ocamlfuse -headless -id={creds.client_id} -secret={creds.client_secret} 去网页确认一下: confirm the authentication of this access.

READ MORE

Zotero 容量满了,用云盘自动同步 - Extending Zotero's syncing folder size by DropBox

Zotero 容量满了,用云盘自动同步 就同步“storage" 那个文件夹就行了。 https://www.zotero.org/support/sync#alternative_syncing_solutions https://forums.zotero.org/discussion/29831/what-is-this-pipes-folder-in-my-zotero-folder What you should do, is In Zotero re-locate the data directory back out of the Google Drive folder (same instructions as you followed previously). Close Zotero/Firefox. Disable Google Drive syncing In Google Drive folder, move (not copy) the “storage” folder out of the Zotero data directory somewhere else inside the Google Drive folder. Move the Zotero data directory out of Google Drive folder to the folder you selected above in Zotero. Set up a symbolic link inside the Zotero data directory and point it to the new location of the storage folder.

READ MORE

用python + networkx探索和分析网络数据

Edited by Markdown Refered from: John Ladd, Jessica Otis, Christopher N. Warren, and Scott Weingart, “Exploring and Analyzing Network Data with Python,” The Programming Historian 6 (2017), https://programminghistorian.org/en/lessons/exploring-and-analyzing-network-data-with-python. 只是一个笔记,细节要看原文。 用python探索和分析网络数据 Table of Contents 目标和要求 [数据准备 和 NetworkX 安装](#数据准备 和 NetworkX 安装) 获取数据和基本的操作 进一步分析 [添加属性](#Add attributes) 网络度量分析 The Shape of the Network 最短路径 找出最大component 并生成子图 triadic closure Centrality 度数 特征值中心性 介数中心性 社区发现 导出数据和做结论 目标和要求 **Goals:**做完这里的步骤,你将得到如下技能: 使用Python的NetworkX 包来分析一个网络。 通过分析一个humanities network data来得到以下几点: 网络结构和path length 找到重要的或中心的节点 找到社区和subgroups 通过量化网络分析的方法,你将可以回答像这样的问题: What is the overall structure of the network?

READ MORE