<jupyter_start><jupyter_text>列表List- 一个列表可以储存任意大小的数据集合，你可以理解为他是一个容器  先来一个例子爽一爽![](../Photo/115.png)<jupyter_code>NUMBER_OF_ELEMENTS=5
numbers=[]
sum=0
for i in range(NUMBER_OF_ELEMENTS):
    value=eval(input("Enter a new number:"))
    numbers.append(value)
    sum+=value
average=sum/NUMBER_OF_ELEMENTS
count=0
for i in range(NUMBER_OF_ELEMENTS):
    if numbers[i]>average:
        count+=1
        
print("average is",average)
print("Number of elements above the average is",count)<jupyter_output>Enter a new number:2
Enter a new number:2
Enter a new number:3
Enter a new number:4
Enter a new number:5
average is 3.2
Number of elements above the average is 2<jupyter_text>创建一个列表- a = [1,2,3,4,5]<jupyter_code>NUMBER_OF_ELEMENTS=5
a=[]
for i in range(NUMBER_OF_ELEMENTS):
    value=eval(input("Enter a new number:"))
    a.append(value)
print(a)
a=[1,2,3,4,5,'a',0.1,True,'s','d','f']
a[0:]
a[0:-1]
a[0:4:2]
len(a)
sum(a)
a4=[1,2,3,4,5]
sum(a4)
for i in a4:
    print(i)    
a5=[1,2]
a6=[3,4]
a5>a6<jupyter_output><empty_output><jupyter_text>列表的一般操作![](../Photo/116.png)  列表索引操作- Mylist[index]- 正序索引，逆序索引- 列表一定注意越界- ![](../Photo/117.png)<jupyter_code>try:
    a5[3]
except Exception as e:
    print('fashenfweizhicuowu')
    
file_path ='\\Users\\Administrator\\Desktop\\kaifangX.txt'
write_path ='\\Users\\Administrator\\Desktop\\lala.txt'
open_file_path=open(file_path,mode='r',encoding='gbk',errors='ignore') 
open_write_path=open(file_path,mode='w',encoding='gbk') 
for i in range(100):
     a=f.readline()
        b=a.strip('\n')
        c=b.split(',')
        #print(c)
        try:
            d=c[-2]
            if'@'in d:
                print(d)
            else:
                pass
        except Exception as e:
            print(e)
open_file_path.closs()
open_write_path.closs()
file_path ='\\Users\\Administrator\\Desktop\\kaifangX.txt'
with open(file_path,mode='r',encoding='gbk',errors='ignore') as f:
    while 1:
        a=f.readline()
        b=a.strip('\n')
        c=b.split(',')
        #print(c)
        try:
            d=c[-2]
            if'@'in d:
            print(d[])
            else:
                pass
        except Exception as e:
            print(e)<jupyter_output><empty_output><jupyter_text>列表切片操作- Mylist[start:end]- 正序切片，逆序切片  列表 +、*、in 、not in  使用for循环遍历元素- for 循环可以遍历一切可迭代元素  EP：- 使用while 循环遍历列表  列表的比较- \>,=,<=,==,!=  列表生成式[x for x in range(10)]<jupyter_code>(lambda x:print(x))(10)
a=[x for x in range(10)]
a
a=[x for x in input()]
a
a=[x for x in input().split()]
a
a=[int(x) for x in input().split()]
a
a=[x for x in range(10) if x%2==0]
a
a=(x for x in range(10))
next(a)
next(a)

a=[x for x in range(10)]
next(a)
next(a)
next(a)
next(a)
next(a)
next(a)
next(a)
next(a)
next(a)
next(a)
next(a)
next(a)
def test():
    yield"hahah"
a1=test()
next(a1)<jupyter_output><empty_output><jupyter_text>列表的方法![](../Photo/118.png)<jupyter_code>a=[x for x in range(10)]
for i in"joker":
    a.append(i)
