Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/341.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jsf-2/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在python中将提取的列表项转换为列表_Python_List - Fatal编程技术网

在python中将提取的列表项转换为列表

在python中将提取的列表项转换为列表,python,list,Python,List,此代码返回 import numpy x = [*range(1, 101)] for n in x: if numpy.mod(n, 2) == 0: print(f'Even numbers between 0 and 101 are: {n}') 等等。但我希望它以列表形式返回偶数 Even numbers between 0 and 101 are: 2 Even numbers between 0 and 101 are: 4 Even numbers be

此代码返回

import numpy
x = [*range(1, 101)]
for n in x:
    if numpy.mod(n, 2) == 0:
        print(f'Even numbers between 0 and 101 are: {n}') 
等等。但我希望它以列表形式返回偶数

Even numbers between 0 and 101 are: 2
Even numbers between 0 and 101 are: 4
Even numbers between 0 and 101 are: 6
我尝试使用列表(n),但它显示

Even numbers between 0 and 101 are: [2, 4, 6, 8, 10, 12, ..., 100]
回溯(最近一次呼叫最后一次):
文件“”,第3行,在
打印('0和101之间的偶数为:',列表(n))
TypeError:“int”对象不可编辑

请建议一种替代方法。

您应该将
n
附加到列表中。 请查看:

试试看

此外,您不必将范围存储在变量
x
中。尝试使用下面的列表理解

import numpy
x = [*range(1, 101)]
output = list()
for n in x:
    if numpy.mod(n, 2) == 0:
        output.append(n)

print(f'Even numbers between 0 and 101 are: {output}') 


或者使用
%
运算符作为列表理解:

import numpy
output = [n for n in range(1,101) if numpy.mod(n, 2) == 0]

print(f'Even numbers between 0 and 101 are: {output}')

我将首先声明一个空列表,向其添加项目,最后显示列表:

output = [x for x in range(1, 101) if x % 2 == 0]
print(f'Even numbers between 0 and 101 are: {output}')
是以下内容的长版本:

en = []
import numpy as np
x = [*range(1, 101)]
for n in x:
    if np.mod(n, 2) == 0:
        en.append(n)

print(f'Even numbers between 0 and 101 are: {en}') 

实际上,numpy是usless,python只有模数运算符
%

您应该将偶数附加到
中的列表中,以便执行
循环。这可以使用
list.append()
完成:


当我把print(0到101之间的偶数是:{output}')放在output.append(n)下面时,发生了一些奇怪的事情,您必须运行它。为什么即使没有循环也会发生这种情况?(当打印(…)按您编写的方式放置时,一切正常。您必须在打印之前将所有偶数追加到列表中。如果您在每次追加后打印,它将打印
[2]、[2,4]、[2,4,6]
en = []
import numpy as np
x = [*range(1, 101)]
for n in x:
    if np.mod(n, 2) == 0:
        en.append(n)

print(f'Even numbers between 0 and 101 are: {en}') 
print(f'Even numbers between 0 and 101 are: {[x for x in list(range(1,101)) if x % 2 == 0]}')
even_numbers = []
for n in x:
   if numpy.mod(n, 2) == 0:
       even_numbers.append(n)
print(f'Even numbers between 0 and 101 are: {even_numbers}')