资讯专栏INFORMATION COLUMN

列表的乐趣

littleGrow / 516人阅读

摘要:列表的乐趣标签空格分隔文章的原文是删除元素所有等于值的元素重复警告该方法不负责列表项的顺序。

列表的乐趣

标签(空格分隔): Python list


  

文章的原文是 Fun with Lists

1 删除元素 1.1 所有等于 X 值的元素
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 根据字符串长度

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"]
4 其他 4.1 列表中的所有值相加
a = [1, 2.5, 7, 13221, 4.6545]
b = sum(a)
print(b)
13236.1545
4.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)
True
4.7 检查一个列表是否包含值 x
a = [1, 2.5, 7, 13221, 4.6545]
if 7 in a:
    print("yes")
else:
    print("no")
yes
4.8 计算 x 出现在一个列表中的次数
a = [1, 2.5, 7, 13221, 4.6545, 7]
b = a.count(7)
print(b)
2
4.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 个列表的不同

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]
4.11 一个列表中首先出现的 n 个元素
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
5
4.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: 4
4.20 遍历一个列表的元素
a = [1, 2, 3, 4]
for x in a:
    print x
1
2
3
4
4.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)
4
4.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)
1
4.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

相关文章

  • 左滑右滑乐趣

    摘要:左滑右滑你不再是一个人无论你是一个程序猿还是一个程序媛,每天干的事除了还是,代码不能解决的问题什么问题自己心里还没点数嘛,探探能帮你解决。 左滑 右滑 你不再是一个人 无论你是一个程序猿还是一个程序媛,每天干的事除了coding还是coding,代码不能解决的问题(什么问题自己心里还没点abcd数嘛),探探能帮你解决。最近网上特流行一款交友软件叫探探(据说是yp软件)。作为探探曾经的一...

    Muninn 评论0 收藏0
  • [Java猫说]谈谈编程人生乐趣与苦恼

    摘要:当我想聊聊的是对于编程系统编程系统产品编程其实其中也有很多好玩的地方与神奇的景象。最重要的一点是,有助于他人,我很开心编程系统产品可能得到用户的认可,且对其有所帮助。这就是编程,一个让许多人痛苦挣扎却又是一种乐趣和苦恼共存的创造性活动。 程序员是一个在你即将30、40随时可能面临高压秃顶的工作,这是一个朋友在听到我的新职业的第一反应。我也习以为常的笑笑敷衍过去。 现在高压已经不是...

    walterrwu 评论0 收藏0
  • 前端开发小乐趣

    摘要:开发者的小乐趣今天发现了一个比较有趣的东西,在控制台打印出属于自己风格的。。。 开发者的小乐趣 今天发现了一个比较有趣的东西,在控制台打印出属于自己风格的。。。代码 常规在控制台调试代码直接使用console.log() 现在我们重新定义一套打印风格 使用console.log(%c 文字,style); 注意%后面有空格 //自定义控制台 function Console(){ ...

    andong777 评论0 收藏0
  • 重拾写作乐趣——MarkEditor

    摘要:用了一段时间的和,忍无可忍之下几乎要打算自己动手做一款编辑器了。客户端元,客户端元目前元,无任何功能添加。简单易用,而功能又十分强大,除了很多精美的主题,丰富的开放接口,让写作和折腾的双重快感可以同时拥有。 用了一段时间的 Ulysses 和 MWeb,忍无可忍之下几乎要打算自己动手做一款 Markdown 编辑器了。还好,又遇到了 MarkEditor。 成本分析 首先,从各位客官最...

    kyanag 评论0 收藏0
  • 给自己页面添加点乐趣

    摘要:关于可参照张鑫旭大大的讲解设置变化速度然后将方法里面的赋值去掉加上方法如下心形样式可变化心形颜色加上随机颜色的函数下面的属性随位移是变化的可添加你需要变化的样式比如透明度缩放最后加上事件图不会搞撒 2018年开工,大家都很清闲,随意浏览各个社区,有些小发现,希望跟大家分享下,语言组织太差请忽略:大致效果:鼠标每次点击页面,鼠标处便出现一个♥,然后♥慢慢上升至消失。还是直接上代码吧。 1...

    Aomine 评论0 收藏0

发表评论

0条评论

littleGrow

|高级讲师

TA的文章

阅读更多
最新活动
阅读需要支付1元查看
<