Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/ms-access/4.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/5/reporting-services/3.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 2.7 如何将列表作为输入并将其提供给函数_Python 2.7 - Fatal编程技术网

Python 2.7 如何将列表作为输入并将其提供给函数

Python 2.7 如何将列表作为输入并将其提供给函数,python-2.7,Python 2.7,这是我现在的代码。我如何将输入作为列表,并将其提供给函数。每次它显示无法将str连接到列表的错误时,我都想知道如何输入列表。我使用input()和raw_input()尝试过,然后使用list函数,但问题仍然存在尝试以下修改: import sys a=sys.stdin.read(1) l=list(a) print l def genSubset(l): res=[] if len(l)==0: return [[]] smaller=genSubs

这是我现在的代码。我如何将输入作为列表,并将其提供给函数。每次它显示无法将str连接到列表的错误时,我都想知道如何输入列表。我使用input()和raw_input()尝试过,然后使用list函数,但问题仍然存在

尝试以下修改:

import sys
a=sys.stdin.read(1)
l=list(a)
print l

def genSubset(l):
    res=[]
    if len(l)==0:
        return [[]]
    smaller=genSubset(l[:-1])
    extra=l[-1]
    new=[]
    for small in smaller:
        new.append(small+extra)
    return smaller+ new
print genSubset(l)
示例

l = input("INPUT: ")
#print type(l) # list
#print l

def genSubset(l):
    res=[]
    if len(l)==0:
        return [[]]
    smaller=genSubset(l[:-1])
    extra=l[-1]
    new=[]
    for small in smaller:
        #print type(small) # list
        #print type(extra) # int
        new.append(small + [extra])
    return smaller+ new
print genSubset(l)

在代码
中,额外的
是字符串

>>>
INPUT: [1, 2, 3]
[[], [1], [2], [1, 2], [3], [1, 3], [2, 3], [1, 2, 3]]
要获取包含最后一项的列表,请使用切片表示法:

>>> l = ['1', '2', '3']
>>> extra = l[-1]
>>> extra
'3'

def发电机组(l):
res=[]
如果len(l)==0:
返回[]]
较小=发电机组(l[:-1])
额外=l[-1:][#>>genSubset(['1','2'])
[[], ['1'], ['2'], ['1', '2']]

顺便说一句,使用sys.stdin.read(1)
,您总是会得到长度为1(或0)的字符串。

我使用sys.stdin.read(),它不断提示输入,所以搜索stackoverflow,在一个问题中我看到read(1),所以我认为它只提示一次,所以请更正me@SaranshGupta,使用
raw\u input()
怎么样。它将读取一行并返回该行(不带尾随换行符)。但我想使用sys.stdin,因为raw_输入不能用于大输入data@SaranshGupta,为什么您认为原始输入不能用于大数据?在codechef问题论坛中,他们建议人们使用sys.stdin输入来解决一些涉及大数据的特定问题,例如巨大的输入测试问题
>>> extra = l[-1:]
>>> extra
['3']
def genSubset(l):
    res=[]
    if len(l)==0:
        return [[]]
    smaller=genSubset(l[:-1])
    extra=l[-1:] # <---
    new=[]
    for small in smaller:
        new.append(small+extra)
    return smaller+ new

>>> genSubset(['1', '2'])
[[], ['1'], ['2'], ['1', '2']]