<jupyter_start><jupyter_text>列表List- 一个列表可以储存任意大小的数据集合，你可以理解为他是一个容器  先来一个例子爽一爽![](../Photo/115.png)<jupyter_code>noe=5
number=[]
sum1=0
for i in range(noe):
    value=eval(input('enter a new number:'))
    number.append(value)
    sum1+=value
average=sum1/noe
count=0
for i in range(noe):
    if number[i]>average:
        count+=1
print('Average is ',average)
print('Number of elements above the average is ',count)<jupyter_output>enter a new number:1
enter a new number:2
enter a new number:3
enter a new number:4
enter a new number:5
Average is  3.0
Number of elements above the average is  2<jupyter_text>创建一个列表- a = [1,2,3,4,5]  列表的一般操作![](../Photo/116.png)<jupyter_code>a=[1,2,3]
for i in a:
    print(i)<jupyter_output>1
2
3<jupyter_text>list()函数可以将一个可迭代对象转换成列表list('abc')<jupyter_code>list('abc')
a2=[1,2,3,'hahah',True]
100 in a2
a3=[100,200,300,'lalal',False]
a2+a3
a4=[1.0,2,3,4,5]
sum1=0
sum2=0
for i in a4:
    sum1=sum1+i
print(sum1)
print(sum(a4))
for j in range(len(a4)):
    sum2+=a4[j]
print(sum2)
a5=[1,2,3]
a6=[1,1]
a5==a6
try:
    a5[3]
except Exception as e:
    print(e)
try:
    a5[3]
except Exception as e:
    print('发生未知错误！')<jupyter_output>发生未知错误！<jupyter_text>列表索引操作- Mylist[index]- 正序索引，逆序索引- 列表一定注意越界- ![](../Photo/117.png)  列表切片操作- Mylist[start:end]- 正序切片，逆序切片  列表 +、*、in 、not in  使用for循环遍历元素- for 循环可以遍历一切可迭代元素  EP：- 使用while 循环遍历列表  列表的比较- \>,=,<=,==,!=  列表生成式[x for x in range(10)]<jupyter_code>a=[x for x in range(10)]
