其他分享
首页 > 其他分享> > day 4 list,元祖

day 4 list,元祖

作者:互联网

1,作业讲解

 1 # s = '132a4b5c'
 2 # s1 = s[0]+s[2]+s[1]
 3 # print(s1)
 4 
 5 index = 0
 6 # while 1:
 7 #     print(s[index])
 8 #     index+=1
 9 #     if index == len(s):break
10 
11 
12 # 如: conten = input ('请输入内容)
13 
14 # 如果用户输入:5+9  或 5+ 9或5 + 9,然后进行分割计算
15 # content = input('>>>').strip()
16 # index = content.find('+')
17 # a =  int (content[0:index])
18 # b =  int(content[index+1:])
19 # print(a+b)
20 
21 # 任意输入一串文字+数字,统计出数字的个数
22 s = input('请输入:')
23 count = 0
24 for i in s:
25     if i.isdigit():
26         count += 1
27 print(count)
28 # #使用while和for循环分别打印字符串s='fkld'中的每个元素
29 # s='asferd'
30 # for i in  s:
31 #     print(i)
32 #while
33 
34 # 

2.list相关内容

1.list基础操作

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

2.append增加

 1 # li = ['alex',[1,2,3],'wusir','egon','女神','taibai']
 2 # li.append('日天')
 3 # li.append(1)
 4 # print(li)
 5 # li = ['alex',[1,2,3],'wusir','egon','女神','taibai']
 6 # while 1:
 7 #     yuangong=input('输入姓名:')
 8 #     if yuangong.strip().upper()== 'Q':
 9 #         break
10 #         li.append('yuangong ')
11 # print(li)

(1)指定位置添加

# li = ['alex',[1,2,3],'wusir','egon','女神','taibai']
# li.insert(4,'春哥')
# print(li)

(2)迭代添加

1 # li = ['alex',[1,2,3],'wusir','egon','女神','taibai']
2 # li.extend('二哥')
3 # li.extend('1,2,3')
4 # print(li)

3.删除

1 # li = ['alex',[1,2,3],'wusir','egon','女神','taibai']
2 # name=li.pop(1) #返回值
3 # # name=li.pop()#默认删除最后一个
4 # print(name,li)

(1)按元素删除

1 #li.remove('taibai)#按元素删
2 #print(li)

(2)清空

1 #li.clear()#清空
2 #print(li)

(3)切片删除

#del li
# li = ['alex',[1,2,3],'wusir','egon','女神','taibai']
# del li[0:2]#切片删除
# print(li)

4.改

1 # li[0] = '男神'
2 # li[0] = [1,2,3]
3 #切片
4 # li[0:3] = '云姐hahhakdkald'
5 # li[0:3] = [1,2,3,'春哥','泰格']
6 # print(li)

5.查

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

6.公共发方法

 1 # l = len(li)
 2 # print(l)
 3 # num=li.count('taibai')
 4 # print(num)
 5 # print(li.index('wusir'))
 6 
 7 # li = [1,4,2,6,3]
 8 #正序排序
 9 # li.sort()
10 # print(li)
11 #反向排序
12 # li.sort(reverse=True)
13 # print(li)
14 #反转
15 # li.reverse()
16 # print(li)

7.列表嵌套

 1 li = ['taibai','武藤兰','苑昊',['alex','egon',89],23]
 2 # print(li[1][1])
 3 # print(li[0].capitalize())
 4 # name=li[0].capitalize()
 5 # li[0]=name
 6 # print(li)
 7 # li[2]='苑日天'
 8 # print(li)
 9 # li[2]=li[2].replace('昊','日天')
10 # print(li)
11 li[3][0]=li[3][0].upper()
12 print(li)li = ['taibai','武藤兰','苑昊',['alex','egon',89],23]
13 # print(li[1][1])
14 # print(li[0].capitalize())
15 # name=li[0].capitalize()
16 # li[0]=name
17 # print(li)
18 # li[2]='苑日天'
19 # print(li)
20 # li[2]=li[2].replace('昊','日天')
21 # print(li)
22 li[3][0]=li[3][0].upper()
23 print(li)

3.元祖相关内容

1.元祖基础操作

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

2.字符连接

1 # s='alex'
2 #字符连接
3 # s1='_'.join(s)
4 # print(s1)

3.list——>str

  str——>list

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

4.range

 1 #range
 2 # for i in range(0,100):
 3 #     print(i)
 4 # for i in range(100):
 5 # #     print(i)
 6 # for i in range(0,100,3):
 7 #     print(i)
 8 # for i in range(100,0,-2):
 9 #     print(i)
10 # for i in range(100,-1,-2):
11 #     print(i)

5,打印列表元素,并且打印元素是列表里的元素

 1 li=[1,2,3,5,'alex',[2,3,4,5,'alex'],'afds']
 2 #
 3 # for i in li:
 4 #     if type(i)==list:
 5 #       for j in i:
 6 #           print(j)
 7 #     else:
 8 #      print(i)
 9 for i in range(len(li)):
10      if type(li[i]) == list:
11          for j in li[i]:
12              print(j)
13      else:
14          print(li[i])
15 # for i in li:
16 #     if type(i) == list:
17 #         for k in i:
18 #             print(k)
19 #     else:print(i)

 

标签:alex,list,li,taibai,print,egon,元祖,day
来源: https://www.cnblogs.com/gcs888/p/10745118.html