python list 列表解析 方式使用(获取相同,差异,替换元素等)

#!/usr/bin/env python  
#-*- coding: utf-8 -*-
'''
@创建时间:{2018/8/23}
@作者:sharefun2068
@邮箱:
@用途:
'''
list1=['192.168.1.100', '192.168.1.101', '192.168.1.102']
list2=['192.168.1.104', '192.168.1.100','192.168.1.105',]
list3=['192.168.1.100', '192.168.1.101']

#用列表解析的方法实现 ip地址是否包含在白名单中
#['4' if x == '1' else x for x in lst]
#d = [False  if c not in list1 else True for c in list2 ]
d = [False   for c in list2 if c not in list1]
dd = [False if c not in list1 else '11' for c in list2 ]
print(str(d)) #[False, False]
print(str(dd)) #[False, '11', False]

 

#获取2个列表差异元素
list4=[1,2,3,4,5]
list5=[3,4,5,6,7,8]
diff1=[c  for c in list5 if c not in list4]
diff2=[c  for c in list4 if c not in list5]
print diff1 #[6, 7, 8]
print diff2 #[1, 2]
diffTotal=diff1+diff2

print str(diffTotal)#[6, 7, 8, 1, 2]

#获取2个列表相同元素
list6=[1,2,3,4,5]
list7=[3,4,5,6,7,8]

list8=[c for c in list6 if c  in list7]
print list8 #[3, 4, 5]

转载于:https://my.oschina.net/u/3773662/blog/1933097

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值