我有一个列表,我想用None替换值,其中condition()返回True.
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
Run Code Online (Sandbox Code Playgroud)
例如,如果条件检查bool(项目%2)应该返回:
[None, 1, None, 3, None, 5, None, 7, None, 9, None]
Run Code Online (Sandbox Code Playgroud)
最有效的方法是什么?
假设我有一个清单:
myl = [1, 2, 3, 4, 5, 4, 4, 4, 6]
Run Code Online (Sandbox Code Playgroud)
什么是最有效的,最简单的Python的方式就地(双加权)替换所有出现的4用44?
我也很好奇为什么没有standard办法做到这一点(特别是,当strings有一个非就地 replace方法)?
使用字符串,您可以替换长度大于1的子字符串.例如,'abcabc'.replace('abca','*')yield '*bc'.
我想用一个列表来做这个.例如,像这样:
[1, [0], 'a', 1, [0], 'a'].replace([1, [0], 'a', 1], [5])
Run Code Online (Sandbox Code Playgroud)
应该屈服
[5, [0], 'a']
Run Code Online (Sandbox Code Playgroud)
请注意,此问题不重复,因为它们不需要匹配模式,只需要匹配列表中的特定项目.
我试图替换列表中的元素
# the raw data
square = ['(', ')', '.', '^', '-']
# the result I want
square = ['[', ']', '.', '^', '-']
Run Code Online (Sandbox Code Playgroud)
使用remove和的方法的多个步骤insert
In [21]: square.remove('(')
In [22]: square.remove(')')
In [23]: square.insert(0, '[')
In [24]: square.insert(1, ']')
In [25]: square
Out[25]: ['[', ']', '.', '^', '-']
Run Code Online (Sandbox Code Playgroud)
如何以一种直截了当的方式解决这样的问题?
我有一个 python 列表如下:
['item1','item2','item3']
Run Code Online (Sandbox Code Playgroud)
我正在尝试将列表中的项目重命名为
['person1','person2','person3']
Run Code Online (Sandbox Code Playgroud)
谁能指导我。谢谢
与此问题类似,但我不想用一个项目替换另一个项目,而是想用列表的内容替换任何一个项目.
orig = [ 'a', 'b', 'c', 'd', 'c' ]
repl = [ 'x', 'y', 'z' ]
desired = [ 'a', 'b', 'x', 'y', 'z', 'd', 'x', 'y', 'z' ]
# these are all incorrect, or fail to compile
[ repl if x == 'c' else x for x in orig ]
[ [a for a in orig] if x == 'c' else x for x in orig ]
[ (a for a in orig) if x == …Run Code Online (Sandbox Code Playgroud) 我使用.split()方法从字符串中创建了一个列表.例如:string ="我喜欢鸡"我将使用.split()来创建字符串中的单词列表['I','like','chicken']
现在,如果我想用其他东西替换'chicken',我可以使用的方法就像.replace( )但是对于一个清单?
我有一些使用 itertools.zip_longest 压缩在一起的数据
import itertools
names = 'Tim Bob Julian Carmen Sofia Mike Kim Andre'.split()
locations = 'DE ES AUS NL BR US'.split()
confirmed = [False, True, True, False, True]
zipped_up = list(itertools.zip_longest(names, locations, confirmed))
Run Code Online (Sandbox Code Playgroud)
如果我按照现在的方式打印 zipped_up,我会得到以下信息:
[('Tim', 'DE', False), ('Bob', 'ES', True),
('Julian','AUS', True), ('Carmen', 'NL', False),
('Sofia', 'BR',True), ('Mike', 'US', None),
('Kim',None, None),('Andre', None, None)]
Run Code Online (Sandbox Code Playgroud)
这很好,缺失值默认为“无”。 现在我想将“无”值更改为 '-'。
似乎我应该能够在以下嵌套循环中这样做。如果我在下面的代码中包含一个打印语句,看起来一切都按我想要的方式工作:
for items in zipped_up:
for thing in items:
if thing == None:
thing = '-'
print(thing)
Run Code Online (Sandbox Code Playgroud)
但是如果我再次打印 zipped_up …
我在 python 中有一个二维数组,如下所示:
2d_array_1 =[[0,1],[1,0]]
Run Code Online (Sandbox Code Playgroud)
并想要创建第二个相同大小的数组,所有值都初始化为零。
如果初始数组的大小可以变化,我该如何在 python 中做到这一点?
类似于这个问题:查找和替换列表(python)中的元素, 但使用多维数组。例如,我想用 0 替换所有 N:
list =
[['N', 0.21],
[-1, 6.6],
['N', 34.68]]
Run Code Online (Sandbox Code Playgroud)
我想把它改成:
list =
[[0, 0.21],
[-1, 6.6],
[0, 34.68]]
Run Code Online (Sandbox Code Playgroud) python ×10
list ×5
python-3.x ×3
replace ×3
arrays ×1
find ×1
in-place ×1
methods ×1
performance ×1
string ×1