其他分享
首页 > 其他分享> > day4

day4

作者:互联网

作业回顾:

# s = '132a4b5c'
# s1 = s[0]+s[2]+s[1]
# print(s1)
#使用while和for循环分别打印字符串s=’asdfer’中每个元素。
s = 'fkld'
# for i in s:
#     print(i)
# index = 0
# while 1:
#     print(s[index])
#     index += 1
#     if index == len(s):break
#如:content = input(‘请输入内容:’)  # 如用户输入:5+9或5+ 9或5 + 9,然后进行分割再进行计算。
# content=input('>>>').strip()
# con1=content.split('+')
#li[]
# num=0
# for i in con1:
#     num+=int(i)
# print(num)
# content=input('>>>').strip()
# index = content.find("+")
# a = int(content[0:index])
# b = int(content[index+1:])
# print(a + b)
#咱们任意输入一串文字+数字 统计出来数字的个数

s = input("请输入:")  # '1234324324fdsaf1fdsaf12'
count = 0
for i in s:
    if i.isdigit():
        count += 1
print(count)
View Code

list:

 li = ['alex',[1,2,3],'wusir','egon','女神','taibai']
# l1 = li[0]
# print(l1)
# l2 = li[1]
# print(l2)
# l3 = li[0:3]
# print(l3)

增:

# append 追加,给列表的最后面追加一个元素
l = [1, 2, 'a']
l.append(666)
print(l) # [1, 2, 'a', 666]

# insert  插入在列表的任意位置插入元素
l = [1, 2, 'a']
l.insert(1,'太白')
print(l) # [1, '太白', 2, 'a']

# extend  迭代着追加,在列表的最后面迭代着追加一组数据
l = [1, 2, 'a']
l.extend('太白a')
print(l)

列表的增
View Code

 

删:

复制代码
# pop  通过索引删除列表中对应的元素,该方法有返回值,返回值为删除的元素
l = ['太白', 'alex', 'WuSir', '女神']
ret = l.pop(1)
print(ret,l) # alex ['太白', 'WuSir', '女神']

# remove  通过元素删除列表中该元素
l = ['太白', 'alex', 'WuSir', '女神']
l.remove('alex')
print(l) # ['太白', 'WuSir', '女神']

# clear 清空列表
l = ['太白', 'alex', 'WuSir', '女神']
l.clear()
print(l) # []  

# del
#按照索引删除该元素
l = ['太白', 'alex', 'WuSir', '女神']
del l[2]
print(l) # ['太白', 'alex', '女神']

# 切片删除该元素
l = ['太白', 'alex', 'WuSir', '女神']
del l[1:]
print(l) # ['太白']

# 切片(步长)删除该元素
l = ['太白', 'alex', 'WuSir', '女神']
del l[::2]
print(l) # ['alex', '女神']
复制代码
View Code

 

改:

# 按照索引改值
l = ['太白', 'alex', 'WuSir', '女神']
l[0] = '男神'
print(l) # ['男神', 'alex', 'WuSir', '女神']

# 按照切片改值(迭代着增加)
l = ['太白', 'alex', 'WuSir', '女神']
l[1:3] = 'abcdefg'
print(l) # ['太白', 'a', 'b', 'c', 'd', 'e', 'f', 'g', '女神'] 

# 按照切片(步长)改值(必须一一对应)
l = ['太白', 'alex', 'WuSir', '女神']
l[::2] = '对应'
print(l) # ['对', 'alex', '应', '女神']

列表的改
View Code

查:

# for i in li:
#     print(i)
# print(li[0:2])

#公共方法:
# l = len(li)
# print(l)
# num = li.count('taibai')
# print(num)
# print(li.index('wusir'))
# li = [1,5,4,7,6,2,3]
# #正向排序
# # li.sort()
# # print(li)
# #反向排序
# # li.sort(reverse=True)
# # print(li)
# #反转
# li.reverse()
# print(li)
View Code

列表的嵌套::

li = ['taibai','武藤兰','苑昊',['alex','egon',89],23]

# print(li[1][1])
# name = li[0].capitalize()
# # print(name)
# li[0] = name
# li[0] = li[0].capitalize()
# li[2] = '苑日天'
# print(li[2].replace('昊','ritian'))
# li[2] = li[2].replace('昊','ritian')
# li[3][0] = li[3][0].upper()
# print(li)
View Code

元祖::

元祖 只读列表,可循环查询,可切片。
儿子不能改,孙子可能可以改。
# tu = (1,2,3,'alex',[2,3,4,'taibai'],'egon')
# # print(tu[3])
# # print(tu[0:4])
# # for i in tu:
# #     print(i)
# tu[4][3]=tu[4][3].upper()
# print(tu)
# tu[4].append('sb')
# print(tu)

# s = 'alex'
# s1 = 'sb'.join(s)
# print(s1)
#列表转化成字符串  list -----> str    join
# li = ['taibai','alex','wusir','egon','女神',]
# s = '++++'.join(li)
#str ----->list   split()
# print(s)


#range  [1,2,3,4,5,6,.......100........]

# for i in range(3,10):
#     print(i)
# for i in range(10):
#     print(i)
# for i in range(0,10,3):
#     print(i)
# for i in range(10,0,-2):
#     print(i)
# for i in range(10,-1,-2):
#     print(i)
View Code

 



li = [1,2,3,5,'alex',[2,3,4,5,'taibai'],'afds']
# for i in li:
#     if type(i) == list:
#         for k in i:
#             print(k)
#     else:print(i)

for i in range(len(li)):
    if type(li[i]) == list:
        for j in li[i]:
            print(j)
    else:print(li[i])

 

 

 

标签:太白,alex,女神,day4,li,WuSir,print
来源: https://www.cnblogs.com/tongge/p/11073065.html