如何将值输出到一个列表而不是多个列表?

时间:2017-11-26 13:46:56

标签: python

我很难解决我的问题。我做了这个:

while

问题是,我得到了一大堆列表,但我只想要一个。

例如,这是我得到的输出:

def thereyougo():
    """
    The function thereyougo() should return a list with all the numbers from
    1200 upto and including 2399 that are divisible by 12.
    """

    i = range(1200, 2400)
    z = []

    for k in i:
        if k % 12 == 0:
            z.append(k)
            print z

thereyougo()

虽然我想要[1200] [1200, 1212] [1200, 1212, 1224] [1200, 1212, 1224, 1236]

我做错了什么?

1 个答案:

答案 0 :(得分:2)

print只需for k in i: if k % 12 == 0: z.append(k) print z

step

请注意,您可以使用range()生成器的12th参数更有效地执行此操作。这样您就可以获得每个multiple数字,根据定义,每个def thereyougo(): print list(range(1200, 2400, 12)) 数字都会{。}}。

使用上述内容,您可以将功能降低到:

[1200, 1212, 1224, 1236, 1248, 1260, 1272, 1284, 1296, 1308, 1320, 1332, 1344, 1356, 1368, 1380, 1392, 1404, 1416, 1428, 1440, 1452, 1464, 1476, 1488, 1500, 1512, 1524, 1536, 1548, 1560, 1572, 1584, 1596, 1608, 1620, 1632, 1644, 1656, 1668, 1680, 1692, 1704, 1716, 1728, 1740, 1752, 1764, 1776, 1788, 1800, 1812, 1824, 1836, 1848, 1860, 1872, 1884, 1896, 1908, 1920, 1932, 1944, 1956, 1968, 1980, 1992, 2004, 2016, 2028, 2040, 2052, 2064, 2076, 2088, 2100, 2112, 2124, 2136, 2148, 2160, 2172, 2184, 2196, 2208, 2220, 2232, 2244, 2256, 2268, 2280, 2292, 2304, 2316, 2328, 2340, 2352, 2364, 2376, 2388]

,当被叫时,会给出:

import { Component, OnInit } from '@angular/core';
import { FormGroup, FormBuilder, Validators } from '@angular/forms';
import { Customer } from './customer';

@Component({
   selector: 'my-signup',
   templateUrl: './app/customers/customer.component.html'
})
export class CustomerComponent implements OnInit {
   customerForm: FormGroup;
   customer: Customer= new Customer();

   constructor(private fb: FormBuilder){}

   ngOnInit(): void {
      this.customerForm = this.fb.group({
          firstName: ['', [Validators.required, Validators.minLength(3)]],
          //lastName: {value:'', disabled:true},
          lastname: ['', [Validators.required, Validators.minLength(50)]],
          email: ['', [Validators.required, Validators.pattern('[a-z0-9._%+-]+@[a-z0-9.-]+')]],
          phone: '',
        notification: 'email',
        sendCatalog: true
    });
}


populateTestData(): void {
    this.customerForm.patchValue({
        firstName: 'Jack',
        lastName: 'Harkness',
        //email: 'jack@torchwood.com',
        sendCatalog: false
    });
}

save() {
    console.log(this.customerForm);
    console.log('Saved: ' + JSON.stringify(this.customerForm.value));
}

setNotification(notifyVia: string): void{
    const phoneControl = this.customerForm.get('phone');
    if (notifyVia === 'text'){
        phoneControl.setValidators(Validators.required);
    } else {
        phoneControl.clearValidators();
    }
    phoneControl.updateValueAndValidity();
}
}