我有一份清单清单:
a = [[1, 3, 4], [2, 5, 7]]
Run Code Online (Sandbox Code Playgroud)
我希望输出格式如下:
1 3 4
2 5 7
Run Code Online (Sandbox Code Playgroud)
我已经尝试过以下方式,但输出不是所需的方式:
for i in a:
for j in i:
print(j, sep=' ')
Run Code Online (Sandbox Code Playgroud)
输出:
1
3
4
2
5
7
Run Code Online (Sandbox Code Playgroud)
在更改打印调用时使用end:
for i in a:
for j in i:
print(j, end = ' ')
Run Code Online (Sandbox Code Playgroud)
输出:
1 3 4 2 5 7
Run Code Online (Sandbox Code Playgroud)
有任何想法吗?
我在Python中发现了关于迭代器行为的这个问题:
当我输入代码时:
a = iter(list(range(10)))
for i in a:
print a
next(a)
Run Code Online (Sandbox Code Playgroud)
进入jupyter-qtconsole它返回:
0
2
4
6
8
Run Code Online (Sandbox Code Playgroud)
就像Martijn Pieters所说的那样,当翻译不响应号召时next(a).
但是,当我在我的Bash解释器和IDLE中再次运行相同的代码时,代码打印出来:
0
1
2
3
4
5
6
7
8
9
Run Code Online (Sandbox Code Playgroud)
到控制台.
我跑了代码:
import platform
platform.python_implementation()
Run Code Online (Sandbox Code Playgroud)
在所有三个环境中,他们都说我跑了'CPython'.
那么为什么QtConsole next(a)在IDLE和Bash没有时会抑制调用呢?
如果它有帮助,我在Mac OSX上运行Python 2.7.9并使用Anaconda发行版.
具有多个参数和类型提示的方法的悬挂缩进的正确语法是什么?
在第一个参数下对齐
def get_library_book(self,
book_id: str,
library_id: str
)-> Book:
Run Code Online (Sandbox Code Playgroud)
缩进一层
def get_library_book(
self,
book_id: str,
library_id: str
) -> Book:
Run Code Online (Sandbox Code Playgroud)
PEP8支持凹槽下面的一个级别,但不指定是否允许在第一个参数下进行对齐.它指出:
使用悬挂式凹痕时,应考虑以下因素; 第一行应该没有参数,应该使用进一步的缩进来明确区分自己作为延续线.
我正在查看第三方API,他们有以下代码:
def array_u16 (n): return array('H', '\0\0'*n)
Run Code Online (Sandbox Code Playgroud)
我明白这'\0'意味着NULL,确实'\0\0'有任何特殊意义,或仅仅意味着2 NULL秒?
我需要它匹配的连续串正则表达式a和b,例如:
ababa
bab
Run Code Online (Sandbox Code Playgroud)
边缘情况(最小):
ab
ba
Run Code Online (Sandbox Code Playgroud)
(没有上限.)
......并且不应该匹配:
abba
bbab
bbaabb
Run Code Online (Sandbox Code Playgroud)
我试过几个正则表达式,但这个有点棘手.你能告诉我任何提示吗?
我的尝试:
(a|b)+(ab|ba)*(aba|bab)+这一个非常接近!http://www.regexr.com/38lqg
我正在使用 SQLAlchemy 并尝试将事件添加到我的代码中。虽然一切正常,但不会触发事件。这是我的简化代码:
from sqlalchemy import Column, String
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
from sqlalchemy import event
alchemy_base = declarative_base()
class Person(alchemy_base):
__tablename__ = 'my_table'
name = Column(String, primary_key=True)
@event.listens_for(Person, "before_insert")
def before_insert(mapper, connection, instance):
print("before_insert", mapper, connection, instance)
engine = create_engine('sqlite:///:memory:', echo=True)
alchemy_base.metadata.create_all(engine)
session_maker = sessionmaker(bind=engine)
session = session_maker()
insert_dicts = [{'name': "ami"}, {'name': "beni"}]
engine.execute(Person.__table__.insert(), insert_dicts)
session.commit()
Run Code Online (Sandbox Code Playgroud)
before_insert尽管值已输入到 DB 中,但并未调用该函数。
我在 Mac OS 上使用 python 2.7.10,SQLAlchemy 1.0.10。
在Java或C中,我们将<condition> ? X : Y其转换为Python X if <condition> else Y.
但也有这个小技巧:<condition> and X or Y.
虽然我明白,这是相当于前述三元运营商,我觉得很难把握如何and和or运营商能够产生正确的结果.这背后的逻辑是什么?
我需要使用 Python 3 解码一个按以下方式编码的字符串:
>>> s = numpy.asarray(numpy.string_("hello\nworld"))
>>> s
array(b'hello\nworld',
dtype='|S11')
Run Code Online (Sandbox Code Playgroud)
我试过:
>>> str(s)
"b'hello\\nworld'"
>>> s.decode()
AttributeError Traceback (most recent call last)
<ipython-input-31-7f8dd6e0676b> in <module>()
----> 1 s.decode()
AttributeError: 'numpy.ndarray' object has no attribute 'decode'
>>> s[0].decode()
---------------------------------------------------------------------------
IndexError Traceback (most recent call last)
<ipython-input-34-fae1dad6938f> in <module>()
----> 1 s[0].decode()
IndexError: 0-d arrays can't be indexed
Run Code Online (Sandbox Code Playgroud) 我在C++中寻找一些decltype类似物.我想要完成的是以下内容:
def f(a: int) -> List[Tuple(int, float)]
def g(a: List[int]) -> List[decltype(f)]
Run Code Online (Sandbox Code Playgroud)
所以想法是使用另一个函数的类型注释.我找到的解决方案看起来有些笨拙:
def g(a: List[int])->f.__annotations__['return']
Run Code Online (Sandbox Code Playgroud)
基本上,问题是是否存在类似decltype的东西(可能它应该被称为"return_type")或是否在其他版本中计划.我还编写了一个小函数来说明可能使用此功能:
def return_type(f: Callable):
try:
return get_type_hints(f)['return']
except(KeyError, AttributeError):
return Any
def g() -> return_type(f):
Run Code Online (Sandbox Code Playgroud)
UPD正如Jim Fasarakis-Hilliard所建议的,我们也可以使用get_type_hints而不是注释
在 Python 中,异步生成器函数是协程,生成器函数也是协程。
生成器函数和异步生成器函数的用途有什么区别?
谢谢。
python ×9
python-3.x ×6
python-2.7 ×2
string ×2
type-hinting ×2
asynchronous ×1
coroutine ×1
interpreter ×1
jupyter ×1
list ×1
nested-lists ×1
numpy ×1
pep8 ×1
printing ×1
python-3.6 ×1
regex ×1
sqlalchemy ×1
syntax ×1
types ×1