摘要:列表的樂趣標(biāo)簽空格分隔文章的原文是刪除元素所有等于值的元素重復(fù)警告該方法不負(fù)責(zé)列表項(xiàng)的順序。
列表的樂趣
標(biāo)簽(空格分隔): 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 重復(fù)
警告!
該方法不負(fù)責(zé)列表項(xiàng)的順序。
a = [1, 2, 3, 4, 3, 5, 1, 6] b = list(set(a)) print(b) [1, 2, 3, 4, 5, 6]1.3 第一個(gè)元素
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 最后一個(gè)元素
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 個(gè)元素
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 第一個(gè)元素
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 最后一個(gè)元素
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 個(gè)元素
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 根據(jù)字符串長(zhǎng)度
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 使函數(shù)對(duì)列表中的每個(gè)元素生效
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 兩個(gè)列表的笛卡爾積 (矢量)
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 個(gè)列表的笛卡爾積 (矢量)
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 檢查兩個(gè)列表是否有一個(gè)共同的元素
a = [1, 2, 0] b = [3, 0, 4] d = len((set(a) & set(b))) > 0 print(d) True4.7 檢查一個(gè)列表是否包含值 x
a = [1, 2.5, 7, 13221, 4.6545] if 7 in a: print("yes") else: print("no") yes4.8 計(jì)算 x 出現(xiàn)在一個(gè)列表中的次數(shù)
a = [1, 2.5, 7, 13221, 4.6545, 7] b = a.count(7) print(b) 24.9 兩個(gè)列表的不同
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 個(gè)列表的不同
4.11 一個(gè)列表中首先出現(xiàn)的 n 個(gè)元素
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 一個(gè)列表中首先出現(xiàn)的 n 個(gè)非 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 第一次出現(xiàn)之后的位置
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 第一次出現(xiàn)之前的位置
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 兩個(gè)列表的相同元素
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 個(gè)列表的相同元素
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 遍歷列表的索引/值對(duì)
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 遍歷一個(gè)列表的元素
a = [1, 2, 3, 4] for x in a: print x 1 2 3 44.21 列表中最大的數(shù)
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 一個(gè)列表的長(zhǎng)度
a = [1, 2, 3, 4] l = len(a) print(l) 44.24 合并兩個(gè)列表
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 列表中最小的數(shù)字
a = [1, 2.5, 7, 13221, 4.6545] b = min(a) print(b) 14.29 2 個(gè)列表的對(duì)稱差
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 個(gè)列表的對(duì)稱差
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 兩個(gè)列表的交集
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 個(gè)列表的交集
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]
文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請(qǐng)注明本文地址:http://specialneedsforspecialkids.com/yun/37453.html
摘要:當(dāng)我想聊聊的是對(duì)于編程系統(tǒng)編程系統(tǒng)產(chǎn)品編程其實(shí)其中也有很多好玩的地方與神奇的景象。最重要的一點(diǎn)是,有助于他人,我很開心編程系統(tǒng)產(chǎn)品可能得到用戶的認(rèn)可,且對(duì)其有所幫助。這就是編程,一個(gè)讓許多人痛苦掙扎卻又是一種樂趣和苦惱共存的創(chuàng)造性活動(dòng)。 程序員是一個(gè)在你即將30、40隨時(shí)可能面臨高壓禿頂?shù)墓ぷ鳎@是一個(gè)朋友在聽到我的新職業(yè)的第一反應(yīng)。我也習(xí)以為常的笑笑敷衍過去。 現(xiàn)在高壓已經(jīng)不是...
摘要:開發(fā)者的小樂趣今天發(fā)現(xiàn)了一個(gè)比較有趣的東西,在控制臺(tái)打印出屬于自己風(fēng)格的。。。 開發(fā)者的小樂趣 今天發(fā)現(xiàn)了一個(gè)比較有趣的東西,在控制臺(tái)打印出屬于自己風(fēng)格的。。。代碼 常規(guī)在控制臺(tái)調(diào)試代碼直接使用console.log() 現(xiàn)在我們重新定義一套打印風(fēng)格 使用console.log(%c 文字,style); 注意%后面有空格 //自定義控制臺(tái) function Console(){ ...
摘要:用了一段時(shí)間的和,忍無可忍之下幾乎要打算自己動(dòng)手做一款編輯器了。客戶端元,客戶端元目前元,無任何功能添加。簡(jiǎn)單易用,而功能又十分強(qiáng)大,除了很多精美的主題,豐富的開放接口,讓寫作和折騰的雙重快感可以同時(shí)擁有。 用了一段時(shí)間的 Ulysses 和 MWeb,忍無可忍之下幾乎要打算自己動(dòng)手做一款 Markdown 編輯器了。還好,又遇到了 MarkEditor。 成本分析 首先,從各位客官最...
摘要:關(guān)于可參照張?chǎng)涡翊蟠蟮闹v解設(shè)置變化速度然后將方法里面的賦值去掉加上方法如下心形樣式可變化心形顏色加上隨機(jī)顏色的函數(shù)下面的屬性隨位移是變化的可添加你需要變化的樣式比如透明度縮放最后加上事件圖不會(huì)搞撒 2018年開工,大家都很清閑,隨意瀏覽各個(gè)社區(qū),有些小發(fā)現(xiàn),希望跟大家分享下,語言組織太差請(qǐng)忽略:大致效果:鼠標(biāo)每次點(diǎn)擊頁面,鼠標(biāo)處便出現(xiàn)一個(gè)?,然后?慢慢上升至消失。還是直接上代碼吧。 1...
閱讀 1403·2021-10-11 10:59
閱讀 3104·2019-08-30 15:54
閱讀 2724·2019-08-30 13:19
閱讀 2456·2019-08-30 13:02
閱讀 2372·2019-08-30 10:57
閱讀 3347·2019-08-29 15:40
閱讀 981·2019-08-29 15:39
閱讀 2300·2019-08-29 12:40