我注意到在python中有两种类似的查找方法来查找数字的绝对值:
第一
abs(-5)
Run Code Online (Sandbox Code Playgroud)
第二
import math
math.fabs(-5)
Run Code Online (Sandbox Code Playgroud)
这些方法有何不同?
默认情况下,Python 3对源代码文件使用UTF-8编码.我还应该在每个源文件的开头使用编码声明吗?喜欢# -*- coding: utf-8 -*-
我有一个非常简单的列表:
example_list = [
{'points': 400, 'gold': 2480},
{'points': 100, 'gold': 610},
{'points': 100, 'gold': 620},
{'points': 100, 'gold': 620}
]
Run Code Online (Sandbox Code Playgroud)
我如何总结所有黄金价值?我正在寻找漂亮的oneliner.
现在我正在使用此代码(但它不是最佳解决方案):
total_gold = 0
for item in example_list:
total_gold += example_list["gold"]
Run Code Online (Sandbox Code Playgroud) 我正在尝试使用for循环修改列表中的项目,但是我收到错误(参见下文).示例代码:
#!/usr/bin/env python
# *-* coding: utf8 *-*
data = []
data.append("some")
data.append("example")
data.append("data")
data.append("here")
for item in data:
data[item] = "everything"
Run Code Online (Sandbox Code Playgroud)
错误:
Traceback (most recent call last):
File "./testy.py", line 11, in <module>
data[item] = "everything"
TypeError: list indices must be integers, not str
Run Code Online (Sandbox Code Playgroud)
有什么方法可以解决这个问题吗?
我写了一个改变壁纸的bash脚本(对于GNOME3).
#!/bin/bash
# Wallpaper's directory.
dir="${HOME}/images/wallpapers/"
# Random wallpaper.
wallpaper=`find "${dir}" -type f | shuf -n1`
# Change wallpaper.
# http://bit.ly/HYEU9H
gsettings set org.gnome.desktop.background picture-options "spanned"
gsettings set org.gnome.desktop.background picture-uri "file://${wallpaper}"
Run Code Online (Sandbox Code Playgroud)
在终端仿真器(例如gnome-terminal)中执行的脚本效果很好.在执行cron或ttyX终端时出错:
** (process:26717): WARNING **: Command line `dbus-launch --autolaunch=d64a757758b286540cc0858400000603 --binary-syntax --close-stderr' exited with non-zero exit status 1: Autolaunch error: X11 initialization failed.\n
** (process:26717): WARNING **: Command line `dbus-launch --autolaunch=d64a757758b286540cc0858400000603 --binary-syntax --close-stderr' exited with non-zero exit status 1: Autolaunch error: X11 initialization failed.\n
** (process:26721): WARNING **: Command line …Run Code Online (Sandbox Code Playgroud) 我为urllib(python3)写了一个小包装器.如果导入模块是否合适且安全?
if self.response_encoding == 'gzip':
import gzip
Run Code Online (Sandbox Code Playgroud)
我没有找到关于此代码的任何PEP.然而,它困扰我.
我正在使用React和TypeScript,我已经创建了无状态函数.为了便于阅读,我从示例中删除了无用的代码.
interface CenterBoxProps extends React.Props<CenterBoxProps> {
minHeight?: number;
}
export const CenterBox = (props: CenterBoxProps) => {
const minHeight = props.minHeight || 250;
const style = {
minHeight: minHeight
};
return <div style={style}>Example div</div>;
};
Run Code Online (Sandbox Code Playgroud)
一切都很棒,这段代码工作正常.但有我的问题:我怎么可以定义defaultProps为CenterBox组件?
正如反应文档中提到的那样:
(...)它们是输入的纯函数变换,没有样板.但是,您仍然可以通过将它们设置为函数的属性来指定.propTypes和 .defaultProps,就像在ES6类上设置它们一样.(......)
它应该很容易:
CenterBox.defaultProps = {
minHeight: 250
}
Run Code Online (Sandbox Code Playgroud)
但是此代码生成TSLint错误: error TS2339: Property 'defaultProps' does not exist on type '(props: CenterBoxProps) => Element'.
再说一遍:我怎样才能defaultProps在上面的堆栈中正确定义(React + TypeScript)?
我有很多这样的表行:
<tr>
<td>100</td>
<td>200</td>
<td><input type="radio" value="123599"></td>
</tr>
Run Code Online (Sandbox Code Playgroud)
迭代:
table = BeautifulSoup(response).find(id="sometable") # Make soup.
for row in table.find_all("tr")[1:]: # Find rows.
cells = row.find_all("td") # Find cells.
points = int(cells[0].get_text())
gold = int(cells[1].get_text())
id = cells[2].input['value']
print id
Run Code Online (Sandbox Code Playgroud)
错误:
File "./script.py", line XX, in <module>
id = cells[2].input['value']
TypeError: 'NoneType' object has no attribute '__getitem__'
Run Code Online (Sandbox Code Playgroud)
我怎样才能获得输入值?我不想使用正则表达式.
在PyCharm中,目录可以标记为:
我想知道资源和资源有什么不同.
我在文档中找不到关于我的问题的任何信息.有没有定义?
我正在我的 React 应用程序中准备微调器。
它工作得很好。但是,一些 UX 提示说,应该在等待一段时间后显示 spinner/loader/etc。对于这个例子,假设它应该是 750 毫秒。
如何节流/去抖动(我仍然不确定有什么区别)重新渲染组件?
在上面的例子中,加载状态不应该在任何时候出现。
python ×6
reactjs ×2
bash ×1
cron ×1
debouncing ×1
dictionary ×1
encoding ×1
gnome ×1
if-statement ×1
items ×1
javascript ×1
linux ×1
list ×1
loops ×1
pycharm ×1
python-3.x ×1
throttling ×1
typescript ×1
utf-8 ×1