摘要:列表的乐趣标签空格分隔文章的原文是删除元素所有等于值的元素重复警告该方法不负责列表项的顺序。
列表的乐趣
标签(空格分隔): Python list
1 删除元素 1.1 所有等于 X 值的元素文章的原文是 Fun with Lists
x = 4 a = [1, 2, 3, 4, 4, 5, 6, 1, 4] for i in range(a.count(x)): a.pop(a.index(x)) print(a) [1, 2, 3, 5, 6, 1]
a = [1, 2, 3, 4, 4, 5, 6, 1, 4] b = [v for v in a if v != x] print(b) [1, 2, 3, 5, 6, 1] print(a) [1, 2, 3, 4, 4, 5, 6, 1, 4]1.2 重复
警告!
该方法不负责列表项的顺序。
a = [1, 2, 3, 4, 3, 5, 1, 6] b = list(set(a)) print(b) [1, 2, 3, 4, 5, 6]1.3 第一个元素
a = [1, 2, 3, 4, 5, 6] b = a.pop(0) print(b) 1 print(a) [2, 3, 4, 5, 6]
a = [1, 2, 3, 4, 5, 6] del a[0] print(a) [2, 3, 4, 5, 6]
a = [1, 2, 3, 4, 5, 6] d = a[1:] print(d) [2, 3, 4, 5, 6] print(a) [1, 2, 3, 4, 5, 6]1.4 最后一个元素
a = [1, 2, 3, 4, 5, 6] b = a.pop() print(b) 6 print(a) [1, 2, 3, 4, 5]
a = [1, 2, 3, 4, 5, 6] del a[-1] print(a) [1, 2, 3, 4, 5]
a = [1, 2, 3, 4, 5, 6] c = a[:-1] print(c) [1, 2, 3, 4, 5] print(a) [1, 2, 3, 4, 5, 6]1.5 第 n 个元素
n = 3 a = [1, 2, 3, 4, 5, 6] b = a.pop(n - 1) print(b) 3 print(a) [1, 2, 4, 5, 6]
a = [1, 2, 3, 4, 5, 6] del a[n - 1] print(a) [1, 2, 4, 5, 6]
a = [1, 2, 3, 4, 5, 6] c = a[:n - 1] + a[n:] print(c) [1, 2, 4, 5, 6] print(a) [1, 2, 3, 4, 5, 6]
a = [1, 2, 3, 4, 5, 6] c = [v for i, v in enumerate(a) if i != n - 1] print(c) [1, 2, 4, 5, 6] print(a) [1, 2, 3, 4, 5, 6]2 替换元素 2.1 所有等于 x 值的元素
x = 4 a = [1, 2, 3, 4, 4, 5, 6, 1, 4] for i in range(a.count(x)): a[a.index(x)] = 0 print(a) [1, 2, 3, 0, 0, 5, 6, 1, 0]
a = [1, 2, 3, 4, 4, 5, 6, 1, 4] b = [v if v != x else 0 for v in a ] print(b) [1, 2, 3, 0, 0, 5, 6, 1, 0] print(a) [1, 2, 3, 4, 4, 5, 6, 1, 4]2.2 第一个元素
a = [1, 2, 3, 4] a[0] = 0 print(a) [0, 2, 3, 4]
a = [1, 2, 3, 4] b = [0] + a[1:] print(b) [0, 2, 3, 4] print(a) [1, 2, 3, 4]2.3 最后一个元素
a = [1, 2, 3, 4] a[-1] = 0 print(a) [1, 2, 3, 0]
a = [1, 2, 3, 4] b = a[:-1] + [0] print(b) [1, 2, 3, 0] print(a) [1, 2, 3, 4]2.4 第 n 个元素
n = 3 a = [1, 2, 3, 4] a[n - 1] = 0 print(a) [1, 2, 0, 4]
a = [1, 2, 3, 4] b = [v if i != n -1 else 0 for i, v in enumerate(a)] print(b) [1, 2, 0, 4] print(a) [1, 2, 3, 4]3 排序 3.1 按照字母顺序排列地 (不分大小写)
a = ["d", "C", "B", "a"] b = sorted(a, key=lambda x: x.lower()) print(b) ["a", "B", "C", "d"] print(a) ["d", "C", "B", "a"] a.sort(key=lambda s: s.lower()) print(a) ["a", "B", "C", "d"]3.2 按照字母顺序排列地 (分大小写)
a = ["d", "C", "B", "a"] b = sorted(a) print(b) ["B", "C", "a", "d"] print(a) ["d", "C", "B", "a"] a.sort() print(a) ["B", "C", "a", "d"]3.3 升序
a = ["a", "c", "d", "b"] b = sorted(a, reverse=True) print(b) ["d", "c", "b", "a"] print(a) ["a", "c", "d", "b"] a.sort(reverse=True) print(a) ["d", "c", "b", "a"]3.4 根据字符串长度
4 其他 4.1 列表中的所有值相加
a = ["aaaa", "B", "CCC", "dd"] b = sorted(a, key=lambda x: len(x)) print(b) ["B", "dd", "CCC", "aaaa"] print(a) ["aaaa", "B", "CCC", "dd"] a.sort(key=lambda x: len(x)) print(a) ["B", "dd", "CCC", "aaaa"]
a = [1, 2.5, 7, 13221, 4.6545] b = sum(a) print(b) 13236.15454.2 在列表中新增元素
a = [1, 2, 3] a.append(4) print(a) [1, 2, 3, 4]4.3 使函数对列表中的每个元素生效
def func(x): print(x) def func_2(x): return 2*x a = [1, 2, 3, 4] map(func, a) 1 2 3 4 b = map(func_2, a) print(b) [2, 4, 6, 8] c = [func_2(x) for x in a] print(c) [2, 4, 6, 8]4.4 两个列表的笛卡尔积 (矢量)
a = [1, 2, 3] b = [4, 5, 6] c = [(x, y) for x in a for y in b] print(c) [(1, 4), (1, 5), (1, 6), (2, 4), (2, 5), (2, 6), (3, 4), (3, 5), (3, 6)]
import itertools d = [p for p in itertools.product(a, b)] print(d) [(1, 4), (1, 5), (1, 6), (2, 4), (2, 5), (2, 6), (3, 4), (3, 5), (3, 6)]4.5 n 个列表的笛卡尔积 (矢量)
from pprint import pprint import itertools a = [[0, 1], [2, 3], [4, 5]] b = [p for p in itertools.product(*a)] print(b) [(0, 2, 4), (0, 2, 5), (0, 3, 4), (0, 3, 5), (1, 2, 4), (1, 2, 5), (1, 3, 4), (1, 3, 5)]4.6 检查两个列表是否有一个共同的元素
a = [1, 2, 0] b = [3, 0, 4] d = len((set(a) & set(b))) > 0 print(d) True4.7 检查一个列表是否包含值 x
a = [1, 2.5, 7, 13221, 4.6545] if 7 in a: print("yes") else: print("no") yes4.8 计算 x 出现在一个列表中的次数
a = [1, 2.5, 7, 13221, 4.6545, 7] b = a.count(7) print(b) 24.9 两个列表的不同
a = [1, 2, 3, 4] b = [3, 4, 5, 6] c = [x for x in a if not x in b] print(c) [1, 2]
d = list(set(a) - set(b)) print(d) [1, 2]
e = list(set.difference(set(a), set(b))) print(e) [1, 2]4.10 n 个列表的不同
4.11 一个列表中首先出现的 n 个元素
a = [1, 2, 3, 4] b = [[3, 5, 6, 7], [1, 8, 9, 10]] a - b[0] - b[1]: c = [x for x in a if not any([x in l for l in b])] print(c) [2, 4] d = list(set(a).difference(*[set(l) for l in b])) print(d) [2, 4]
n = 2 a = [1, 2, 3, 4, 5, 6] b = a[:2] print(b) [1, 2]4.12 一个列表中首先出现的 n 个非 x 值的元素
n = 3 x = 2 a = [1, 2, 2, 3, 2, 2, 2, 4, 5, 6] gen = (v for v in a if v != x) b = [gen.next() for i in range(n)] print(b) [1, 3, 4] c = [] for v in a: if v != x: c.append(v) if len(c) == n: break print(c) [1, 3, 4] 4.13 Flatten a list of lists a = [[1, 2, 3], [4, 5, 6], [7, 8, 9]] b = [i for s in a for i in s] print(b) [1, 2, 3, 4, 5, 6, 7, 8, 9]4.14 把 x 插入 y 第一次出现之后的位置
x = 4 y = 3 a = [1, 2, 3, 5, 3, 6, 2] try: a.insert(a.index(y) + 1, x) except ValueError: a.append(x) print(a) [1, 2, 3, 4, 5, 3, 6, 2] a = [1, 2, 3, 5, 3, 6, 2] try: i = a.index(y) b = a[:i + 1] + [x] + a[i + 1:] except ValueError: b = a + [x] print(b) [1, 2, 3, 4, 5, 3, 6, 2] print(a) [1, 2, 3, 5, 3, 6, 2]4.15 把 x 插入 y 第一次出现之前的位置
x = 3 y = 4 a = [1, 2, 4, 5, 4, 6, 2] try: a.insert(a.index(y), x) except ValueError: a.append(x) print(a) [1, 2, 3, 4, 5, 4, 6, 2] a = [1, 2, 4, 5, 4, 6, 2] try: i = a.index(y) b = a[:i] + [x] + a[i:] except ValueError: b = a + [x] print(b) [1, 2, 3, 4, 5, 4, 6, 2] print(a) [1, 2, 4, 5, 4, 6, 2]4.16 两个列表的相同元素
a = [1, 2, 3, 4] b = [3, 4, 5, 6] c = [x for x in a if x in b] print(c) [3, 4] d = list(set(a) & set(b)) print(d) [3, 4] e = list(set(a).intersection(b)) print(e) [3, 4]4.17 n 个列表的相同元素
a = [[1, 2, 3, 4], [3, 4, 5, 6], [1, 3, 7, 8]] b = list(set.intersection(*[set(l) for l in a])) print(b) [3] c = [x for x in a[0] if all([x in l for l in a[1:]])] print(c) [3]4.18 Iterate over every other element of a list
a = [1, 2, 3, 4, 5, 6] for x in a[::2]: print x 1 3 54.19 遍历列表的索引/值对
a = [1, 2.5, 7, 13221, 4.6545] for i, v in enumerate(a): print("%i: %i" % (i, v)) 0: 1 1: 2 2: 7 3: 13221 4: 44.20 遍历一个列表的元素
a = [1, 2, 3, 4] for x in a: print x 1 2 3 44.21 列表中最大的数
a = [1, 2.5, 7, 13221, 4.6545] b = max(a) print(b) 13221 4.22 Last n elements of a list n = 2 a = [1, 2, 3, 4, 5, 6] b = a[-2:] print(b) [5, 6]4.23 一个列表的长度
a = [1, 2, 3, 4] l = len(a) print(l) 44.24 合并两个列表
a = [1, 2, 3] b = [4, 5, 6] c = a + b print(c) [1, 2, 3, 4, 5, 6] print(a) [1, 2, 3] print(b) [4, 5, 6] a.extend(b) print(a) [1, 2, 3, 4, 5, 6] print(b) [4, 5, 6]4.25 列表的组合
from pprint import pprint from itertools import permutations as perm a = ["a", "b", "c"] b = list(perm(a)) pprint(b) [("a", "b", "c"), ("a", "c", "b"), ("b", "a", "c"), ("b", "c", "a"), ("c", "a", "b"), ("c", "b", "a")] c = ["".join(p) for p in perm(a)] print(c) ["abc", "acb", "bac", "bca", "cab", "cba"] d = ["".join(p) for p in perm(a, 2)] print(d) ["ab", "ac", "ba", "bc", "ca", "cb"]4.26 Prepend an element to a list
a = [2, 3, 4] a.insert(0, 1) print(a) [1, 2, 3, 4] a = [2, 3, 4] b = [1] + a print(b) [1, 2, 3, 4] print(a) [2, 3, 4]4.27 列表反序
a = [1, 2, 3, 4] a.reverse() print(a) [4, 3, 2, 1] a = [1, 2, 3, 4] c = a[::-1] print(c) [4, 3, 2, 1] print(a) [1, 2, 3, 4]4.28 列表中最小的数字
a = [1, 2.5, 7, 13221, 4.6545] b = min(a) print(b) 14.29 2 个列表的对称差
a = [1, 2, 3, 4] b = [3, 4, 5, 6] c = [x for x in a if x not in b] + [x for x in b if x not in a] print(c) [1, 2, 5, 6] d = list(set(a) ^ set(b)) print(d) [1, 2, 5, 6] e = list(set(a).symmetric_difference(b)) print(e) [1, 2, 5, 6]4.30 n 个列表的对称差
a = [[1, 2, 3, 4], [3, 4, 5, 6], [1, 5, 7, 8]] b = a[0] for i in range(len(a) - 1): b = list(set(b) ^ set(a[i + 1])) print(b) [2, 6, 7, 8] tmp = {} for v in [i for s in a for i in s]: tmp[v] = (v not in tmp) c = [k for k, v in tmp.iteritems() if v] print(c) [2, 6, 7, 8]4.31 两个列表的交集
a = [1, 2, 3, 4] b = [3, 4, 5, 6] c = [x for x in set(a + b)] print(c) [1, 2, 3, 4, 5, 6] d = list(set(a) | set(b)) print(d) [1, 2, 3, 4, 5, 6] e = list(set(a).union(b)) print(e) [1, 2, 3, 4, 5, 6]4.32 n 个列表的交集
a = [[1, 2, 3, 4], [3, 4, 5, 6], [1, 6, 7, 8]] b = list(set.union(*[set(l) for l in a])) print(b) [1, 2, 3, 4, 5, 6, 7, 8] import itertools c = [x for x in set(itertools.chain(*a))] print(c) [1, 2, 3, 4, 5, 6, 7, 8]4.33 列表的左边加 0
max_length = 10 a = [1, 2, 3, 4] b = [0]*(max_length - len(a)) + a print(b) [0, 0, 0, 0, 0, 0, 1, 2, 3, 4]4.34 列表的右边加 0
max_length = 10 a = [1, 2, 3, 4] b = a + [0]*(max_length - len(a)) print(b) [1, 2, 3, 4, 0, 0, 0, 0, 0, 0]
文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。
转载请注明本文地址:https://www.ucloud.cn/yun/37453.html
摘要:当我想聊聊的是对于编程系统编程系统产品编程其实其中也有很多好玩的地方与神奇的景象。最重要的一点是,有助于他人,我很开心编程系统产品可能得到用户的认可,且对其有所帮助。这就是编程,一个让许多人痛苦挣扎却又是一种乐趣和苦恼共存的创造性活动。 程序员是一个在你即将30、40随时可能面临高压秃顶的工作,这是一个朋友在听到我的新职业的第一反应。我也习以为常的笑笑敷衍过去。 现在高压已经不是...
摘要:用了一段时间的和,忍无可忍之下几乎要打算自己动手做一款编辑器了。客户端元,客户端元目前元,无任何功能添加。简单易用,而功能又十分强大,除了很多精美的主题,丰富的开放接口,让写作和折腾的双重快感可以同时拥有。 用了一段时间的 Ulysses 和 MWeb,忍无可忍之下几乎要打算自己动手做一款 Markdown 编辑器了。还好,又遇到了 MarkEditor。 成本分析 首先,从各位客官最...
摘要:关于可参照张鑫旭大大的讲解设置变化速度然后将方法里面的赋值去掉加上方法如下心形样式可变化心形颜色加上随机颜色的函数下面的属性随位移是变化的可添加你需要变化的样式比如透明度缩放最后加上事件图不会搞撒 2018年开工,大家都很清闲,随意浏览各个社区,有些小发现,希望跟大家分享下,语言组织太差请忽略:大致效果:鼠标每次点击页面,鼠标处便出现一个♥,然后♥慢慢上升至消失。还是直接上代码吧。 1...
阅读 1385·2021-10-11 10:59
阅读 3066·2019-08-30 15:54
阅读 2688·2019-08-30 13:19
阅读 2409·2019-08-30 13:02
阅读 2333·2019-08-30 10:57
阅读 3293·2019-08-29 15:40
阅读 900·2019-08-29 15:39
阅读 2223·2019-08-29 12:40