在python中编辑列表中的元素 [英] Editing elements in a list in python

查看:59
本文介绍了在python中编辑列表中的元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何从列表中的元素中删除字符?

示例:

mylist = ['12:01', '12:02']

我想从文件中的时间戳中删除冒号,以便我可以更轻松地将它们转换为 24 小时制.现在我正在尝试遍历列表中的元素并搜索包含冒号的元素并进行替换.

 for num in mylist:re.sub(':', '', num)

但这似乎不起作用.

帮助!

解决方案

列表推导式解决方案是最 Pythonic 的解决方案,但是,有一个重要的转折:

mylist[:] = [s.replace(':', '') for s in mylist]

如果您分配给 mylist 裸名,就像在另一个答案中一样,而不是分配给 mylist[:],整个列表切片",因为我建议,您确实在做与替换列表中的条目"非常不同的事情:您正在创建一个新列表,只是重新绑定您之前用来引用旧列表的裸名.

如果旧列表被多个名称(包括容器中的条目)引用,则此重新绑定不会影响其中任何一个:例如,如果您有一个将 mylist 作为一个参数,裸名赋值只对函数有任何影响,并且不会改变调用者看到的列表内容.

分配给整个列表切片,mylist[:] = ...改变列表对象,而不是通过切换裸名绑定来解决问题——现在该列表确实发生了变化,无论如何引用,新值都是所看到的.例如,如果您有一个将 mylist 作为参数的函数,整个列表切片赋值会改变调用者所看到的列表内容.

关键是确切地知道你想要什么效果——最常见的是你想要改变列表对象,所以,如果必须猜测,整个列表切片分配通常是最好的猜测;-).在性能方面,无论哪种方式都没有区别(除了裸名分配,如果它同时保留新旧列表对象,当然,无论两个对象仍然存在,无论时间流逝都会占用更多内存).

How do I remove a character from an element in a list?

Example:

mylist = ['12:01', '12:02']

I want to remove the colon from the time stamps in a file, so I can more easily convert them to a 24hour time. Right now I am trying to loop over the elements in the list and search for the one's containing a colon and doing a substitute.

for num in mylist:
    re.sub(':', '', num)

But that doesn't seem to work.

Help!

解决方案

The list comprehension solution is the most Pythonic one, but, there's an important twist:

mylist[:] = [s.replace(':', '') for s in mylist]

If you assign to mylist, the barename, as in the other answer, rather than to mylist[:], the "whole-list slice", as I recommend, you're really doing something very different than "replacing entries in the list": you're making a new list and just rebinding the barename that you were previously using to refer to the old list.

If that old list is being referred to by multiple names (including entries in containers), this rebinding doesn't affect any of those: for example, if you have a function which takes mylist as an argument, the barename assignment has any effect only locally to the function, and doesn't alter what the caller sees as the list's contents.

Assigning to the whole-list slice, mylist[:] = ..., alters the list object rather than mucking around with switching barenames' bindings -- now that list is truly altered and, no matter how it's referred to, the new value is what's seen. For example, if you have a function which takes mylist as an argument, the whole-list slice assignment alters what the caller sees as the list's contents.

The key thing is knowing exactly what effect you're after -- most commonly you'll want to alter the list object, so, if one has to guess, whole-list slice assignment is usually the best guess to take;-). Performance-wise, it makes no difference either way (except that the barename assignment, if it keeps both old and new list objects around, will take up more memory for whatever lapse of time both objects are still around, of course).

这篇关于在python中编辑列表中的元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