示例用法,快速学废python序列seq列表list数组array各种操作

创作不易 只因热爱!!
**

热衷分享,一起成长!

示例用法,快速学废python序列seq列表list数组array各种操作


讲重点

python序列seq列表list数组array 集合: 交集,并集,差集
序列列表数组转换: 字符串, 元组 , 字典

# 7.多列表的集合: 交集,并集,差集
# 7.1 多列表的集合: 交集,并集,差集
list_a = [1,2]
list_b = [2,3,4,'abc']
list(set(list_a).intersection(set(list_b))) # 交集 [2]
list(set(list_a) | set(list_b)) # 并集 [1, 2, 3, 4, 'abc']
list(set(list_a).difference(set(list_b))) # 差集 [1]
list(set(list_b).difference(set(list_a))) # 差集 [3, 4, 'abc']     
# 7.2 使用Counter求并集,并保留重复元素
from collections import Counter
[item for counter in [Counter(list_a), Counter(list_b)] for item, count in counter.items() for _ in range(count)]

# 8.序列列表数组转换: 字符串, 元组 , 字典
# 8.1 字符串
# 8.1.1 转成字符串
"".join(['l', 'u', 'c', 'k', 'y', 'w', 'i', 't', 'h', '4'])
str(['l', 'u', 'c', 'k', 'y', 'w', 'i', 't', 'h', '4'])
# 8.1.2 字符串转成序列列表数组 eval(string))
list('luckywith4')
eval("['" + "','".join(['l', 'u', 'c', 'k', 'y', 'w', 'i', 't', 'h', '4']) + "']")

# 8.2 转成元组序列
list(enumerate(['l', 'u', 'c', 'k', 'y', 'w', 'i', 't', 'h', '4']))

# 8.3 转成字典
list_test = list('luckywith4')
# 8.3.1 字典
{k:v for k,v in list(enumerate(list_test))}
# 8.3.2 字典
{i: list_test[i] for i in range(len(list_test))}
# 8.3.3 字典
dict(zip(range(len(list_test)),list_test ))

讲基础

python序列seq列表list数组array各种操作

# 定义序列.定义列表.定义数组 
list_test = list('luckywith4')
print(list_test) # ['l', 'u', 'c', 'k', 'y', 'w', 'i', 't', 'h', '4']
list_a = [1,2]
list_b = [2,3,4,'abc']
list_c = ['1','2','3','4','5']

# 2.访问列表中的值
# 2.1 list列表下标从0开始计,list_a的 key:0->value:1
print(list_a[0]) # 1
print(list_c[-2])  # '4'

# 2.2 截取,切片,列表中一段元素,区间值. [start:end:step]区间start在,end不在,step步长.
print(list_c[0:3])  # ['1', '2', '3']
print(list_c[2:999]) # ['3', '4', '5']
print(list_c[::-2]) # ['5', '3', '1']

# 2.3 访问列表中的索引值, key
print(list_b.index('abc')) # 3
                    
# 3.删除元素  pop,remove,del区别用法, .clear() 清空.
# 3.1 pop操作key并返回值value, remove操作value无返回值. 删除元素
list_a = [1,2]
1 == list_a.pop(0) 
print(list_a) # [2]

list_a = [1,2]
list_a.remove(1) 
print(list_a) # [2]
# 3.1.1 注意value不存在时抛出错误,list_a不重新定义,再次运行抛出异常
try:
    list_a.remove(1) 
except Exception as e:
    print(e)  # list.remove(x): x not in list

# 3.2 del操作key, 删除元素
list_b = [2,3,4,'abc']
del list_b[0]
print(list_b) # [3,4,'abc']

# 4.修改元素
# 4.1.1 插入新元素 .insert(key,value)
list_a = [1,2]
list_a.insert(1, '987')
print(list_a) # [ 1,'987', 2]
# 4.1.2 在列表末尾添加新的元素 .append(value)
list_a = [1,2]
list_a.append('987')
print(list_a) # [1, 2, '987']
# 4.1.2 在列表末尾添加新的多元素 .extend(list)
list_a.extend(list_b)
print(list_a) # [1, 2, '987', 2, 3, 4, 'abc']

# 4.2 变更原有元素 list[key] = new_value
list_a = [1,2]
list_a[0] = [1,2]
print(list_a) # [[1, 2], 2]

# 5.反转元素 前后倒置 .reverse()  reversed(list)
list_b = [2,3,4,'abc']
list_b.reverse()
print(list_b) # ['abc', 4, 3, 2]  
# print(reversed(list_b))

# 6.其他: 列表长度, 列表元素出现次数, 列表排序 .sort() sorted(list)
list_test = [1, 5, 3, 3, 2, 4]
print(len(list_test)) # 6
print(list_test.count(3)) # 2

list_test.sort()
print(list_test) # [1, 2, 3, 3, 4, 5] 
# print(sorted(list_test))

end

^**你好呀,我是一个医信行业工程师,喜欢学习,喜欢搞机,喜欢各种捣,也会持续分享,如果喜欢我,那就关注我吧!**^


作者|医信工程师随笔|Carltiger_github

图片|网络|侵删!!

关注我,我们共同成长

“你的鼓励就是我分享的动力”

  • 8
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值