首页 > 编程语言 >Python list replication All In One

Python list replication All In One

时间:2023-09-13 18:25:41浏览次数:45  
标签:strs nums Python list number replication https print

Python list replication All In One

error

For the reference value list, using the list * number does not work as you expected.

# reference value list
letter_lists = [['A', 'B']]
number_lists = [[1, 2]]
strs = letter_lists * 2
nums = number_lists * 2
print('strs =', strs)
print('nums =', nums)
# strs = [['A', 'B'], ['A', 'B']]
# nums = [[1, 2], [1, 2]]

strs[0][1] = "❌"
nums[0][1] = "❌"
print('strs =', strs)
print('nums =', nums)
# strs = [['A', '❌'], ['A', '❌']]
# nums = [[1, '❌'], [1, '❌']]

solution

Using the literal to create a list is OK because it does not reuse any item in the original list.

# literal list
strs = [['A', 'B'], ['A', 'B']]
nums = [[1, 2], [1, 2]]
print('strs =', strs)
print('nums =', nums)
# strs = [['A', 'B'], ['A', 'B']]
# nums = [[1, 2], [1, 2]]

strs[0][1] = "✅"
nums[0][1] = "✅"
print('strs =', strs)
print('nums =', nums)
# strs = [['A', '✅'], ['A', 'B']]
# nums = [[1, '✅'], [1, 2]]

For the primary value list, use the list * number is OK

# primary value list
letter_list = ['A', 'B']
number_list = [1, 2]
strs = letter_list * 2
nums = number_list * 2
print('strs =', strs)
print('nums =', nums)
# strs = ['A', 'B', 'A', 'B']
# nums = [1, 2, 1, 2]

strs[1] = "✅"
nums[1] = "✅"
print('strs =', strs)
print('nums =', nums)
# strs = ['A', '✅', 'A', 'B']
# nums = [1, '✅', 1, 2]

The reason is that you do not have a deep understanding of the list data type when using list * number to create a new copy of the list in Python.

image

demos

(

标签:strs,nums,Python,list,number,replication,https,print
From: https://www.cnblogs.com/xgqfrms/p/17700400.html

相关文章

  • Python第四章(5)集合
    1.集合的特性:(1)集合为无序的不重复元素序列。(2)集合中的元素必须为不可变的类型。2.集合的创建与删除:(1)直接使用大括号:day={1,2,"Monsday"}(2)若集合中有重复元素,python会自动保留一个。(3)集合推导式:squared={x**2forxinrange(1,3)}......
  • restTemplate接收返回对象中的数据为list是集合被转为对象问题处理
    接收的对象resultModel是服务者接口自定义的返回对象,包含msg,code,以及一个泛型对象data用于存放接口返回的数据。 当消费者通过restTemplate,postObject调用时,data需要通过map封装一下需要返回的数据,然后将map填充到data。若map中出现了集合数据,当集合数据只有一条时,会出现集合......
  • 【Python篇】Python基础语法
    【Python篇】Python基础语法拖拖拖,能使工作便捷高效的为何要拒绝,作个记录---【蘇小沐】1.实验环境默认情况下,Python3源码文件以<fontcolor='red'>UTF-8</font>编码,所有字符串都是unicode字符串。指定源码文件其它编码:#-*-coding:cp-1252-*-#允许在源文件中使用W......
  • (转)对比学习:Golang VS Python3
    原文:https://juejin.cn/post/6844903843050815502Golang和Python都是目前在各自领域最流行的开发语言之一。Golang其高效而又友好的语法,赢得了很多后端开发人员的青睐,最适用于高并发网络编程的语言之一。Python不用说,TIOBE排行榜的前十常驻居民,现在已经稳定在前五了。在机器......
  • python pandas 读取msql并转置数据表
    importrandom,string,json,pymysqlimportpandasaspdconnect=pymysql.connect(user='',password='h',db='g,host='r',port=3306,charset='ut......
  • 微信使用python定时主动群发消息
    目前市面上的微信营销软件,绝大部分是模拟登录或者进程hook外挂形式,属于违规使用微信的范畴,容易被微信官方封号。经过思考后,我觉得利用python自动化UI点击,鼠标键盘操作,是符合真人使用微信的习惯的,被封的风险最低。因为必须是UI自动化,所以我们使用windows系统,并且结合微信电脑版来实......
  • Python pandas 读取 excel文件
    参考:https://www.jb51.net/article/283627.htm#单纯导出一个数据集defexportData_pandas(excelFile='output.xlsx'):try:#data={'Name':['Alice','Bob','Charlie'],#'Age':[......
  • python学习笔记(持续更新)
    一、对列表的操作#建立一个列表list=[1,2,3,4]#打印列表print(list)#在列表尾部添加元素list.append(5)#在指定位置插入元素list.insert(0,0)#在0位置插入元素0#在尾部插入另一个列表List=[5,6,7]list.extend(List)#删除指定位置的元素list.remove(0)#移除指定......
  • 《精通Python设计模式》 PDF电子书 +源码
    第1章工厂模式第2章建造者模式第3章其他创建型模式第4章适配器模式第5章装饰器模式第6章桥接模式第7章外观模式第8章其他结构型模式第9章职责链模式第10章命令模式第11章观察者模式第12章状态模式第13章其他行为型模式第14......
  • Python基于Flask的高校舆情分析,舆情监控可视化系统
    一、前言在当今社会,舆情监控越来越被重视。随着互联网技术的发展,我们从传统媒体渠道、官方报告、调查问卷等方式搜集到的舆情信息,逐渐被网络上的内容所替代。因为网络上的内容传播速度快、及时性强、覆盖范围广,成为了管理者、企业、政府等了解社会大众情绪、掌握市场动向的重要途......