如何破坏子列表,使子列表中的每个元素成为python列表中的普通元素

时间:2016-11-13 07:35:55

标签: python list python-3.x

例如我有

[[1,2,3],[1,2,3],3,5,6]

我想把它变成

[1,2,3,1,2,3,3,5,6]

python 3中的代码是什么样的?

1 个答案:

答案 0 :(得分:0)

迭代$downloads_model = Printingmachinedownloads::find()->where(['productid'=>$product_id])->orderBy(['type'=>SORT_DESC])->all(); $previous_title = ''; $iteration = 1; foreach($downloads_model as $doc): $subtitles = Printingmachinedownloads::find()->where(['productid'=>$product_id, 'type' => $doc['type'] )->all(); $count = count($subtitles); $previous_title = $previous_title ?: $doc['type']; if( $previous_title == $doc['type'] && $count == $iteration ): <div class="amazingcarousel-container-1 tab_style"> <h3><?= $previous_title ?></h3> foreach($subtitles as $subtitle): <a target = '_blank' href="<?php echo Yii::$app->homeUrl?>images/printingmachine/downloads/<?php echo $subtitle['documentname']; ?>"> <?php echo $subtitle['documentlabel'] ?> </a> enforeach; </div> $previous_title = ''; $iteration = 1; else: $iteration++; endif; endforeach; 个项目,如果项目是列表则迭代子项目;其他只是附加到新列表:

a

使用list comprehension(嵌套a = [[1,2,3],[1,2,3],3,5,6] b = [] for x in a: if isinstance(x, list): b.extend(x) else: b.append(x) # b == [1,2,3,1,2,3,3,5,6] )与conditional expression相结合的另一种方法:

for