我有一个列表,需要创建条件满足次数的计数.这样做是否更加pythonic:
cnt = sum([1 for s in a_list if some_condition])
Run Code Online (Sandbox Code Playgroud)
或者这是更pythonic:
cnt = 0
for s in a_list:
if (some_condition):
cnt += 1
Run Code Online (Sandbox Code Playgroud) 在 PHP 中,我经常使用array_column()函数,因为它允许我从一个数组中的特定列中检索值,在一个漂亮的数组中返回自己,例如,使用这个数组:
$users = [
[
'id' => 1,
'name' => 'Peter'
],
[
'id' => 2,
'name' => 'Paul'
],
[
'id' => 3,
'name' => 'John'
]
];
Run Code Online (Sandbox Code Playgroud)
做array_column($users, 'name')将返回:
Array
(
[0] => Peter
[1] => Paul
[2] => John
)
Run Code Online (Sandbox Code Playgroud)
自从过渡到 Python 后,我仍然没有找到一个内置函数,我可以用它在 a listof dicts上做同样的事情。
这样的功能是否存在,如果不存在,实现这一目标的最佳方法是什么?
拿一个2d列表.我想创建一个新列表,只包含每个列表中的第i个元素.做这个的最好方式是什么?
我有:
map(lambda x: x[i], l)
Run Code Online (Sandbox Code Playgroud)
这是一个例子
>>> i = 0
>>> l = [[1,10],[2,20],[3,30]]
>>> map(lambda x: x[i], l)
[1, 2, 3]
Run Code Online (Sandbox Code Playgroud) 有没有办法在矩阵中选择每个第二个或第三个(例如)项目?
例如:
f = [["1", "5", "8", "9"], ["2", "6", "9", "10"], ["3", "7", "11", "12"]]
Run Code Online (Sandbox Code Playgroud)
我想知道是否有一个直接的功能来选择每个列表中的每个第二个数字(最好也将这些数字放在一个列表中).从而导致:
["5", "6", "7"]
Run Code Online (Sandbox Code Playgroud)
我知道我可以使用循环实现这一点,但我想知道我是否可以直接实现这一点.
使用时我在Python中遇到这个小问题
print ",".join(array)
Run Code Online (Sandbox Code Playgroud)
例如,使用array=['dog\r', 'monkey', 'pig']输出时如下所示:
,monkey,pig
Run Code Online (Sandbox Code Playgroud)
有任何想法吗?
好吧,代码本身看起来像这样
a = raw_input(">>")
uAlpha = a.split(",")
Run Code Online (Sandbox Code Playgroud)
现在我知道如何删除/ r但是如何防止它首先到达那里?
说我有一个字符串元素列表
wordlist = ["hi what's up home diddle mc doo", "Oh wise master kakarot", "hello have a da"]
Run Code Online (Sandbox Code Playgroud)
我希望列表中的每个元素最多包含3个单词或20个字符.有这个功能吗?
我想检查字符串列表中的字符串是否包含某个子字符串。如果他们这样做,我想将该列表项保存到新列表中:
list = ["Maurice is smart","Maurice is dumb","pie","carrots"]
Run Code Online (Sandbox Code Playgroud)
我尝试使用以下代码:
new_list = [s for s in list if 'Maurice' in list]
Run Code Online (Sandbox Code Playgroud)
但这只是复制列表,如果它的项目之一是'Maurice'。所以我想知道是否有办法通过使用以下语法来解决这个问题:
if "Maurice" in list:
# Code that saves all list items containing the substring "Maurice" to a new list
Run Code Online (Sandbox Code Playgroud)
结果应该是:
new_list = ["Maurice is smart", "Maurice is dumb"]
Run Code Online (Sandbox Code Playgroud)
如果一直在寻找一种方法来做到这一点,但我找不到任何东西。
我从这个问题中学到了可以", ".join(var)在Python 中使用数组连接值.此代码适用于字符串,例如:
var = ["hello","world"]
print (", ".join(var))
Run Code Online (Sandbox Code Playgroud)
给出hello, world我想要的输出.
但是,当我在数组中使用数字时:
var = [1,2,3]
print (", ".join(var))
Run Code Online (Sandbox Code Playgroud)
它给出了一条错误消息:
Traceback (most recent call last):
File "C:\path\test.py", line 2, in <module>
print (", ".join(var))
TypeError: sequence item 0: expected str instance, int found
Run Code Online (Sandbox Code Playgroud)
然而,我希望输出为:
1, 2, 3
Run Code Online (Sandbox Code Playgroud)
有没有其他方法可以这样,它将打印所有数字,还有能力增加,所以我可以有任何数量的数字?
我有一个清单:
['6.26%', '5.94%', '7.47%', '6.90%', '5.99%', '7.94%', '8.75%',
'9.01%', '10.07%', '5.51%', '4.87%', '5.88%', '4.26%', '2.97%',
'6.38%', '4.93%', '3.96%', '4.62%', '3.73%', '5.15%', '0.86%',
'2.68%', '4.01%', '4.89%', '5.84%', '5.23%', '5.57%', '5.53%',
'2.39%', '1.00%', '2.44%', '4.65%', '3.66%', '4.60%', '4.54%',
'2.30%', '-1.51%', '2.36%', '3.13%', '3.12%', '1.28%', '3.55%',
'3.48%', '1.13%', '3.45%']
Run Code Online (Sandbox Code Playgroud)
并且我想从列表中删除所有 ' 和 % 以给我一个包含整数的列表,我可以用它来添加到另一个列表中。我已经看到并查看了许多关于列表的问题,但似乎没有一个适用于我的场景。请帮忙!