目录:

1. 嵌套列表对应位置元素相加 (add the corresponding elements of nested list)

2. 多个列表对应位置相加(add the corresponding elements of several lists)

3. 列表中嵌套元组对应位置相加 (python sum corresponding position in list neseted tuple)

4. 判断列表中所有元素是否都是0 (python check if all element in list is zero)

5. 寻找列表中所有最大值的位置 (python find all position of maximum value in list)

6. 计算列表中出现次数最多的所有项 (python get all value with the highest occurrence in list)

7. 生成等间隔列表 (python create list in same space)

8. 寻找嵌套列表的最大值 (python find max value in nested list)

9. 找到列表中的众数 (python find mode in list)

10. 列表按照某个规则排序 (python sort a list according to an regulation)

11. 列表寻找大于0位置的开始索引和结束索引 (python find the starting and ending indices of values greater than 0 in a list)

12. 列表里面元素所有是否满足某一个条件 (python check if all element of a list matches a condition)

13. 对两个列表一起进行排序 (python sort two list in same order)

14.  把嵌套的列表平铺成一个列表 (python convert nested list to a flat list)

内容:

1. 嵌套列表对应位置元素相加 (add the corresponding elements of nested list)

https://stackoverflow.com/questions/11280536/how-can-i-add-the-corresponding-elements-of-several-lists-of-numbers

方法1:

>>> lis=[[1,2,3,4,5],[2,3,4,5,6],[3,4,5,6,7]]
>>> [sum(x) for x in zip(*lis)]
[6, 9, 12, 15, 18] 

方法2:

>>> seq = np.array([
... [1,2,3,4,5],
... [2,3,4,5,6],
... [3,4,5,6,7]])
>>> np.sum(seq,axis=0)
array([ 6, 9, 12, 15, 18])

2. 多个列表对应位置相加(add the corresponding elements of several lists)  

https://stackoverflow.com/questions/11280536/how-can-i-add-the-corresponding-elements-of-several-lists-of-numbers

方法1:

a = [1,2,3,4,5]
b = [2,3,4,5,6]
c = [3,4,5,6,7]
[sum(n) for n in zip(*[a, b, c])]

方法2:

>>> seq = np.array([
... [1,2,3,4,5],
... [2,3,4,5,6],
... [3,4,5,6,7]])
>>> np.sum(seq,axis=0)
array([ 6, 9, 12, 15, 18])

3. 列表中嵌套元组对应位置相加 (python sum corresponding position in list neseted tuple)

https://stackoverflow.com/questions/14180866/sum-each-value-in-a-list-of-tuples

方法1:

l = [(1, 2), (3, 4), (5, 6), (7, 8), (9, 0)]
[sum(x) for x in zip(*l)]
[25, 20]

方法2:

map(sum, zip(*l))
[25, 20]

4. 判断列表中所有元素是否都是0 (python check if all element in list is zero)

https://stackoverflow.com/questions/10666163/how-to-check-if-all-elements-of-a-list-matches-a-condition

In [62]: b=[0, 0, 0, 0, 0, 0, 0, 0, 0]
In [63]: all(i==0 for i in b)
Out[63]: True

5. 寻找列表中所有最大值的位置 (python find all position of maximum value in list)

https://stackoverflow.com/questions/3989016/how-to-find-all-positions-of-the-maximum-value-in-a-list

方法1:

a = [32, 37, 28, 30, 37, 25, 27, 24, 35, 55, 23, 31, 55, 21, 40, 18, 50, 35, 41, 49, 37, 19, 40, 41, 31]
>>> m = max(a)
>>> [i for i, j in enumerate(a) if j == m]
[9, 12]

方法2:

m = max(a)
[i for i in range(len(a)) if a[i] == m]

6. 计算列表中出现次数最多的所有项 (python get all value with the highest occurrence in list)

https://stackoverflow.com/questions/36516279/how-to-return-elements-with-the-highest-occurrence-in-list

In [10]: from collections import Counter
In [11]: a=[2, 3, 5, 1, 6, 1, 5]
In [12]: ct = Counter(a)
In [13]: ct
Out[13]: Counter({1: 2, 2: 1, 3: 1, 5: 2, 6: 1})
In [14]: max_value = max(ct.values())
In [15]: max_value
Out[15]: 2
In [16]: sorted(key for key, value in ct.items() if value == max_value)
Out[16]: [1, 5]

7. 生成等间隔列表 (python create list in same space)

https://stackoverflow.com/questions/6683690/making-a-list-of-evenly-spaced-numbers-in-a-certain-range-in-python

方法1:

In [3]: [3+x*5 for x in range(10)]
Out[3]: [3, 8, 13, 18, 23, 28, 33, 38, 43, 48]

方法2:

In [46]: import numpy as np
In [47]: np.linspace(0,5,10)
Out[47]:
array([ 0. , 0.55555556, 1.11111111, 1.66666667, 2.22222222,
2.77777778, 3.33333333, 3.88888889, 4.44444444, 5. ])

