当前位置:首页 > IT科技类资讯

5个小技巧,让你的for循环瞬间高大上!

本文转载自公众号“读芯术”(ID:AI_Discovery)

或许每个初学Python的技巧间高程序员最早接触的概念中都有For循环,这一点理所当然,环瞬 for循环可以在不费吹灰之力的大上情况下对数据执行很多操作。

然而,技巧间高大量的环瞬使用for循环也可能会让使用者的思维拘泥于简单的迭代中,而忽略了一些更加高效且简洁的大上迭代方法。

如何让你的技巧间高for循环告别繁复拥抱简洁,如何重启探索Python循环迭代的环瞬大门,希望以下几个小技巧能够给你启发。大上

Zip:同时在两个列表中循环

笔者在实践中发现代码可以同时在两个数组中进行循环。技巧间高要想在其他的环瞬编程语言中做到这一点相对来说难度大很多,这也体现出了Python的大上简易性。要达到同时在两个数组中进行循环这一目的技巧间高,只需使用zip()函数。环瞬

for first,大上second in zip(array1,array2):     print(first)     print(second) 

在一个偶整数序列和一个奇整数序列中使用这一方法就能体现出这一函数的功效。

odds = [1,3,5,7,9] evens = [2,4,6,8,10] for oddnum, evennum in zip(odds,evens):     print(oddnum)     print(evennum) 

以上函数输出的结果便是:

1 2 3 4 5 6 7 8 9 10 

In Range函数:编写C-Style循环

C-Style似乎看起来有点儿平凡,但它能在循环中焕发光彩。

for i in range(10):     print(i)     if i == 3:         i.update(7) 

C语言爱好者可能觉得以上的代码并不是云南idc服务商C-Style循环,但如果不想自己动手编写迭代函数,以上内容已经是最完美的形式了。

不过笔者热衷于“浪费时间”,因此决定编写一个新的迭代程序来写出尽可能完美的C-Style循环。

class forrange:     def __init__(self, startOrStop,stop=None, step=1):         if step == 0:             raise ValueError(forrangestep argument must not be zero)         if not isinstance(startOrStop,int):             raise TypeError(forrangestartOrStop argument must be an int)         if stop is not None and notisinstance(stop, int):             raise TypeError(forrangestop argument must be an int)         if stop is None:             self.start = 0             self.stop = startOrStop             self.step = step         else:             self.start = startOrStop             self.stop = stop             self.step = step     def __iter__(self):         returnself.foriterator(self.start, self.stop, self.step)     class foriterator:         def __init__(self, start, stop,step):             self.currentValue = None             self.nextValue = start             self.stop = stop             self.step = step         def __iter__(self): return self         def next(self):             if self.step > 0 andself.nextValue >= self.stop:                 raise StopIteration             if self.step < 0 andself.nextValue <= self.stop:                 raise StopIteration             self.currentValue =forrange.forvalue(self.nextValue, self)             self.nextValue += self.step             return self.currentValue     class forvalue(int):         def __new__(cls, value,iterator):             value =super(forrange.forvalue, cls).__new__(cls, value)             value.iterator = iterator             return value         def update(self, value):             if not isinstance(self, int):                 raiseTypeError(forvalue.update value must be an int)             if self ==self.iterator.currentValue:                 self.iterator.nextValue =value + self.iterator.step 

Filter()函数:只对需要的数据进行循环

在处理大量的数据时,使用filter函数能够使得数据在使用时效果更佳。Filter函数正如其名,其功效是在对数据进行迭代前进行过滤。当只需要使用某一范围内的数据而且不想再添加一个条件时,filter十分实用。

people = [{ "name": "John","id": 1}, { "name": "Mike", "id": 4},{ "name": "Sandra", "id": 2}, { "name":"Jennifer", "id": 3}]for person in filter(lambda i:i["id"] % 2 == 0, people): ...     print(person) ... { name: Mike, id: 4} { name: Sandra, id: 2} 

Enumerate()函数:对维度进行索引

在Python中使用枚举函数可以让Python将从数组中输出的列表索引进行编号。笔者制作了一个包含三个元素的列表对这一功能进行展示:

l = [5,10,15] 

现在可以利用以下方法来访问数组索引:

l[1] 10 l[0] 5 l[2] 15 

在这些列表中进行枚举时,维度的索引位置和维度会结合产生一个新的变量。请注意这一新变量的类型。

Python会自动将这些索引置入一个元组之中,这一点十分奇怪。笔者还是倾向于从只有一个元素的Python库中获得这些结果。站群服务器还好,我们可以把这些枚举函数置入到一个Python库中。

data = dict(enumerate(l)) 

输入以上代码之后就会得出:

>>> data { 0: 5, 1: 10, 2: 15} 

图源:unsplash

Sorted()函数:使用数据中进行排序,而非使用前

Sort函数对于常常需要处理大量数据的人来说至关重要,它将字符串根据首字母A到B进行排列,将整数和倍数自负无穷起由小至大排列。需要注意的是,这一函数无法用于带有字符串和整数或浮点数的列表。

l = [15,6,1,8] for i in sorted(l):     print(i) 1 6 8 15 

也可以将相反的参数设为False来进行逆运算。

for i in sorted(l,reverse = True):     print(i) 15 8 6 1 

对于可用的最后一个参数,可以使用key函数。Key是一个应用于已知循环中的每个维度的函数。而笔者偏向于使用lambda,Lambda会创造一个匿名但仍可调用的函数。

l.sort(key=lambda s: s[::-1]) 

写代码时,遇到大量的带有迭代的数据在所难免。简洁成就卓越,这些方法能够使代码简洁明了并且运行起来更快。循环的世界值得你继续探索!

分享到:

滇ICP备2023006006号-16