Python笔记:List相关操作

List相关操作小例子

获取list的下标和值

>>> mylist = ['a', 'b', 'c', 'd']
>>> for index, value in enumerate(mylist):
...         print(index, value)
...
0 a
1 b
2 c
3 d
>>>

删除list中的空字符

list1 = ['1', '','2', '3', '  ', ' 4  ', '  5', '    ','6 ', '', '     ',None, '7']
print(list1)
list2 = list(filter(None, list1)) 
print(list2) # ['1', '2', '3', '  ', ' 4  ', '  5', '    ', '6 ', '     ', '7']
list3 = [x.strip() for x in list2]
print(list3) # ['1', '2', '3', '', '4', '5', '', '6', '', '7']
list4 = list(filter(None, list3))  
print(list4) # ['1', '2', '3', '4', '5', '6', '7']

删除list元素

使用remove、pop和del方法参删除list中的某个元素

>>> mylist = ['a', 'b', 'c', 'd','e','f','g','h']
>>> mylist.remove('a')
>>> mylist
['b', 'c', 'd', 'e', 'f', 'g', 'h']
>>> mylist.pop(0)
'b'
>>> mylist
['c', 'd', 'e', 'f', 'g', 'h']
>>> del mylist[0]
>>> mylist
['d', 'e', 'f', 'g', 'h']
>>> del mylist[0:2]
>>> mylist
['f', 'g', 'h']
>>> del mylist
>>> mylist
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'mylist' is not defined
>>>

计算中位数

def get_median(data):
     data.sort()
     half = len(data) // 2
     return (data[half] + data[~half]) / 2

将字符串list转换为int

>>> test_list = ['1', '4', '3', '6', '7']
>>> test_list = list(map(int, test_list))
>>> test_list
[1, 4, 3, 6, 7]
>>>

合并、连接字符串list

>>> test_list = ['192', '168', '0', '1']
>>> test_list = '.'.join(test_list)
>>> test_list
'192.168.0.1'
>>>

取多个字符串/list交集

>>> a = ['123','234','1253']
>>> list(reduce(lambda x,y : set(x) & set(y), a))
['2', '3']
>>> b = [[1,2,3],[1,2],[1,2,3,4],[12,1,2]]
>>> list(reduce(lambda x,y : set(x) & set(y), b))
[1, 2]

合并字典value值

>>> mydict = {0:"hello ", 1:"world"}
>>> mylist =reduce(lambda x, y : x + y, mydict.values())
>>> mylist
'hello world'
>>> mydict = {0:[1,2,3,4], 1:[2,3,4,5,6]}
>>> mylist = list(reduce(lambda x, y : x + y, mydict.values()))
>>> mylist
[1, 2, 3, 4, 2, 3, 4, 5, 6]
>>> field_counters = dict(Counter(mylist))
>>> field_counters
{1: 1, 2: 2, 3: 2, 4: 2, 5: 1, 6: 1}

列表排列组合

列表排列组合可以使用迭代器模块itertools

列表排列

import itertools

l = [1, 2, 3]
print(list(itertools.permutations(l, 2)))
print(list(itertools.permutations(l, 3)))

结果:

[(1, 2), (1, 3), (2, 1), (2, 3), (3, 1), (3, 2)]
[(1, 2, 3), (1, 3, 2), (2, 1, 3), (2, 3, 1), (3, 1, 2), (3, 2, 1)]

组合

import itertools

l = [1, 2, 3]
print(list(itertools.combinations(l, 2)))

结果:

[(1, 2), (1, 3), (2, 3)]

多个列表元素组合:笛卡尔积

两个列表元素两两组合:

import itertools

l = [1, 2, 3]
l1 = [11, 12, 13]
l2 = [21, 22, 23]
# 笛卡尔积
print(list(itertools.product(l, l)))
print(list(itertools.product(l, repeat=2)))
print(list(itertools.product(l1, l2)))

结果:

[(1, 1), (1, 2), (1, 3), (2, 1), (2, 2), (2, 3), (3, 1), (3, 2), (3, 3)]
[(1, 1), (1, 2), (1, 3), (2, 1), (2, 2), (2, 3), (3, 1), (3, 2), (3, 3)]
[(11, 21), (11, 22), (11, 23), (12, 21), (12, 22), (12, 23), (13, 21), (13, 22), (13, 23)]

列表倒序遍历

# reverse方法
a = [1, 2, 3]
a.reverse()
for n in a:
    print(n)

# reversed
for n in reversed(a):
    print(n)  
    
# 切片
a = [1, 2, 3]
b = a[::-1]
--THE END--

系列文章

1. VSCode + Python环境配置
2. Python PEP—Python增强提案
3. 正则表达式介绍及Python使用方法
4. Python笔记:List相关操作
5. Python笔记:字符串操作
6. Python函数的参数类型
7. Python反射介绍
8. Python笔记:属性值设置和判断变量是否存在
9. Python中的__new__、__init__以及metaclass
10. Python对象及内存管理机制
11. Python内存驻留机制
12. Python笔记:Python装饰器
13. Python中的闭包
14. Python笔记:lambda匿名函数
15. Python多线程与多进程
16. Python协程
17. Python笔记:日期时间获取与转换
18. Python笔记:命令行参数解析
19. Python 命令行参数解析之 Click
20. Python json文件读写
21. Python yaml文件读写
22. Python Scapy 报文构造和解析


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值