首页 技术 正文
技术 2022年11月7日
0 收藏 446 点赞 873 浏览 1353 个字

赋值

>>> list=[]
>>> app=[list,list,list]
>>> app
[[], [], []]
>>> app[1].append(1)
>>> app
[[1], [1], [1]]
>>> id(app[1])
1666670423944
>>> id(app[2])
1666670423944

条件语句:

>>> app=[1,'',"cat",[]]
>>> for i in app:
print(app[i])
>>> for i in app:
print(i)1cat
[]
>>> for i in app:
if i:
print(i)1
cat
>>> [i for i in app]
[1, '', 'cat', []]
>>> [i for i in app if i]
[1, 'cat']

any和all

>>> any(w!=0 for w in app)
True
>>> all(w!='' for w in app)
False

元组:

>>> t='ass',33,''
>>> t
('ass', 33, '')

各种遍历序列的方式

>>> s
(2, 5, 3, 1, 6, 7, 95, 3)
>>> [n for n in s]
[2, 5, 3, 1, 6, 7, 95, 3]
>>> [n for n in sorted(s)]
[1, 2, 3, 3, 5, 6, 7, 95]
>>> [n for n in set(s)]
[1, 2, 3, 5, 6, 7, 95]
>>> [n for n in reversed(s)]
[3, 95, 7, 6, 1, 3, 5, 2]
>>> [n for n in s[::-1]]
[3, 95, 7, 6, 1, 3, 5, 2]
>>> [n for n in set(s).difference(t)]
[1, 2, 3, 5, 6, 7, 95]
>>> t=[1,23,4,5]
>>> [n for n in set(s).difference(t)]
[2, 3, 6, 7, 95]
>>> [n for n in random.shuffle(s)]

训练集和测试集语料划分:9:1

>>> text=open(r"C:\Users\BNC-PC\Desktop\text.txt","r").read()
>>> len(text)
34176
>>> cut=int(0.9*len(text))
>>> training_data,test_data=text[:cut],text[cut:]
>>> len(training_data)
30758
>>> len(test_data)
3418
>>> text == training_data + test_data
True
>>> len(training_data)/len(test_data)
8.998829724985372
>>>

合并

>>> words='我 是 中国 人 , 我 爱 祖国'.split()
>>> worelen=[w for w in words]
>>> worelen
['我', '是', '中国', '人', ',', '我', '爱', '祖国']
>>> '='.join(w for w in worelen)
'我=是=中国=人=,=我=爱=祖国'

函数:

def bncsum(m,n):
sum=0
if n:
sum=m/n
else:
sum=n
print(sum)>>> bncsum(4,5)
0.8
>>> bncsum(4,0)
0
>>> help(bncsum)
Help on function bncsum in module __main__:bncsum(m, n)

  

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,000
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,512
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,358
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,141
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,771
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,849