a
b=[x for x in range(10) if x %2==0]
b<jupyter_output><empty_output><jupyter_text>列表生成器 yield 关键字是函数生成器 生成器目的主要是为了内存优化<jupyter_code>a=(x for x in range(10)) #列表生成器，用到一个取一个
next(a)
next(a)
next(a)
next(a)
b=[x for x in 'Joker']
b<jupyter_output><empty_output><jupyter_text>列表的方法![](../Photo/118.png)<jupyter_code>b.append('r')
b
b.count('r')
b.extend('ab')
b
b.extend(b)
b<jupyter_output><empty_output><jupyter_text>将字符串分割成列表- split  按照自定义的内容拆分<jupyter_code>b1=[x for x in range(100)]
b2=b1[-1:-len(b1)-1:-1]
b2
b3=[x for x in range(10)]
for i in range(len(b3)//2):
    #b3[0],b3[9]=b3[9],b3[0]
    b3[i],b[len(b3)-1-i]=b[len(b3)-1-i],b3[i]
b3  
b4=[0,5,1,4,9]
L=len(b4)
for i in range(L-1):
    for j in range(L-i-1):
        if b4[j]>b4[j+1]:
            b4[j],b4[j+1]=b4[j+1],b4[j]
b4
b5=[['Joker',19],['xiaoming',73],['xiaohua',35],['xiaomei',4]]
L=len(b5)
for i in range(L-1):
    for j in range(L-i-1):
        if b5[j][1]>b5[j+1][1]:
            b5[j][1],b5[j+1][1]=b5[j+1][1],b5[j][1]
b5<jupyter_output><empty_output><jupyter_text>lambda作为匿名函数，'：'前为输入内容 '：'后为输出内容<jupyter_code>b5=[['Joker',19],['xiaoming',73],['xiaohua',35],['xiaomei',4]]
b5.sort(key=lambda x:x[1])    #lambda作为匿名函数，：前为输入内容 ’：‘后为输出内容
b5
b5=[['Joker',19,['a',5]],['xiaoming',73,['b',3]],['xiaohua',35,['c',65]],['xiaomei',4,['d',34]]]
b5.sort(key=lambda x:x[2][1])
b5
b5=[['Joker',19,['a',5]],['xiaoming',73,['b',3]],['xiaohua',35,['c',65]],['xiaomei',4,['d',34]]]
b5.sort()
b5
b7=[1,2,3]
b7.pop(1)
b7<jupyter_output><empty_output><jupyter_text>EP:![](../Photo/119.png)![](../Photo/120.png)  列表的复制- copy 浅复制    单层都是浅拷贝，如果列表中还有子列表，则需要进行深拷贝- deepcopy import copy  深复制- http://www.pythontutor.com/visualize.htmlmode=edit<jupyter_code>b5=[1,2,3]
b6=b5.copy()
b6[0]=100
print(b5,b6)
b5=[1,2,3,[100,200]]
b6=b5.copy()
b6[3][1]=1000
print(b5,b6)
import copy
b5=[1,2,3,[100,200]]
#b6=b5.copy()
b6=copy.deepcopy(b5)
b6[3][1]=1000
print(b5,b6)
strq='a,b,c,d,e'
lsq=strq.split(',',3)
lsq<jupyter_output><empty_output><jupyter_text>列表排序- sort- sorted    返回一个新的列表，不改变原来列表- 列表的多级排序    - 匿名函数  EP：- 手动排序该列表[5,3,8,0,17]，以升序或者降序<jupyter_code>b5=[['Joker',19,['a',5]],['xiaoming',73,['b',3]],['xiaohua',35,['c',65]],['xiaomei',4,['d',34]]]
b6=sorted(b5,key=lambda x:x[2][1],reverse=True)
b6
stra='asdflklasdfkjsadfklaskldgfklfjaslkfglkahsdgfkhkajsdfkljasldf'
lsa=list(stra)
lsb=lsa
lsa[0]='q'
lsb<jupyter_output><empty_output><jupyter_text>- 1![](../Photo/121.png)<jupyter_code>def degreescore():
    lsscore=[int(x) for x in input('enter scores:').split()]
    best=max(lsscore)
    j=0
    for i in lsscore:
        if i>=best-10:
            print('Student ',j,' score is ',i,'and grade is A')
            j+=1
        elif i>=best-20:
            print('Student ',j,' score is ',i,'and grade is B')
            j+=1
        elif i>=best-30:
            print('Student ',j,' score is ',i,'and grade is C')
            j+=1
        elif i>=best-40:
            print('Student ',j,' score is ',i,'and grade is D')
            j+=1
        else:
            print('Student ',j,' score is ',i,'and grade is F')
            j+=1
degreescore()<jupyter_output>enter scores:40 55 70 58 23
Student  0  score is  40 and grade is C
Student  1  score is  55 and grade is B
Student  2  score is  70 and grade is A
Student  3  score is  58 and grade is B
Student  4  score is  23 and grade is F<jupyter_text>- 2![](../Photo/122.png)<jupyter_code>lsint=[int(x) for x in input('enter a int list:').split()]
newint=lsint[-1:-len(b1)-1:-1]
for i in newint:
    print(i,end=' ')<jupyter_output>enter a int list:20 30 40 50 60
60 50 40 30 20<jupyter_text>- 3![](../Photo/123.png)<jupyter_code>lsint=[int(x) for x in input('enter integers between 1 and 100:').split()]
for i in range(100):
    if i in lsint:
        print(i,'occurs',lsint.count(i),'times')<jupyter_output>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)<jupyter_code>lsint=[int(x) for x in input('enter integers between 1 and 100:').split()]
arv=sum(lsint)/len(lsint)
eqd=0
eqx=0
for i in lsint:
    if i>=arv:
        eqd+=1
    else:
        eqx+=1
print('平均分是',arv,' \n大于平均分的个数有',eqd,' \n小于平均分的个数有',eqx)<jupyter_output>enter integers between 1 and 100:20 40 20 30 50 60 40
平均分是 37.142857142857146  
大于平均分的个数有 4  
小于平均分的个数有 3<jupyter_text>- 5![](../Photo/125.png)<jupyter_code>import random
counts=[]
for i in range(10):
    counts.append(i-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 的个数是： 94
1 的个数是： 106
2 的个数是： 109
3 的个数是： 88
4 的个数是： 92
5 的个数是： 85
6 的个数是： 118
7 的个数是： 98
8 的个数是： 93
9 的个数是： 117<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 :20 50 60 40 50 55 8
6<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 5 6 7 8 9
2 3 7 6 5 9 4 8 1<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 4 3 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:3 2 5 46 2
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:9 8 7 6 5 4 3 2 1 10
1 2 3 4 5 6 7 8 9 10<jupyter_text>- 11![](../Photo/132.png) - 12![](../Photo/133.png)<jupyter_code>for i in range(5):
    pass
print(i)
def iseq(lst):
    if lst.count(lst[0])==4:
        return True
    else:
        return False
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:1 2 3 4 5 6 8 6 6 6 6 6 8 7 
存在
{'fraction_non_alphanumeric': 0.1893913634818089, 'fraction_numerical': 0.07763799161283011, 'mean_word_length': 4.091748413156377, 'pattern_counts': {'":': 0, '<': 72, '<?xml version=': 0, '>': 81, 'https://': 0, 'lorem ipsum': 0, 'www.': 1, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '21', 'dir': 'jupyter-structured-clean-dedup', 'id': '233425', 'license': 'Apache-2.0', 'n_tokens_mistral': 4585, 'n_tokens_neox': 4220, 'n_words': 857, 'path': '7.25.ipynb', 'repo_name': 'jastermax/mypython', 'size': '8823'}