在kaggle中,是独立包含内核的,因此我们并不需要格外的编辑器来对我们所编写的语言进行编译.直接在kaggle内核这点击ctrl+回车运行即可。
内核的简单功能介绍。
赋值运算
简单地讲述了变量交换的方法
a,b=b,a
讲解了括号的用法
round(num,ndigits),根据ndigits为正或负将num进行科学计数或者舍弃小数点
示例:
round(14932,-2) #将返回149
round(1.4334,2) #将返回1.43
time库中两个函数的用法,time sleep
time示例:
from time import time
t = time()
print(t) #返回当前时间
sleep示例:
from time import sleep
duration = 5
print("Getting sleepy. See you in", duration, "seconds")
sleep(duration)
print("I'm back. What did I miss?") \
#即刻返回:
Getting sleepy. See you in 5 seconds
#5秒钟后返回:
Getting sleepy. See you in 5 seconds
I'm back. What did I miss?
一个返回为布尔值的函数的实现
示例:
def sign(num):
if num>0:
a=1
elif num<0:
a=-1
else:
a=0
return a
if函数的两种写法
示例:
# 写法1
if total_candies == 1:
print("Splitting 1 candy")
else:
print("Splitting", total_candies, "candies")
***
# 写法2
print("Splitting", total_candies, "candy" if total_candies == 1 else "candies")
any函数的使用
示例:
# 返回的是任何可以被7整除的数组成的列表
def has_lucky_number(nums):
return any([num % 7 == 0 for num in nums])
判断一个字符串是否全为数字以及它的长度
示例:
def is_valid_zip(zip_str):
return len(zip_str) == 5 and zip_str.isdigit()
关于一个将输入的字符串列表中关键词提取的程序
1.关键词仅仅包含其自身
2.不区分大小写,都将结果输出
def word_search(documents, keyword)
indices = []
# enumerate函数用来返回元素下标以及元素本身
for i, doc in enumerate(documents):
# 将字符串根据空格分隔,返回了一个列表
tokens = doc.split()
# rstrip() 删除 string 字符串末尾的指定字符,lower来小写规范字符串(方法可以连续使用)
normalized = [token.rstrip('.,').lower() for token in tokens]
# 将符合的字符串返回到列表中
if keyword.lower() in normalized:
indices.append(i)
return indices
由字典格式返回多个keywords的下标
def multi_word_search(documents, keywords):
keyword_to_indices = {}
for keyword in keywords:
keyword_to_indices[keyword] = word_search(documents, keyword)
return keyword_to_indices
def prettify_graph(graph):
graph.set_title("Results of 500 slot machine pulls")
# Make the y-axis begin at 0
graph.set_ylim(bottom=0)
# Label the y-axis
graph.set_ylabel("Balance")
# Bonus: format the numbers on the y-axis as dollar amounts
# An array of the values displayed on the y-axis (150, 175, 200, etc.)
ticks = graph.get_yticks()
# Format those values into strings beginning with dollar sign
new_labels = ['${}'.format(int(amt)) for amt in ticks]
# Set the new labels
graph.set_yticklabels(new_labels)