Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/csharp-4.0/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 - Fatal编程技术网

Python 如何创建一个从给定列表而不是索引中得到数字的列表?

Python 如何创建一个从给定列表而不是索引中得到数字的列表?,python,Python,我很难得到结果来生成列表中的整数,而不是它所属的索引 #this function takes, as input, a list of numbers and an option, which is either 0 or 1. #if the option is 0, it returns a list of all the numbers greater than 5 in the list #if the option is 1, it returns a list of all the

我很难得到结果来生成列表中的整数,而不是它所属的索引

#this function takes, as input, a list of numbers and an option, which is either 0 or 1.
#if the option is 0, it returns a list of all the numbers greater than 5 in the list
#if the option is 1, it returns a list of all the odd numbers in the list
def splitList(myList, option):
    #empty list for both possible options
    oddList = []
    greaterList = []
    #if option is 0, use this for loop
    if int(option) == 0:
        #create for loop the length of myList
        for i in range(0, len(myList)):
            #check if index is greater than 5
            if ((myList[i])>5):
                #if the number is greater than 5, add to greaterList
                greaterList.append(i)
        #return results
        return greaterList
    #if option is 1, use this for loop
    if int(option) == 1:
        #create for loop the length of myList
        for i in range(0, len(myList)):
            #check if index is odd by checking if it is divisible by 2
            if ((myList[i])%2!=0):
                #if index is not divisible by 2, add the oddList
                oddList.append(i)
        #return results
        return oddList
我收到的结果如下:

>>>splitList([1,2,6,4,5,8,43,5,7,2], 1)
   [0, 4, 6, 7, 8]
我试图得到的结果是[1,5,43,5,7]

if ((myList[i])>5):
    #if the number is greater than 5, add to greaterList
    greaterList.append(i)
不要添加索引
i
,而是添加值(
myList[i]
):

对于
oddList
案例,情况也是如此


注意:@Sukrit Kalla的解决方案更可取,但我将此保留下来,以表明有多种方法可以解决此问题

不要添加索引
i
,而是添加值(
myList[i]
):

对于
oddList
案例,情况也是如此



注意:@Sukrit kalla的解决方案更可取,但我将此保留下来,以表明有多种方法可以解决此问题。

您正在迭代索引的范围。而是迭代列表

for i in myList:
    #check if index is greater than 5
    if i >5:
        #if the number is greater than 5, add to greaterList
        greaterList.append(i)
因此,您的代码被重写为(有一些小改动)

你可以通过这样做来减少它

def splitList(myList, option):
    if int(option) == 0:
        return [elem for elem in myList if elem > 5]
    elif int(option) == 1:
        return [elem for elem in myList if elem % 2 != 0]
输出-

>>> splitList([1,2,6,4,5,8,43,5,7,2], 1)
[1, 5, 43, 5, 7]

您正在迭代索引的范围。而是迭代列表

for i in myList:
    #check if index is greater than 5
    if i >5:
        #if the number is greater than 5, add to greaterList
        greaterList.append(i)
因此,您的代码被重写为(有一些小改动)

你可以通过这样做来减少它

def splitList(myList, option):
    if int(option) == 0:
        return [elem for elem in myList if elem > 5]
    elif int(option) == 1:
        return [elem for elem in myList if elem % 2 != 0]
输出-

>>> splitList([1,2,6,4,5,8,43,5,7,2], 1)
[1, 5, 43, 5, 7]

列表理解大大简化了代码

def split_list(xs, option):
    if option:
        return [x for x in xs if x % 2]
    else:
        return [x for x in xs if x > 5]

列表理解大大简化了代码

def split_list(xs, option):
    if option:
        return [x for x in xs if x % 2]
    else:
        return [x for x in xs if x > 5]

仔细看看您的.append()命令。。。在比较中,您使用的是:

if ((mylList[i])%2!=0) 

…但当你把它放进列表时,你只是在使用

greaterList.append(i)
而不是

greaterList.append(myList[i])

这一定是某个地方的家庭作业或类?

仔细查看您的.append()命令。。。在比较中,您使用的是:

if ((mylList[i])%2!=0) 

…但当你把它放进列表时,你只是在使用

greaterList.append(i)
而不是

greaterList.append(myList[i])

这一定是某个地方的家庭作业或课堂?

我是无数评论的粉丝,但这太荒谬了。你不应该仅仅是以注释的形式重写代码:#如果选项为1,那么就用它作为loopplus,python是我见过的最自我记录的语言。我是无数注释的粉丝,但这太荒谬了。您不应该仅仅以注释的形式重写代码:#如果选项为1,请将其用于loopplus python是我见过的最自我记录的语言