9

体系课-数据可视化入门到精通-打造前端差异化竞争力(已完结)fafa

 3 years ago
source link: https://segmentfault.com/a/1190000041084047
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

体系课-数据可视化入门到精通-打造前端差异化竞争力(已完结)fafa

发布于 12 分钟前

download:体系课-数据可视化入门到精通-打造前端差异化竞争力(已完结)

结构字符串

你会经常需求打印字符串。要是有很多变量,防止下面这样:

name = "Raymond"
age = 22
born_in = "Oakland, CA"
string = "Hello my name is " + name + "and I'm " + str(age) + " years old. I was born in " + born_in + "."
print(string)
额,这看起来多乱呀?你能够用个漂亮简约的办法来替代, .format 。

name = "Raymond"
age = 22
born_in = "Oakland, CA"
string = "Hello my name is {0} and I'm {1} years old. I was born in {2}.".format(name, age, born_in)
print(string)
返回tuple元组

Python允许你在一个函数中返回多个元素,这让生活更简单。但是在解包元组的时分出出线这样的常见错误:

def binary(): return 0, 1
result = binary()
zero = result[0]
one = result[1]
这是没必要的,你完整能够换成这样:

def binary(): return 0, 1
zero, one = binary()
要是你需求一切的元素被返回,用个下划线 _ :

zero, _ = binary()
就是这么高效率!

访问Dict字典

你也会经常给 dicts 中写入 key,value (键,值)。

假如你试图访问一个不存在的于 dict 的 key ,可能会为了防止 KeyError 错误,你会倾向于这样做:

countr = {}
bag = [2, 3, 1, 2, 5, 6, 7, 9, 2, 7]
for i in bag:
if i in countr:
countr[i] += 1 else:
countr[i] = 1
for i in range(10):
if i in countr:
print("Count of {}: {}".format(i, countr[i]))
else:
print("Count of {}: {}".format(i, 0))
但是,用 get() 是个更好的方法。

countr = {}
bag = [2, 3, 1, 2, 5, 6, 7, 9, 2, 7]
for i in bag:
countr[i] = countr.get(i, 0) + 1
for i in range(10):
print("Count of {}: {}".format(i, countr.get(i, 0)))
当然你也能够用 setdefault 来替代。

这还用一个更简单却多费点开支的方法:

bag = [2, 3, 1, 2, 5, 6, 7, 9, 2, 7]

{2: 3, 3: 1, 1: 1, 5: 1, 6: 1, 7: 2, 9: 1}

countr = dict([(num, bag.count(num)) for num in bag])
for i in range(10):
print("Count of {}: {}".format(i, countr.get(i, 0)))
你也能够用 dict 推导式。

countr = {num: bag.count(num) for num in bag}
这两种办法开支大是由于它们在每次 count 被调用时都会对列表遍历。

现有的库只需导入你就能够做你真正想做的了。

还是说前面的例子,我们建一个函数来数一个数字在列表中呈现的次数。那么,曾经有一个库就能够做这样的事情。

from collections import Counter
bag = [2, 3, 1, 2, 5, 6, 7, 9, 2, 7]
countr = Counter(bag)for i in range(10):
print("Count of {}: {}".format(i, countr[i]))
一些用库的理由:

代码是正确而且经过测试的。
它们的算法可能会是最优的,这样就跑的更快。
笼统化:它们指向明白而且文档友好,你能够专注于那些还没有被完成的。
最后,它都曾经在那儿了,你不用再造轮子了。
在列表中切片/步进
你能够指定 start 的点和 stop 点,就像这样 list[start:stop:step] 。我们取出列表中的前5个元素:

bag = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
for elem in bag[:5]:
print(elem)
这就是切片,我们指定 stop 点是5,再中止前就会从列表中取出5个元素。

要是最后5个元素怎样做?

bag = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
for elem in bag[-5:]:
print(elem)
没看明白吗? -5 意味着从列表的结尾取出5个元素。

假如你想对列表中元素距离操作,你可能会这样做:

bag = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
for index, elem in enumerate(bag):
if index % 2 == 0:
print(elem)
但是你应该这样来做:

bag = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
for elem in bag[::2]:
print(elem)

或者用 ranges

bag = list(range(0,10,2))
print(bag)
这就是列表中的步进。 list[::2] 意义是遍历列表同时两步取出一个元素。

你能够用 list[::-1] 很酷的翻转列表。


Recommend

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK