我正在尝试通过索引访问dict_key的元素:
test = {'foo': 'bar', 'hello': 'world'}
keys = test.keys() # dict_keys object
keys.index(0)
AttributeError: 'dict_keys' object has no attribute 'index'
Run Code Online (Sandbox Code Playgroud)
我想得到foo.
同样的:
keys[0]
TypeError: 'dict_keys' object does not support indexing
Run Code Online (Sandbox Code Playgroud)
我怎样才能做到这一点?
可能重复:
在Javascript中访问HTTP标头?
我知道用javascript读取当前标题的唯一方法是:
var req = new XMLHttpRequest();
req.open('GET', document.location, false);
req.send(null);
var headers = req.getAllResponseHeaders().toLowerCase();
Run Code Online (Sandbox Code Playgroud)
但我不想提出新的请求,我想读取当前的标题.
这是真的吗?谢谢!
def foo(
hello: str='world', bar: str=None,
another_string_or_None: str|????=None):
pass
Run Code Online (Sandbox Code Playgroud)
我试图在函数中设置Python中的类型提示,你可以添加多个类型提示something: str|bool='default value',但是,提示的类型是None什么?:/
我正在执行以下python代码:
import yaml
foo = {
'name': 'foo',
'my_list': [{'foo': 'test', 'bar': 'test2'}, {'foo': 'test3', 'bar': 'test4'}],
'hello': 'world'
}
print(yaml.dump(foo, default_flow_style=False))
Run Code Online (Sandbox Code Playgroud)
但正在打印:
hello: world
my_list:
- bar: test2
foo: test
- bar: test4
foo: test3
name: foo
Run Code Online (Sandbox Code Playgroud)
代替:
hello: world
my_list:
- bar: test2
foo: test
- bar: test4
foo: test3
name: foo
Run Code Online (Sandbox Code Playgroud)
我怎样才能以my_list这种方式缩进元素?
我正在尝试过滤列表,我想从列表中提取A(是一个列表列表),哪些元素匹配它们的键索引0,另一个列表B具有一系列值
像这样
list_a = list(
list(1, ...),
list(5, ...),
list(8, ...),
list(14, ...)
)
list_b = list(5, 8)
return filter(lambda list_a: list_a[0] in list_b, list_a)
Run Code Online (Sandbox Code Playgroud)
应该返回:
list(
list(5, ...),
list(8, ...)
)
Run Code Online (Sandbox Code Playgroud)
我怎样才能做到这一点?谢谢!
我有以下字符串:
"hello.world.foo.bar"
我想拆分(使用"."as分隔符,只想从头到尾获得两个元素),如下所示:
["hello.world.foo", "bar"]
我怎么能做到这一点?到底存在限制?
我有一个网址: http://xxx.abcdef.com/fdfdf/
我想得到 xxx.abcdef.com
我可以使用哪个模块来完成此任务?
我想在python2和python3上使用相同的模块和方法
我不喜欢尝试除了python2/3兼容性的方式
非常感谢!
我正在尝试将int编码为base64,我这样做:
foo = 1
base64.b64encode(bytes(foo))
Run Code Online (Sandbox Code Playgroud)
预期产量: 'MQ=='
给定输出: b'AA=='
我做错了什么?
编辑:在Python 2.7.2中正常工作
我正在使用pycharm执行以下操作:
print(os.environ["PATH"]) # returns '/usr/bin:/bin:/usr/sbin:/sbin'
Run Code Online (Sandbox Code Playgroud)
但是当我在shell中执行echo $ PATH时,会返回:
/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/bin:/bin:/usr/sbin:/sbin:/opt/local/bin:/opt/local/sbin
Run Code Online (Sandbox Code Playgroud)
我尝试在Preferences> Console> Python Console> Environment Variables中进行编辑
PATH=/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/bin:/bin:/usr/sbin:/sbin:/opt/local/bin:/opt/local/sbin
但这不起作用
任何的想法?
我需要列出当前目录(.)中的所有文件(包括所有子目录),并排除一些文件,如.gitignore如何工作(http://git-scm.com/docs/gitignore)
使用fnmatch(https://docs.python.org/2/library/fnmatch.html),我将能够使用模式"过滤"文件
ignore_files = ['*.jpg', 'foo/', 'bar/hello*']
matches = []
for root, dirnames, filenames in os.walk('.'):
for filename in fnmatch.filter(filenames, '*'):
matches.append(os.path.join(root, filename))
Run Code Online (Sandbox Code Playgroud)
如何"过滤"并获取与"ignore_files"的一个或多个元素不匹配的所有文件?
谢谢!
python ×9
python-3.x ×3
string ×2
base64 ×1
dictionary ×1
encode ×1
filter ×1
fnmatch ×1
gitignore ×1
glob ×1
http-headers ×1
indentation ×1
int ×1
interpreter ×1
javascript ×1
key ×1
list ×1
nonetype ×1
parsing ×1
pycharm ×1
python-2.x ×1
split ×1
type-hinting ×1
yaml ×1