python中如何使用函数改变list

作者:健忘绅士辣鸡君 时间:2022-06-04 13:38:38 

python使用函数改变list

函数内改变外部的一个list如果这么写

def rotate(nums, k):
    length=len(nums)
    if length!=0:
        nums=nums[length-k:length]+nums[0:length-k]
 
l=[1,2,3,4,5,6,7]
rotate(l,3)
print(l)

外部的list并没有改变,而返回的是[1, 2, 3, 4, 5, 6, 7]

要改变list中的内容需要这么写

def rotate(nums, k):
    length=len(nums)
    if length!=0:
        nums[:]=nums[length-k:length]+nums[0:length-k]
 
l=[1,2,3,4,5,6,7]
rotate(l,3)
print(l)

这样就返回的是[5, 6, 7, 1, 2, 3, 4]

python list函数用法

描述

list()函数是Python的内置函数。它可以将任何可迭代数据转换为列表类型,并返回转换后的列表。当参数为空时,list函数可以创建一个空列表。

语法

list(object)

名称说明备注
object待转换为列表的数据类型可省略的参数

使用示例

1. 创建一个空列表(无参调用list函数)

>>> test = list()
>>> test
[]

2. 将字符串转换为列表

>>> test = list('cat')
>>> test
['c', 'a', 't']

3. 将元组转换为列表

>>> a_tuple = ('I love Python.', 'I also love HTML.')
>>> test = list(a_tuple)
>>> test
['I love Python.', 'I also love HTML.']

4. 将字典转换为列表

>>> a_dict = {'China':'Beijing', 'Russia':'Moscow'}
>>> test = list(a_dict)
>>> test
['China', 'Russia']

⚠️注意:将字典转换为列表时,会将字典的值舍去,而仅仅将字典的键转换为列表。如果想将字典的值全部转换为列表,可以考虑使用字典方法dict.values()

5. 将集合转换为列表

>>> a_set = {1, 4, 'sdf'}
>>> test = list(a_set)
>>> test
[1, 'sdf', 4]

6. 将其他可迭代序列转化为列表

下面的代码将range类型和map类型的可迭代序列转换为列表:

>>> test1 = list(range(10))
>>> test2 = list(map(int, [23.2, 33.1]))
>>> test1
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> test2
[23, 33]

注意事项

1. 参数必须是可迭代序列对象

list函数的参数必须是可迭代对象。当选用不可迭代的对象作为参数时,Python报错。

>>> test = list(12)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'int' object is not iterable

将列表转换为列表

可以使用list函数将列表转换为一个列表,这么做Python不会有任何的异常或者报错。它的作用是将参数列表进行深拷贝:

if __name__ == '__main__':
   source_list = ["a", "b", "c", "d"]
   new_list1 = list(source_list)
   print(id(source_list), id(new_list1))
   # output: 4313597760 4312890304

new_list2 = source_list
   print(new_list1)
   # output: ['a', 'b', 'c', 'd']
   print(new_list2)
   # output: ['a', 'b', 'c', 'd']

source_list[0] = "e"
   print(new_list1)
   # output: ['a', 'b', 'c', 'd']
   print(new_list2)
   # output: ['e', 'b', 'c', 'd']

来源:https://blog.csdn.net/why12345678901/article/details/81271728

标签:python,函数,list
0
投稿

猜你喜欢

  • Python通过两个dataframe用for循环求笛卡尔积

    2023-11-02 04:32:24
  • python小白切忌乱用表达式

    2021-08-11 19:57:11
  • 详解CentOS升级Python2.6到Python2.7并安装pip

    2023-06-13 18:59:45
  • 玩转CSS3色彩[译]

    2010-01-13 13:02:00
  • Dlib+OpenCV深度学习人脸识别的方法示例

    2022-11-08 06:34:42
  • 浅析Python中的getattr(),setattr(),delattr(),hasattr()

    2022-11-10 08:26:41
  • PHP使用自定义key实现对数据加密解密的方法

    2023-08-21 12:48:37
  • python按列索引提取文件夹内所有excel指定列汇总(示例代码)

    2021-11-08 09:46:50
  • 使用pycharm和pylint检查python代码规范操作

    2023-06-06 08:02:38
  • 玩体验,先忘掉自己

    2010-01-30 13:36:00
  • 获得MySQL改变字符集的方案

    2010-08-31 14:53:00
  • 使用python检测主机存活端口及检查存活主机

    2021-08-01 05:20:27
  • python fabric实现远程操作和部署示例

    2021-07-18 14:22:14
  • python自动登录12306并自动点击验证码完成登录的实现源代码

    2021-07-08 12:50:29
  • python偏函数的实例用法总结

    2021-06-05 03:42:13
  • Python比较两个日期的两种方法详解

    2023-12-25 03:52:17
  • 解决python运行效率不高的问题

    2023-04-01 05:31:36
  • Python函数递归调用实现原理实例解析

    2023-01-16 12:00:52
  • JavaScript能判定两个function等价吗?

    2009-08-13 14:45:00
  • python实现基于两张图片生成圆角图标效果的方法

    2023-04-20 17:58:56
  • asp之家 网络编程 m.aspxhome.com