我在Jupyter笔记本上运行代码,我修改了这个链接的代码,所以它从Jupyter笔记本而不是控制台获取它,并迭代一个文件列表.
"""Demonstrates how to make a simple call to the Natural Language API."""
import argparse
import requests
from google.cloud import language
from google.cloud.language import enums
from google.cloud.language import types
def print_result(annotations, movie_review_filename):
score = annotations.document_sentiment.score
magnitude = annotations.document_sentiment.magnitude
file_path_split = movie_review_filename.split("/")
fileName = file_path_split[len(file_path_split) - 1][:-4]
sentencelist = []
statuslist = []
for index, sentence in enumerate(annotations.sentences):
sentence_sentiment = sentence.sentiment.score
singlesentence = [fileName, sentence.text.content, sentence.sentiment.magnitude, sentence_sentiment]
sentencelist.append(singlesentence)
outputdf = pd.DataFrame(sentencelist, columns = ['status_id', 'sentence', 'sentence_magnitude', 'sentence_sentiment'])
outputdf.to_csv("/Users/abhi/Desktop/RetrySentenceCSVs/" + …Run Code Online (Sandbox Code Playgroud) 假设我有4个小型DataFrame
df1,df2,df3和df4
import pandas as pd
from functools import reduce
import numpy as np
df1 = pd.DataFrame([['a', 1, 10], ['a', 2, 20], ['b', 1, 4], ['c', 1, 2], ['e', 2, 10]])
df2 = pd.DataFrame([['a', 1, 15], ['a', 2, 20], ['c', 1, 2]])
df3 = pd.DataFrame([['d', 1, 10], ['e', 2, 20], ['f', 1, 1]])
df4 = pd.DataFrame([['d', 1, 10], ['e', 2, 20], ['f', 1, 15]])
df1.columns = ['name', 'id', 'price']
df2.columns = ['name', 'id', 'price'] …Run Code Online (Sandbox Code Playgroud) 当我遇到在对象中查找的答案之一range是 CONSTANT TIME 操作时,我在 StackOverflow 上滚动浏览了许多与列表与范围相关的问题!这在 O(1) 中如何完成是没有意义的,您将不得不遍历范围以查看是否存在数字。范围对象是否像一些哈希图/字典一样工作?
def usingRange(x):
return x in range(0, 100000000)
print(usingRange(9999999))
def noRange(x):
return x in list(range(0, 100000000))
print(noRange(9999999))
%timeit usingRange(9999999)
%timeit noRange(9999999)
Run Code Online (Sandbox Code Playgroud)
我得到的输出为:
True
True
443 ns ± 8.83 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)
6.89 s ± 380 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)
Run Code Online (Sandbox Code Playgroud)
我想知道为什么它的时间恒定的原因是因为
有人告诉我不要太专注于学习 Python 编程面试中的“技巧”!因为你所做的每一件事,你都必须解释时间复杂度,所以你要么记住i in list1O(N),要么使用 for 循环遍历列表:
for …Run Code Online (Sandbox Code Playgroud)我有一个有效的递归解决方案,但事实证明很多子问题正在重新计算。我需要记忆方面的帮助。
所以这是问题陈述:
你是一名职业强盗,计划抢劫街道上的房屋。每栋房子都藏有一定数量的钱,唯一阻止你抢劫的限制是相邻的房子都连接了安全系统,如果相邻的两栋房子在同一天晚上被闯入,它会自动联系警方。
给定一个代表每栋房子的金额的非负整数列表,确定今晚您可以在不通知警察的情况下抢劫的最大金额。
例子:
输入:
[2,7,9,3,1]输出:12解释: 抢劫房屋 1(金钱 = 2)、抢劫房屋 3(金钱 = 9)和抢劫房屋 5(金钱 = 1)。您可以抢劫的总金额 =2 + 9 + 1 = 12.
另一个:
输入:
[1,2,3,1]输出:4解释:抢夺 1 号房屋(金钱 = 1),然后抢夺 3 号房屋(金钱 = 3)。您可以抢劫的总金额 =1 + 3 = 4.
还有另外一个
输入:
[2, 1, 1, 2]输出:4解释:抢夺 1 号房屋(金钱 = 2),然后抢夺 4 号房屋(金钱 = 2)。您可以抢劫的总金额 =2 + 2 = 4.
现在就像我说的,我有一个完美工作的递归解决方案:当我构建递归解决方案时。我没有想太多。我只是试图理解较小的子问题是什么。
option_1:我将值添加到 current 中index …
可重现的例子:
ex = [{"explode1": ["a", "e", "i"], "word": "US_12", "explode2": []},
{"explode1": [], "word": "US_34", "explode2": ["a", "e", "i"]},
{"explode1": ["a", "e", "i"], "word": "US_56", "explode2": ["o", "u"]}]
df = pd.DataFrame(ex)
Run Code Online (Sandbox Code Playgroud)
给你
explode1 word explode2
0 [a, e, i] US_12 []
1 [] US_34 [a, e, i]
2 [a, e, i] US_56 [o, u]
Run Code Online (Sandbox Code Playgroud)
您可以假设还有一个explode3和 一个explode4列(为了简洁而排除)
预期结果数据框:
exploded_alphabet word exploded_type
0 a US_12 explode1
1 e US_12 explode1
2 i US_12 explode1
3 a …Run Code Online (Sandbox Code Playgroud) 这是一个示例数据帧:
df = pd.DataFrame([[1, 1, 10, 11, 12],
[1, 1, 13, 14, 15],
[1, 2, 16, 17, 18],
[1, 2, 19, 20, 21],
[1, 3, 22, 23, 24],
[1, 3, 25, 26, 27],
[1, 4, 28, 29, 30],
[1, 4, 31, 32, 33],
[1, 4, 34, 35, 36],
[1, 4, 37, 38, 39],
[1, 4, 40, 41, 42]])
df.columns = ['c1', 'c2', 'p1', 'p2', 'p3']
print(df)
Run Code Online (Sandbox Code Playgroud)
给出:
c1 c2 p1 p2 p3
0 1 1 10 11 12
1 1 …Run Code Online (Sandbox Code Playgroud) 我正在执行一个 Sparql 查询,该查询返回关键字apple不属于特定子类的所有 URISpecies
select distinct ?s
where
{
?s a owl:Thing . ?s rdfs:label ?label .
filter(langmatches(lang(?label), 'en')) ?label bif:contains '"apple"' .
filter not exists {?s rdf:type/rdfs:subClassOf* dbo:Species }
}
Run Code Online (Sandbox Code Playgroud)
我想包含更多子类。我想包含许多子类,所以我想像这样过滤掉:
filter not exists {?s rdf:type/rdfs:subClassOf* dbo:Species和filter not exists {?s rdf:type/rdfs:subClassOf* dbo:Organisation和filter not exists {?s rdf:type/rdfs:subClassOf* dbo:SomeOtherSubclass
如何将多个 AND 链接在一起?
class Solution:
def addNums(self, a, b):
return a + b
test1 = Solution()
test1.addNums(5, 6)
Run Code Online (Sandbox Code Playgroud)
以上是我的课!用简单的添加方法。
本质上,我想做的是,为算法和数据结构/编程面试做准备,我为每个输入创建实例,并希望为实例编写单元测试。
这是我在下面尝试过的:
import unittest
class TestSolution(unittest.TestCase):
def test_addNums(self):
example = Solution()
self.assertEqual(example.addNums(9, 10), 19)
if __name__ == '__main__':
unittest.main()
Run Code Online (Sandbox Code Playgroud)
不知道如何执行此操作,如果我运行上面的代码,我会收到以下错误消息:
----------------------------------------------------------------------
AttributeError: module '__main__' has no attribute '/Users/abhishekbabuji/Library/Jupyter/runtime/kernel-eb5f1d39-4880-49a7-9355-bbddc95464ff'
----------------------------------------------------------------------
Ran 1 test in 0.001s
FAILED (errors=1)
An exception has occurred, use %tb to see the full traceback.
SystemExit: True
Run Code Online (Sandbox Code Playgroud)
我希望能够测试类的实例方法的返回值Solution,在本例中addNums(self, a, b)
问题可能比我听起来更简单.听说2个值真值表有3列,共有8个值吗?TTT, TTF...2个值真值表,2列有4个值?TT, TF, FF, FT
在我的问题:
共有5列.
A, B, C, D, E
列A只能1取值:a
列B可以2取值: x和y
列C可以3取值1,2和3(值可以是字符串或整数,数据类型无关紧要.
列D可以采取2的值t和f
最终
列E可以采取3的值alpha,并omega与beta
所以总共会有36种组合!(1 X 2 X 3 X 2 X 3)=36
到目前为止我做了什么?
我想出了这个令人厌恶的东西.. 我想要的输出是通过以下运行产生的:
col1 = ['a']
col2 = ['x', …Run Code Online (Sandbox Code Playgroud) python ×7
python-3.x ×7
pandas ×3
dataframe ×2
combinations ×1
dbpedia ×1
list ×1
lookup ×1
memoization ×1
merge ×1
numpy ×1
oop ×1
recursion ×1
sparql ×1
unit-testing ×1
xrange ×1