Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/16.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_Python 3.x_List_Jupyter Notebook - Fatal编程技术网

从python列表中提取元素

从python列表中提取元素,python,python-3.x,list,jupyter-notebook,Python,Python 3.x,List,Jupyter Notebook,我想要q_奇数列表中p的奇数索引元素和q_偶数列表中p的偶数索引元素 p = [4, 8, 7,19,23,78,67,37,3783,4] q_odd = [] q_even = [] 结果将是: q_odd = [8,19,78,37,4] q_even = [4,7,23,67,3783] 解决方案: 这将解决您的问题陈述: p = [4, 8, 7,19,23,78,67,37,3783,4] q_even, q_odd = [], [] # METHOD

我想要q_奇数列表中p的奇数索引元素和q_偶数列表中p的偶数索引元素

p = [4, 8, 7,19,23,78,67,37,3783,4]

q_odd = []  
    
q_even = []
结果将是:

q_odd = [8,19,78,37,4]
    
q_even = [4,7,23,67,3783]

解决方案:
这将解决您的问题陈述:

p = [4, 8, 7,19,23,78,67,37,3783,4]
q_even, q_odd = [], []

# METHOD 1 : using continue with if condition for data processing
for idx, num in enumerate(p):
    if idx % 2 == 0:
        q_even.append(num)
        continue
    q_odd.append(num)

# METHOD 2 : Using if-else statement for data processing
for idx, num in enumerate(p):
    if idx % 2 == 0:
        q_even.append(num)
    else:
        q_odd.append(num)

# Method 3: using list-slicing : as explained by '@hiro protagonist'
q_odd = p[1::2]
q_even = p[::2]

print(q_odd)
print(q_even)
输出:

[8, 19, 78, 37, 4]
[4, 7, 23, 67, 3783]

注意:一次只能使用一种方法解决方案:
这将解决您的问题陈述:

p = [4, 8, 7,19,23,78,67,37,3783,4]
q_even, q_odd = [], []

# METHOD 1 : using continue with if condition for data processing
for idx, num in enumerate(p):
    if idx % 2 == 0:
        q_even.append(num)
        continue
    q_odd.append(num)

# METHOD 2 : Using if-else statement for data processing
for idx, num in enumerate(p):
    if idx % 2 == 0:
        q_even.append(num)
    else:
        q_odd.append(num)

# Method 3: using list-slicing : as explained by '@hiro protagonist'
q_odd = p[1::2]
q_even = p[::2]

print(q_odd)
print(q_even)
输出:

[8, 19, 78, 37, 4]
[4, 7, 23, 67, 3783]
注意:一次只能使用一种方法

您只需使用:

p[开始:停止:步骤]
从给定列表创建一个新列表
p
。使用
step=2
你每取一个元素,
start=0
(默认值为
0
,因此甚至不需要写,你只需写
p[::2]
)就会给你索引为偶数的元素
start=1
索引为奇数的索引。

您可以使用:

p = [4, 8, 7,19,23,78,67,37,3783,4]
q_even = [p[i] for i in range(len(p)) if i%2 == 0]  
print(q_even)
q_odd = [p[i] for i in range(len(p)) if i%2 != 0]
print(q_odd)
# Have used list comprehension.


p[开始:停止:步骤]
从给定列表创建一个新列表
p
。使用
step=2
你每取一个元素,
start=0
(默认值为
0
,因此甚至不需要写,你只需写
p[::2]
)就会给你索引为偶数的元素
start=1
具有奇数索引的元素。

您尝试了什么?我正在尝试使用if-else语句进行for循环,但无法正确应用这给了我们奇数元素值,我们需要奇数索引@hiroprojector的元素您尝试了什么?我正在尝试使用if-else语句进行for循环,但无法正确应用这给了我们奇数元素值,我们需要奇数索引@HiroProgator的元素这给了我们非元素值的索引值,结果得到q_偶数=[0,2,4,6,8]结果需要-q_偶数=[4,7,23,673783]这给了我们非元素值的索引值,结果得到q_偶数=[0,2,4,6,8]想要的结果-q_偶数=[4,7,23,673783]@divyesshcheck,现在可以了是的,现在可以了,谢谢buddy@Andrew这是一种非常非蟒蛇式的方式。请参见hiro Progator的答案,了解更好的方法。这给了我们索引值,如非元素值,结果得到q_偶数=[0,2,4,6,8]需要的结果-q_偶数=[4,7,23,673783]这给了我们索引值,如非元素值,结果得到q_偶数=[0,2,4,6,8]需要的结果-q_偶数=[4,7,23,673783]@divyesshcheck,现在可以了是的,现在可以了,谢谢buddy@Andrew这是一种非常非蟒蛇式的方式。关于更好的方法,请参见hiro Progator的答案。或者在
if。。else
block。使用if-else语句更新代码。看一看它将非常好地工作。或者在
范围内,如果。。else
block。使用if-else语句更新代码。看一看它会很好地工作。
p = [4, 8, 7,19,23,78,67,37,3783,4]
q_even = [p[i] for i in range(len(p)) if i%2 == 0]  
print(q_even)
q_odd = [p[i] for i in range(len(p)) if i%2 != 0]
print(q_odd)
# Have used list comprehension.