首页 > 编程语言 >list assign in python

list assign in python

时间:2024-02-05 10:46:47浏览次数:33  
标签:blocks python list same will inst copy assign

List Assign with different strategies in Python

Here is a code block

def run_infer(self, g_blocks: List[TraceBlock], max_o_num: int = THREADS_NUM) -> Iterable[B_PAIRS]:
    self.model.eval()
    _g_blocks = [*g_blocks]
    _default_inst = ArmInst(opcode='mov', destination='reg1', source='reg0')
    _trace_inst = TraceInst.init_from_inst(_default_inst)
    _default_block = TraceBlock([_trace_inst], line_idx=-1)
    diff = self.batch_size - len(_g_blocks)

Questions:

  • What is this kind of copy?
    • A shallow copy of the list g_blocks
    • If a deep copy is wanted, should use copy.deepcopy() instead.
  • If it is a shallow copy, why not directly use _g_blocks = g_blocks?
    • Using _g_blocks = g_blocks directly links _g_blocks and g_blocks to the same list object. This means any changes made to the list through _g_blocks will also reflect in g_blocks, and vice versa, because both variables point to the same memory location.
    • In contrast, _g_blocks = [*g_blocks] creates a new list object with the same elements. Here's the difference:
      • Direct Assignment (_g_blocks = g_blocks):
        • Both _g_blocks and g_blocks reference the same list.
        • Changes to the list via either variable will be visible through the other.
        • Example: Adding an item to _g_blocks will also show up when accessing g_blocks.
      • Shallow Copy (_g_blocks = [*g_blocks]):
      • A new list object is created and assigned to _g_blocks.
      • Both _g_blocks and g_blocks contain the same elements, but they are independent lists.
      • Changes to the structure of the list (like adding or removing items) in _g_blocks won't affect g_blocks. However, changes to the elements themselves will reflect in both lists if those elements are mutable objects.

标签:blocks,python,list,same,will,inst,copy,assign
From: https://www.cnblogs.com/holylandofdora/p/18007524

相关文章

  • python import
    pythonimportReferenceworld/│├──africa/│├──__init__.py│└──zimbabwe.py│├──europe/│├──__init__.py│├──greece.py│├──norway.py│└──spain.py│└──__init__.pyinthiscase,ifeverycountryf......
  • [转]gdb源码安装,指定使用的python版本
    转自:https://www.cnblogs.com/shengulong/p/8053370.html gdb调试python的时候,需要根据不同的python版本2.6、2.7、3.x安装相应的gdb;如何指定关联的python版本?下面gdb源码,解压后,进入目录:./configure-h并没有发现--with-python的选项。没有也没有问题,没有也可以自己加:whi......
  • 利用Python进行数据分析 pdf下载
    本书由Pythonpandas项目创始人WesMcKinney亲笔撰写,详细介绍利用Python进行操作、处理、清洗和规整数据等方面的具体细节和基本要点。第2版针对Python3.6进行全面修订和更新,涵盖新版的pandas、NumPy、IPython和Jupyter,并增加大量实际案例,可以帮助你高效解决一系列数据分析问题。......
  • Decorations in Python
    DecorationsinPythonReferences:ref1,ref2,ref3AdecoratorisadesignpatterninPythonthatallowsausertoaddnewfunctionalitytoanexistingobjectwithoutmodifyingitsstructure.FunctionsinPythonarefirst-classcitizens.Thismeansthatt......
  • 深度学习-DNN深度神经网络-反向传播02-python代码实现nn-41
    目录1.举例2.python实现1.举例2.python实现importnumpyasnpfromsklearn.datasetsimportfetch_mldatafromsklearn.utils.extmathimportsafe_sparse_dotdeftrain_y(y_true):y_ohe=np.zeros(10)y_ohe[int(y_true)]=1returny_ohemnist......
  • Python 机器学习 数据集分布可视化
    ​  Python的机器学习项目中,可视化是理解数据、模型和预测结果的重要工具。通过可视化可以观察数据集的分布情况,了解数据的特征和规律,可以评估模型的性能,发现模型的优缺点,分析预测结果,解释模型的预测过程。可视化数据集的分布和预测结果是整个过程中一个重要的步骤。通常可视......
  • 工作安排-od-python
    题目:小明每周上班都会拿到自己的工作清单,工作清单内包含n项工作,每项工作都有对应的耗时时长(单位h)和报酬,工作的总报酬为所有已完成工作的报酬之和。那么请你帮小明安排一下工作,保证小明在指定的工作时间内工作收入最大化。输入描述:输入的第一行为两个正整数T,n。T代表工作时长(单......
  • PyTorch下,使用list放置模块,导致计算设备不一的报错
    报错在复现Transformer代码的训练阶段时,发生报错:RuntimeError:Expectedalltensorstobeonthesamedevice,butfoundatleasttwodevices,cuda:0andcpu!解决方案通过next(linear.parameters()).device确定model已经在cuda:0上了,同时输入model.forward()的......
  • windows查看端口占用,通过端口找进程号(查找进程号),通过进程号定位应用名(查找应用)(netstat
     文章目录通过端口号查看进程号`netstat`通过进程号定位应用程序`tasklist` 通过端口号查看进程号netstat在Windows系统中,可以使用netstat命令来查看端口的占用情况。以下是具体的步骤:打开命令提示符(CMD):按Win+R组合键打开运行对话框,输入cmd并按Enter键。......
  • Windows自带搜索太慢?搜索神器listary推荐
    今天推荐的软件是listary,那个经常被拼写为listray的listary。相信很多人都用过everything,一款非常强大的搜索软件,但是,everything虽然搜索迅速,但是功能比较单一,开启比较麻烦,可能你打开everything的时间用listary已经搜完了。效果如下:还支持计算器,打开网址,网络搜索,命令(网络搜索......