为什么两个列表在python中只调用一个后调用append()函数?

时间:2016-07-10 00:24:53

标签: python list

所以这些天我正在学习python而且遇到了问题。这是我的代码。

def partition(lst, left_index, right_index):
    pivot = lst[right_index]

    while True:
        while left_index < right_index and lst[left_index] <= pivot:
            left_index += 1

        while left_index < right_index and lst[right_index] > pivot:
            right_index -= 1

        if left_index < right_index:
            lst[left_index], lst[right_index] = lst[right_index], lst[left_index]
        else:
            return right_index

输出如下。

a = [1, 2, 3, 4, 5]
b = a
print(a)
print(b)
b.append(8)
print(a)
print(b)

为什么即使只在 b 上调用它们,两个列表也会被修改?当我们分配b = a时,我们不是在制作该列表的单独副本吗?

1 个答案:

答案 0 :(得分:2)

因为python使用对数组,对象等的引用。如果你想要数组的副本使用File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1836, in __call__ return self.wsgi_app(environ, start_response) File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1820, in wsgi_app response = self.make_response(self.handle_exception(e)) File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1403, in handle_exception reraise(exc_type, exc_value, tb) File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1817, in wsgi_app response = self.full_dispatch_request() File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1477, in full_dispatch_request rv = self.handle_user_exception(e) File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1381, in handle_user_exception reraise(exc_type, exc_value, tb) File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1475, in full_dispatch_request rv = self.dispatch_request() File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1461, in dispatch_request return self.view_functions[rule.endpoint](**req.view_args) File "/usr/local/lib/python2.7/dist-packages/flask_login.py", line 758, in decorated_view return func(*args, **kwargs) File "/var/www/vwtools/app/toolset/views.py", line 33, in decorated return func(*args, **kwargs) File "/var/www/vwtools/app/toolset/views.py", line 101, in invoke_model_api mr = api.create_modelrun( modelrun=dict(title='Model Run-Toolset', model_name='prms')) File "/usr/local/lib/python2.7/dist-packages/client/swagger_client/apis/default_api.py", line 119, in create_modelrun callback=params.get('callback')) File "/usr/local/lib/python2.7/dist-packages/client/swagger_client/api_client.py", line 322, in call_api response_type, auth_settings, callback) File "/usr/local/lib/python2.7/dist-packages/client/swagger_client/api_client.py", line 149, in __call_api post_params=post_params, body=body) File "/usr/local/lib/python2.7/dist-packages/client/swagger_client/api_client.py", line 358, in request body=body) File "/usr/local/lib/python2.7/dist-packages/client/swagger_client/rest.py", line 213, in POST body=body) File "/usr/local/lib/python2.7/dist-packages/client/swagger_client/rest.py", line 147, in request headers=headers) File "/usr/local/lib/python2.7/dist-packages/urllib3/request.py", line 73, in request **urlopen_kw) File "/usr/local/lib/python2.7/dist-packages/urllib3/request.py", line 151, in request_encode_body return self.urlopen(method, url, **extra_kw) File "/usr/local/lib/python2.7/dist-packages/urllib3/poolmanager.py", line 248, in urlopen response = conn.urlopen(method, u.request_uri, **kw) File "/usr/local/lib/python2.7/dist-packages/urllib3/connectionpool.py", line 583, in urlopen conn = self._get_conn(timeout=pool_timeout) File "/usr/local/lib/python2.7/dist-packages/urllib3/connectionpool.py", line 257, in _get_conn return conn or self._new_conn() File "/usr/local/lib/python2.7/dist-packages/urllib3/connectionpool.py", line 218, in _new_conn strict=self.strict, **self.conn_kw) File "/usr/local/lib/python2.7/dist-packages/urllib3/connection.py", line 126, in __init__ _HTTPConnection.__init__(self, *args, **kw) TypeError: __init__() got an unexpected keyword argument 'assert_hostname'

copy