这是一个Python脚本的示例:
n, dollar, euro = map(input().split())
if n == 1:
print(dolar / euro) # Note the typo, 'dolar' vs. 'dollar'
else:
print(euro / dollar)
Run Code Online (Sandbox Code Playgroud)
我犯了一个错误("dolar"与一个'l').error(NameError)
除非您输入,否则口译员不会通知甚至是警告n = 1
.
如何在运行时通知它?
尝试对字典中的值求和时,出现以下错误.我希望得到总和(即15),但会引发错误.
这是一个错误吗?
IPython QtConsole 3.1.0 Python 2.7.10 | Continuum Analytics,Inc.| (默认,2015年5月28日,17:04:42)
d = {'1': 1, '2': 2 , '3': 3, '4': 4, '5': 5}
>>> sum(d.values())
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-36-4babd535f17a> in <module>()
----> 1 sum(d.values())
TypeError: 'int' object is not callable
Run Code Online (Sandbox Code Playgroud) 我有两个数据框:
df1
A1 B1
1 a
2 s
3 d
Run Code Online (Sandbox Code Playgroud)
和
df2
A1 B1
1 a
2 x
3 d
Run Code Online (Sandbox Code Playgroud)
我想比较B1列上的df1和df2.列A1可用于连接.我想知道:
我尝试使用合并和连接,但这不是我想要的.
LabelEncoder
不会“记住”参数。当我使用它拟合并转换数据然后询问参数时,我得到的只是{}
。这使得不可能在新数据上重复使用编码器。
例:
from sklearn.preprocessing import LabelEncoder
encode = LabelEncoder()
encode.fit_transform(['one', 'two', 'three'])
print(encode.get_params())
Run Code Online (Sandbox Code Playgroud)
不确定预期的格式,但是我期望类似 {['one', 0], ['two', 1], ['three', 2]}
实际结果: {}
我上线了:
Darwin-16.7.0-x86_64-i386-64bit
Python 3.6.1 |Anaconda 4.4.0 (x86_64)| (default, May 11 2017, 13:04:09)
[GCC 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.57)]
NumPy 1.12.1
SciPy 0.19.0
Scikit-Learn 0.18.1
Run Code Online (Sandbox Code Playgroud) 我有一个值如下所示的数据表:
Score ID
1.2 1
2.4 1
1.1 1
1.9 1
2.4 2
3.5 2
2.2 2
1.1 3
1.1 3
1.7 3
3.1 3
2.2 3
Run Code Online (Sandbox Code Playgroud)
我真正需要的是在单独的列中计算每个ID的更改分数,使其看起来像这样:
Score ID Changes
1.2 1
2.4 1 1.2
1.1 1 -1.3
1.9 1 0.8
2.4 2
3.5 2 1.1
2.2 2 -1.3
1.1 3
1.1 3 0
1.7 3 0.6
3.1 3 1.4
2.2 3 -0.9
Run Code Online (Sandbox Code Playgroud)
注意每个"变化"是如何通过取得前一个分数并从当前分数中减去来计算的:例如,在第一个变化中,你得到1.2乘1.2 = 2.4(当前分数) - 1.2(前一个分数)
问题是这些分数只能仅与ID范围内的分数有关.你不能只是迭代并取得分数的差异.我该怎么做?我可以使用什么逻辑来测试在同一ID中包含"更改"?我熟悉R或python(或BASH)来做到这一点.
我需要得到一个包含字符串中每两个相邻的字符的列表hello
,使得
['he', 'el', 'll', 'lo']
Run Code Online (Sandbox Code Playgroud)
我以为我可以这样做
>>>import re
>>>re.findall(r'..', 'hello')
['he', 'll']
Run Code Online (Sandbox Code Playgroud)
这不是我想要的.我需要使用正则表达式获取上面提到的列表
我在以下模式中有一系列字符串:
string = 'ABCD 1NAME 123456'
.我需要提取最后的数字才能生成ID.我试图使用该isdigit
方法,但问题是它还返回名称前的数字.
注意事项:
len
最后一位数字的范围是5到9.有人可以建议我另类吗?我认为我需要测试前一个位置或下一个位置是否为数字才能提取id,但我无法弄清楚如何实现此测试.
Sentence = input("Type a sentence")
Loop = True
While loop = True:
If # something to detect punctuati#:
Loop = True
Else:
Loop = False
Run Code Online (Sandbox Code Playgroud)