a
b=[x for x in 'jokerr']
b
b.index('j')
b.insert(0,[1,2,3])
b
b.remove('j')
b
l1=[x for x in range(10)]
l1.reverse()
l1
b2=[0,5,1,4,9]
b2.sort()
b2
b2.sort(reverse=True)
b2
b3=[0,5,1,4,9]
for i in range(len(b3)):
    for j in range(i,len(b3)):
        if b3[i]>b3[j]:
            b3[i],b3[j]= b3[j],b3[i]
b3
b4=[['joker',180],['xiaoming',28],['xiaoli',1],['xiaohua',34]]
b4.sort(key=lambda x:x[1])
b4
b5=[['joker',180,['a',5]],['xiaoming',28,['b',1]],['xiaoli',1,['c',3]],['xiaohua',34,['d',7]]]
b5.sort(key=lambda x:x[2][1])
b5
sorted(b5,key=lambda x:x[0],reverse=False)<jupyter_output><empty_output><jupyter_text>将字符串分割成列表- split  按照自定义的内容拆分<jupyter_code>b7='a b c d'
b7.split()
b7='a,b,c,d'
b7.split(',')
b7='a,b,c,d'
b7.split(',',maxsplit=2)<jupyter_output><empty_output><jupyter_text>EP:![](../Photo/119.png)![](../Photo/120.png)  列表的复制- copy 浅复制- deepcopy import copy  深复制- http://www.pythontutor.com/visualize.htmlmode=edit  列表排序- sort- sorted- 列表的多级排序    - 匿名函数  EP：- 手动排序该列表[5,3,8,0,17]，以升序或者降序 - 1![](../Photo/121.png)<jupyter_code>def shuru():
    NUMBER_OF_ELEMENTS=9
    a=[]
    for i in range(NUMBER_OF_ELEMENTS):
        value=eval(input())
        a.append(value)
    return a
shuru()
score=[40,55,70,58]
print("Enter scores:"%score)

for i in range(len(score)):
    best=70
    if score[i]>best-10:
        grade='A'
        print( '%d      %d       %s'%(i,score[i],grade))
    elif score[i]>best-20:
        grade='B'
        print( '%d      %d       %s'%(i,score[i],grade))
    elif score[i]>best-30:
        grade='C'
        print( '%d      %d       %s'%(i,score[i],grade))
    elif score[i]>best-40:
        grade='D'
        print( '%d      %d       %s'%(i,score[i],grade))
    else:
        grade='F'
        print( '%d      %d       %s'%(i,score[i],grade))<jupyter_output>Enter scores:
0      40       D
1      55       B
2      70       A
3      58       B<jupyter_text>- 2![](../Photo/122.png)<jupyter_code>NUMBER_OF_ELEMENTS=5
a=[]
for i in range(NUMBER_OF_ELEMENTS):
    value=eval(input("Enter a new number:"))
    a.append(value)
print(a)
a.reverse()
a<jupyter_output>Enter a new number:1
Enter a new number:3
Enter a new number:4
Enter a new number:2
Enter a new number:8
[1, 3, 4, 2, 8]<jupyter_text>- 3![](../Photo/123.png)<jupyter_code>def shuru():
    NUMBER_OF_ELEMENTS=9
    a=[]
    for i in range(NUMBER_OF_ELEMENTS):
        value=eval(input())
        a.append(value)
    return a

a=shuru()
print("Enter integers between 1 and 100:")
print(a)
print("2 occurs %d times"%a.count(2))
print("3 occurs %d times"%a.count(3))
print("4 occurs %d times"%a.count(4))
print("5 occurs %d times"%a.count(5))
print("6 occurs %d times"%a.count(6))
print("23 occurs %d times"%a.count(23))
print("43 occurs %d times"%a.count(43))<jupyter_output>2
5
6
5
4
3
23
43
2
Enter integers between 1 and 100:
[2, 5, 6, 5, 4, 3, 23, 43, 2]
2 occurs 2 times
3 occurs 1 times
4 occurs 1 times
5 occurs 2 times
6 occurs 1 times
23 occurs 1 times
43 occurs 1 times<jupyter_text>- 4![](../Photo/124.png) - 5![](../Photo/125.png)<jupyter_code>import random
counts=[]
for i in range(10):
    counts.append(i)
