Python3 实例--Python list 常用操作

#代码如下:


#Python3 实例--Python list 常用操作
print("Python3 实例--Python list 常用操作")

print("1.list定义")
li = ['one', 'two', 'three']
print(li)

print("2.list负数索引")
print(li[-1])

print("3.list增加元素")
li.append("new1")
print(li)
li.insert(2, "new2")
print(li)
li.extend(["new3"])
print(li)

print("4.lsit搜索")
li.index("new2")
print(li.index("new1"))

print("5.list删除元素")
li.remove("new3")
print(li)
li.pop()
print(li.pop())
print(li)

print("6.list运算符")
print(li * 2)
li += ["new"]
print(li)

print("7.使用join链接list成为字符串")
s = "one".join(li)
print(s)

print("8.list分隔字符串")
print("split 与 join 正好相反, 它将一个字符串分割成多元素 list。\n"

"注意, 分隔符 (\";\") 被完全去掉了, 它没有在返回的 list 中的任意元素中出现。\n"

"split 接受一个可选的第二个参数, 它是要分割的次数。")

s = "one;two;three;four"
print(s.split(";"))
print(s.split(";", 1))

print("9.list的映射解析")
lis = [1, 3, 5]
print([(ele * 2) for ele in lis])

print("10.dictionary 中的解析")
dic = {"one":"one", "two":"two", "three":"three"}
print(dic.keys())
print(dic.values())
print(dic.items())

print(["%s=%s" % (k, v) for k, v in dic.items()])


print("11.list过滤")
lislis = ["o", "on", "one"]
print([ele for ele in lislis if len(ele) > 1])




#运行结果如下:
Python3 实例–Python list 常用操作
1.list定义
[‘one’, ‘two’, ‘three’]
2.list负数索引
three
3.list增加元素
[‘one’, ‘two’, ‘three’, ‘new1’]
[‘one’, ‘two’, ‘new2’, ‘three’, ‘new1’]
[‘one’, ‘two’, ‘new2’, ‘three’, ‘new1’, ‘new3’]
4.lsit搜索
4
5.list删除元素
[‘one’, ‘two’, ‘new2’, ‘three’, ‘new1’]
three
[‘one’, ‘two’, ‘new2’]
6.list运算符
[‘one’, ‘two’, ‘new2’, ‘one’, ‘two’, ‘new2’]
[‘one’, ‘two’, ‘new2’, ‘new’]
7.使用join链接list成为字符串
oneonetwoonenew2onenew
8.list分隔字符串
split 与 join 正好相反, 它将一个字符串分割成多元素 list。
注意, 分隔符 (";") 被完全去掉了, 它没有在返回的 list 中的任意元素中出现。
split 接受一个可选的第二个参数, 它是要分割的次数。
[‘one’, ‘two’, ‘three’, ‘four’]
[‘one’, ‘two;three;four’]
9.list的映射解析
[2, 6, 10]
10.dictionary 中的解析
dict_keys([‘one’, ‘two’, ‘three’])
dict_values([‘one’, ‘two’, ‘three’])
dict_items([(‘one’, ‘one’), (‘two’, ‘two’), (‘three’, ‘three’)])
[‘one=one’, ‘two=two’, ‘three=three’]
11.list过滤
[‘on’, ‘one’]

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值