方法3:

>>> a = np.arange(0,5, 0.5) # returns a numpy array
>>> a
array([ 0. , 0.5, 1. , 1.5, 2. , 2.5, 3. , 3.5, 4. , 4.5]

8. 寻找嵌套列表的最大值 (python find max value in nested list)

https://stackoverflow.com/questions/33286642/finding-the-largest-number-in-a-nested-list-in-python

In [4]: lists = [[21, 34, 345, 2], [555, 22, 6, 7], [94, 777, 65, 1], [23, 54, 12, 666]]

In [5]: list(map(max, lists))
Out[5]: [345, 555, 777, 666] In [6]: max(map(max, lists))
Out[6]: 777

9. 找到列表中的众数 (python find mode in list)

https://stackoverflow.com/questions/10797819/finding-the-mode-of-a-list

In [1]: from statistics import mode

In [2]: list = [2,3,4,5,64,3,2,3,3,3]

In [3]: mode(list)
Out[3]: 3

10. 列表按照某个规则排序 (python sort a list according to an regulation)

https://www.geeksforgeeks.org/python-sort-list-according-second-element-sublist/

注意:区分sort和sorted,是否是原地修改列表(in place)

In [58]: a=[('rishav', 10), ('akash', 5), ('ram', 20), ('gaurav', 15)]

In [59]: b=sorted(a, key = lambda x: x[1], reverse=True)

In [60]: a
Out[60]: [('rishav', 10), ('akash', 5), ('ram', 20), ('gaurav', 15)] In [61]: b
Out[61]: [('ram', 20), ('gaurav', 15), ('rishav', 10), ('akash', 5)] In [62]: a.sort(key = lambda x: x[1], reverse=True) In [63]: a
Out[63]: [('ram', 20), ('gaurav', 15), ('rishav', 10), ('akash', 5)]

11. 列表寻找大于0位置的开始索引和结束索引(python find the starting and ending indices of values greater than 0 in a list)

https://stackoverflow.com/questions/48076780/find-the-starting-and-ending-indices-of-values-greater-than-0-in-a-list

cross = [0, 7, 5, 8, 0, 0, 0, 7, 5, 0]
def first_and_last_index(cross):
result = []
foundstart = False
foundend = False
startindex = 0
endindex = 0
for i in range(0, len(cross)):
if cross[i] != 0:
if not foundstart:
foundstart = True
startindex = i
else:
if foundstart:
foundend = True
endindex = i - 1 if foundend:
result.append((startin[(1, 3), (7, 8)]dex, endindex))
foundstart = False
foundend = False
startindex = 0
endindex = 0 if foundstart:
result.append((startindex, len(cross)-1))
return result result = first_and_last_index(cross)
print(result)
[(1, 3), (7, 8)]
[(1, 3), (7, 8)]

12. 列表里面元素所有是否满足某一个条件 (python check if all element of a list matches a condition)

https://stackoverflow.com/questions/10666163/how-to-check-if-all-elements-of-a-list-matches-a-condition

>>> items = [[1, 2, 0], [1, 2, 0], [1, 2, 0]]
>>> all(item[2] == 0 for item in items)
True
>>> items = [[1, 2, 0], [1, 2, 1], [1, 2, 0]]
>>> all(item[2] == 0 for item in items)
False
如果想判断至少存在一个这样的元素,则使用下面语句
>>> any(item[2] == 0 for item in items)
True

13. 对两个列表一起进行排序 (python sort two list in same order)

https://stackoverflow.com/questions/9764298/is-it-possible-to-sort-two-listswhich-reference-each-other-in-the-exact-same-w

In [197]: a
Out[197]: [(38, 750, 574, 788), (39, 301, 575, 559), (39, 182, 254, 281)] In [198]: b
Out[198]: [(291, 778), (306, 429), (151, 230)] In [199]: c, d =zip(*sorted(zip(a, b))) In [200]: c
Out[200]: ((38, 750, 574, 788), (39, 182, 254, 281), (39, 301, 575, 559)) In [201]: d
Out[201]: ((291, 778), (151, 230), (306, 429))

14.  把嵌套的列表平铺成一个列表 (python convert nested list to a flat list)

https://djangocentral.com/nested-list-to-flat-list/

In [223]: a=[[(86, 68, 414, 99), (309, 132, 435, 144), (94, 185, 410, 230)], [], [], [(1, 2, 3, 4)], []]
// 方法1
In [224]: import functools
...: import operator
...: In [225]: functools.reduce(operator.concat, a)
Out[225]: [(86, 68, 414, 99), (309, 132, 435, 144), (94, 185, 410, 230), (1, 2, 3, 4)]
// 方法2
In [226]: from functools import reduce In [227]:  reduce(lambda x, y: x+y, a)
Out[227]: [(86, 68, 414, 99), (309, 132, 435, 144), (94, 185, 410, 230), (1, 2, 3, 4)]
// 方法3
In [228]: from itertools import chain In [229]: list(chain.from_iterable(a))
Out[229]: [(86, 68, 414, 99), (309, 132, 435, 144), (94, 185, 410, 230), (1, 2, 3, 4)]

15:

python 常用技巧 — 列表(list)的更多相关文章

  1. python 常用技巧 — 字典 (dictionary)

    目录: 1. python 相加字典所有的键值 (python sum all values in dictionary) 2. python 两个列表分别组成字典的键和值 (python two l ...

  2. python常用技巧

    1,关于tab键与4个空格: 由于不同平台间,tab键值设置有所区别,据相关介绍,官方在缩进方面推荐使用4个空格.方便起见,可设置tab自动转换为4个空格. 1.1在pycharm中:    通过fi ...

  3. python 常用技巧

    一.字符串与数值的转换 Python中字符串转换为数值: str_num = '99' num = int(str_num) 整型数转换为字符串: num = 99 str_num = str(num ...

  4. python开发技巧---列表、字典、集合值的过滤

    主要学习列表,字典,集合表达式的应用: 列表的解析式: 生成一个随机列表: In [4]: datalist = [randint(-10,10) for _ in range(10)] In [5] ...

  5. python常用技巧 — 杂

    目录: 1. 找到字符串中的所有数字(python find digits in string) 2. python 生成连续的浮点数(如 0.1, 0.2, 0.3, 0.4, ... , 0.9) ...

  6. python 常用技巧 — 数组 (array)

    目录: 1. 数组每一行除以这一行的总数(numpy divide row by row sum) 2. 数组每一行或者每一列求平均 (python average array columns or ...

  7. #1 Python灵活技巧

    前言 Python基础系列博文已顺利结束,从这一篇开始将进入探索更加高级的Python用法,Python进阶系列文章将包含面向对象.网络编程.GUI编程.线程和进程.连接数据库等.不过在进阶之前,先来 ...

  8. Python SQLAlchemy基本操作和常用技巧包含大量实例,非常好python

    http://www.makaidong.com/%E8%84%9A%E6%9C%AC%E4%B9%8B%E5%AE%B6/28053.shtml "Python SQLAlchemy基本操 ...

  9. python算法常用技巧与内置库

    python算法常用技巧与内置库 近些年随着python的越来越火,python也渐渐成为了很多程序员的喜爱.许多程序员已经开始使用python作为第一语言来刷题. 最近我在用python刷题的时候想 ...

随机推荐

  1. 【挖坟】HDU3205 Factorization

    分圆多项式 问题在于精度貌似出了一些奇怪的问题... [输出也写的有问题QAQ] 完全不会处理了 加上全网没有题解T^T 挖个坑以后补.. #include<cstdio> #includ ...

  2. 05.配置为开发模式、配置静态资源locations、自定义消息转化器、FastJson

    配置为开发模式,代码做了修改,不用重新运行 idea需要该配置,mac测试无效 <dependency> <groupId>org.springframework</gr ...

  3. 26.LockSupport线程阻塞工具

    import java.util.concurrent.locks.LockSupport; /** * 线程阻塞工具类:LockSupport * 可以在线程内任意位置让线程阻塞 */ public ...

  4. 禁止打开 F12 开发者工具

    禁止F12 window.onkeydown = window.onkeyup = window.onkeypress = function (event) { // 判断是否按下F12,F12键码为 ...

  5. Apache Flink 为什么能够成为新一代大数据计算引擎?

    众所周知,Apache Flink(以下简称 Flink)最早诞生于欧洲,2014 年由其创始团队捐赠给 Apache 基金会.如同其他诞生之初的项目,它新鲜,它开源,它适应了快速转的世界中更重视的速 ...

  6. HTML基础知识笔记摘要

    HTML互联网三大基石:1.HTML:显示数据2.HTTP:传输数据 http传输协议3.URL:定位数据协议://ip地址或主机名:端口/网络中的内容... HTML(hyper text make ...

  7. 09-排序2 Insert or Merge(25 分)

    According to Wikipedia: Insertion sort iterates, consuming one input element each repetition, and gr ...

  8. BZOJ 2806: [Ctsc2012]Cheat(单调队列优化dp+后缀自动机)

    传送门 解题思路 肯定先要建出来广义后缀自动机.刚开始以为是个二分+贪心,写了一下结果\(20\)分.说一下正解,首先显然\(L_0\)具有单调性,是可以二分的.考虑二分后怎样判合法,对于分割序列很容 ...

  9. Android setXfermode 模式

    参考:http://onewayonelife.iteye.com/blog/1169176  setXfermode  设置两张图片相交时的模式  我们知道 在正常的情况下,在已有的图像上绘图将会在 ...

  10. phpredis报错信息:protocol error, got 'o' as reply type byte解决方案

    今天在前端调用PHP的接口时,有报错信息为:protocol error, got 'o' as reply type byte另外此错误有几率会重现,并不是必现的.十分疑惑,遂百度一下,发现是red ...