给出一个可能有更多整数列表的整数列表,我们应该返回一个扁平列表。
例如:
Traceback (most recent call last):
File "C:\Users\jaked\AppData\Local\Programs\Python\Python37\lib\site-packages\urllib3\connectionpool.py", line 384, in _make_request
six.raise_from(e, None)
File "<string>", line 2, in raise_from
File "C:\Users\jaked\AppData\Local\Programs\Python\Python37\lib\site-packages\urllib3\connectionpool.py", line 380, in _make_request
httplib_response = conn.getresponse()
File "C:\Users\jaked\AppData\Local\Programs\Python\Python37\Lib\http\client.py", line 1321, in getresponse
response.begin()
File "C:\Users\jaked\AppData\Local\Programs\Python\Python37\Lib\http\client.py", line 296, in begin
version, status, reason = self._read_status()
File "C:\Users\jaked\AppData\Local\Programs\Python\Python37\Lib\http\client.py", line 257, in _read_status
line = str(self.fp.readline(_MAXLINE + 1), "iso-8859-1")
File "C:\Users\jaked\AppData\Local\Programs\Python\Python37\Lib\socket.py", line 589, in readinto
return self._sock.recv_into(b)
File "C:\Users\jaked\AppData\Local\Programs\Python\Python37\Lib\ssl.py", line 1052, in recv_into
return self.read(nbytes, buffer)
File "C:\Users\jaked\AppData\Local\Programs\Python\Python37\Lib\ssl.py", line 911, in read
return self._sslobj.read(len, buffer)
socket.timeout: The read operation timed out
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Users\jaked\AppData\Local\Programs\Python\Python37\lib\site-packages\requests\adapters.py", line 449, in send
timeout=timeout
File "C:\Users\jaked\AppData\Local\Programs\Python\Python37\lib\site-packages\urllib3\connectionpool.py", line 638, in urlopen
_stacktrace=sys.exc_info()[2])
File "C:\Users\jaked\AppData\Local\Programs\Python\Python37\lib\site-packages\urllib3\util\retry.py", line 367, in increment
raise six.reraise(type(error), error, _stacktrace)
File "C:\Users\jaked\AppData\Local\Programs\Python\Python37\lib\site-packages\urllib3\packages\six.py", line 686, in reraise
raise value
File "C:\Users\jaked\AppData\Local\Programs\Python\Python37\lib\site-packages\urllib3\connectionpool.py", line 600, in urlopen
chunked=chunked)
File "C:\Users\jaked\AppData\Local\Programs\Python\Python37\lib\site-packages\urllib3\connectionpool.py", line 386, in _make_request
self._raise_timeout(err=e, url=url, timeout_value=read_timeout)
File "C:\Users\jaked\AppData\Local\Programs\Python\Python37\lib\site-packages\urllib3\connectionpool.py", line 306, in _raise_timeout
raise ReadTimeoutError(self, url, "Read timed out. (read timeout=%s)" % timeout_value)
urllib3.exceptions.ReadTimeoutError: HTTPSConnectionPool(host='stats.nba.com', port=443): Read timed out. (read timeout=30)
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:/Users/jaked/PycharmProjects/NBA/src/classes/scratch.py", line 13, in <module>
dummy = try_request1(commonplayerinfo.CommonPlayerInfo(player['id']))
File "C:\Users\jaked\AppData\Local\Programs\Python\Python37\lib\site-packages\nba_api\stats\endpoints\commonplayerinfo.py", line 32, in __init__
self.get_request()
File "C:\Users\jaked\AppData\Local\Programs\Python\Python37\lib\site-packages\nba_api\stats\endpoints\commonplayerinfo.py", line 40, in get_request
timeout=self.timeout,
File "C:\Users\jaked\AppData\Local\Programs\Python\Python37\lib\site-packages\nba_api\library\http.py", line 123, in send_api_request
response = requests.get(url=base_url, params=parameters, headers=request_headers, proxies=proxies, timeout=timeout)
File "C:\Users\jaked\AppData\Local\Programs\Python\Python37\lib\site-packages\requests\api.py", line 75, in get
return request('get', url, params=params, **kwargs)
File "C:\Users\jaked\AppData\Local\Programs\Python\Python37\lib\site-packages\requests\api.py", line 60, in request
return session.request(method=method, url=url, **kwargs)
File "C:\Users\jaked\AppData\Local\Programs\Python\Python37\lib\site-packages\requests\sessions.py", line 524, in request
resp = self.send(prep, **send_kwargs)
File "C:\Users\jaked\AppData\Local\Programs\Python\Python37\lib\site-packages\requests\sessions.py", line 637, in send
r = adapter.send(request, **kwargs)
File "C:\Users\jaked\AppData\Local\Programs\Python\Python37\lib\site-packages\requests\adapters.py", line 529, in send
raise ReadTimeout(e, request=request)
requests.exceptions.ReadTimeout: HTTPSConnectionPool(host='stats.nba.com', port=443): Read timed out. (read timeout=30)
返回flat_list([1, [2, 2, 2], 4])
[1, 2, 2, 2, 4]
返回flat_list([-1, [1, [-2], 1], -1])
我认为伪代码如下:
[-1, 1, -2, 1, -1]
我遇到了困难,因为我不知道如何持久保存每次递归调用之间建立的列表。
具体地说,我在这条线上有困难:
if input_list length is 1
return input_list
for i, number in input_list
if number is an int
return [number] + flat_list(input_list[i+1:])
if number is a List
return flat_list(input_list[i+1:]
因为我认为将数字返回为return [number] + flat_list(input_list[i+1:])
不会创建所需的输出。