for i in range(1000):
    num=random.randint(0,9)
    counts[num]+=1
for i in range(10):
    print(i,'的个数是：',counts[i])<jupyter_output>0 的个数是： 105
1 的个数是： 92
2 的个数是： 107
3 的个数是： 115
4 的个数是： 111
5 的个数是： 102
6 的个数是： 100
7 的个数是： 94
8 的个数是： 103
9 的个数是： 116<jupyter_text>- 6![](../Photo/126.png)<jupyter_code>def indexofsmallestE(lstint):
    m=min(lsint)
    for i in range(len(lsint)):
        if lsint[i]==m:
            print(i)
            break
lsint=[int(x) for x in input('enter integers :').split()]
indexofsmallestE(lsint)<jupyter_output>enter integers :52 3 6 8 98 65 34 23
1<jupyter_text>- 7![](../Photo/127.png)![](../Photo/128.png)<jupyter_code>import random
def shuffle(lst):
    ll=len(lst)
    for i in range(10):
        la=random.randint(0,ll-1)
        lb=random.randint(0,ll-1)
        lst[la],lst[lb]=lst[lb],lst[la]
    return lst
lsint=[int(x) for x in input('enter integers :').split()]
lsint1=shuffle(lsint)
for i in lsint:
    print(i,end=' ')<jupyter_output>enter integers :1 2 3 4 6 5 4 3 8 
4 8 2 1 4 5 3 3 6<jupyter_text>- 8![](../Photo/129.png)<jupyter_code>def eldup(lst):
    ll=len(lst)
    lst1=[]
    for i in range(ll):
        if lst[i] not in lst1:
            lst1.append(lst[i])
    return lst1  
lsint=[int(x) for x in input('enter integers :').split()]
lsint=eldup(lsint)
print('The distinct numbers are:')
for i in lsint:
    print(i,end=' ')<jupyter_output>enter integers :1 2 3 2 1 6 3 4 5 2
The distinct numbers are:
1 2 3 6 4 5<jupyter_text>- 9![](../Photo/130.png)<jupyter_code>def issorted(lst):
    lst1=sorted(lst)
    if lst1 == lst:
        return True
    else:
        return False
lsint=[int(x) for x in input('enter integers list:').split()]
if issorted(lsint):
    print('The list is already sorted')
else:
    print('The list is not sorted')<jupyter_output>enter integers list:1 1 3 4 4 5 7 9 10 30 11
The list is not sorted<jupyter_text>- 10![](../Photo/131.png)<jupyter_code>def qipao(lst):
    ll=len(lst)
    for i in range(ll-1):
        for j in range(ll-i-1):
            if lst[j]>lst[j+1]:
                lst[j],lst[j+1]=lst[j+1],lst[j]
    return lst
lsint=[int(x) for x in input('enter 10 integers list:').split()]
lsint=qipao(lsint)
for i in range(len(lsint)):
    print(lsint[i],end=' ')<jupyter_output>enter 10 integers list:1 2 4 6 3 9 6 7 4 5 
1 2 3 4 4 5 6 6 7 9<jupyter_text>- 11![](../Photo/132.png) - 12![](../Photo/133.png)<jupyter_code>lsint=[int(x) for x in input('enter integers list:').split()]
flag=0
for i in range(len(lsint)-3):
    newls=lsint[i:i+4]
    #print(newls,iseq(newls))
    if iseq(newls):
        flag=1
        break
if flag ==1:
    print('存在')
else:
    print('不存在')<jupyter_output>enter integers list:123466778889
不存在
StarcoderdataJupyterStructuredCleanDedup