我有以下代码行:
records = []
for future in futures:
records.extends(future.result())
每个未来都会返回一个列表。
如何编写上述代码,但是在一个班轮中?
records = [future.result() for future in futures]
会导致列表中的列表。
我有数百万条记录,在列表
中创建列表之后我宁愿不做它答案 0 :(得分:6)
body {
margin: 0;
}
.msg-ex {
display: flex;
align-items: center;
justify-content: center;
background-position: center;
background-size: cover;
background-repeat: no-repeat;
}
.msg-ex .msg-ex-overlay {
position: relative;
background-color: rgba(0, 0, 0, 0.4);
width: 100vw;
height: 100vh;
opacity: 0;
z-index: 2;
}
.msg-ex .msg-ex-overlay .msg-ex-cta {
position: absolute;
color: red;
background-color: #DCE0E3;
font-size: 1.1em;
font-weight: 700;
border-radius: 18px;
height: 38px;
width: 100px;
cursor: pointer;
top: 50%;
left: 50%;
transform: translateX(-48%) translateY(-47%);
}
.msg-ex .msg-ex-overlay:hover {
opacity: 1;
}
.msg-ex-box {
position: relative;
background-color: $voiceColor;
}
.msg-ex-box::after {
content: '';
bottom: 0;
right: 0;
width: 32px;
height: 32px;
background-image: url(../pics/data/img1.png);
background-position: center;
background-repeat: no-repeat;
background-size: cover;
position: absolute;
z-index: 1;
}
}
.msg-ex-voice:hover {
background-color: rgba(0, 0, 0, 0.4);
}
答案 1 :(得分:2)
有很多方法可以做到这一点:
itertools.chain
:records = list(itertools.chain.from_iterable(future.result() for future in futures))
itertools
消费食谱:records = collections.deque((records.extend(future.result()) for future in futures), maxlen=0)
[records.extend(future.result()) for future in futures]
。 records
现在将拥有所有必需的内容,您将暂时列出None
s 您也可以functools.reduce(operator.add, (future.result() for future in futures))
,但这不会很好地扩展
答案 2 :(得分:1)
我认为这就是你要找的东西
import functools
records = functools.reduce(lambda res, future: res + future.result()), futures, [])