循环中的list.append()只能返回最终列表

时间:2019-02-21 23:50:06

标签: python list loops append

例如:

a = {}; 
b = [];
for i in range(20):
    b.append(i)
    a[i] = b

循环后,a的所有键都链接到相同的最终列表b [0,1,2,...,19]。 但是,我想要的是a[0] = [0]a[1] = [0,1]a[2] = [0,1,2]。 我不知道这是怎么了。

1 个答案:

答案 0 :(得分:0)

// Exclude products in recently viewed products widget from "machine" product category
add_filter( 'woocommerce_recently_viewed_products_widget_query_args', 'custom_recently_viewed_products_widget_query_args', 10, 1 );
function custom_recently_viewed_products_widget_query_args( $args ) {

    $args['tax_query'][] = array(
           'taxonomy' => 'product_cat',
           'field'    => 'slug',
           'terms'    => array( 'machine' ), 
           'operator' => 'NOT IN', 
    );

    return $args;
}

希望有帮助