- 仅对数字进行除法运算
t#ry except 自定义异常内容
print("Give me two numbers, and I'll divide them.")
print("Enter 'q' to quit.")
while True:
first_number = input("\nFirst number: ")
if first_number == 'q':
break
elif first_number.isdigit():
second_number = input("Second number: ")
if second_number == "q":
break
elif second_number.isdigit():
try:
answer = int(first_number) / int(second_number)
except ZeroDivisionError:
print("You can't divide by 0!")
else:
print(answer)
else:
print("only num")
continue
else:
print("only num")
continue
```
-列表位置
str1 = "123456789"
str1[-1:-10:-1] 与 str[::-1] 结果一样翻转字符串
str[i:j:s]
i缺省默认为0第一个元素,
j缺省默认len(str) ,
s表示递进取值,缺省为1,s<0, 表示倒序取元素,ij 也应小于0,i缺省-1,j缺省-len(str)-1 ,
list1.extend(list2) 在列表末尾追加新列表,
[x for x in list]列表扩展
[x for y in list for x in y] 展开列表中的列表
每次循环文件固定行数
keyword_list=[line.strip() for line in open("keywords.txt",'r')]
f1=open("part_wiki_vec.txt",'r')
f1=open("wiki_vectors.txt")
f2=open("result.txt",'w')
i=0
content=f1.readlines()
while i<1150:
for line in content[300i:300(i+1)]: 每次读300行
line=line.strip().split(' ')
if line[0] in keyword_list:
wordvec=' '.join(line)
print wordvec
f2.write(wordvec)
print line
i+=1
网友评论