假设我想创建一个包含5个元素的列表或numpy数组,如下所示:
array = [i, j, k, l, m]
Run Code Online (Sandbox Code Playgroud)
哪里:
i 在1.5到12.4的范围内j 在0到5的范围内k 在4到16的范围内l 在3到5的范围内m 在2.4到8.9的范围内.这是一个示例,表明某些范围包括分数.什么是一个简单的方法来做到这一点?
假设我有这段文字:
Saturday and Sunday and Monday and Tuesday and Wednesday and Thursday and Friday are days of the week.
Run Code Online (Sandbox Code Playgroud)
我希望除了最后and一个用逗号代替之外的所有内容:
Saturday, Sunday, Monday, Tuesday, Wednesday, Thursday and Friday are days of the week.
Run Code Online (Sandbox Code Playgroud)
在正则表达式中有一个简单的方法吗?据我所知,replace正则表达式中的方法一直替换字符串.
我想填写一个特定格式的字符串.当我有一个值时,很容易构建它:
>>> x = "there are {} {} on the table.".format('3', 'books')
>>> x
'there are 3 books on the table.'
Run Code Online (Sandbox Code Playgroud)
但是如果我有很长的对象列表呢?
items =[{'num':3, 'obj':'books'}, {'num':1, 'obj':'pen'},...]
Run Code Online (Sandbox Code Playgroud)
我想用完全相同的方式构造句子:
There are 3 books and 1 pen and 2 cellphones and... on the table
Run Code Online (Sandbox Code Playgroud)
鉴于我不知道列表的长度,我怎么能这样做呢?使用format我可以很容易地构造字符串,但我必须事先知道列表的长度.
假设我有一个类似于此的DataFrame:
d = {'col1': [0, 2, 4], 'col2': [1, 3, 5], 'col3': [2, 4, 8]}
df = pd.DataFrame(d)
col1 col2 col3
0 0 1 2
1 2 3 4
2 4 5 8
Run Code Online (Sandbox Code Playgroud)
如何选择col1和col2并将它们转换为此数组?
array([[0, 1],
[2, 3],
[4, 5]])
Run Code Online (Sandbox Code Playgroud) 对于分类任务,我使用投票分类器来集成逻辑回归和SVM,投票参数设置为soft.结果明显优于每个单独的模型.我不确定我是否理解它是如何工作的.模型如何才能在两个模型之间找到多数投票?
python classification machine-learning scikit-learn ensemble-learning
假设我有一个这样的列表,其中数字以不同的步骤增加:
[ 0, 4, 6, 8, 12, 15, 19, 21, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32]
Run Code Online (Sandbox Code Playgroud)
我想返回列表中第一个元素的索引,其中增量是增量的(仅+1步)。在本例中,23 是第一个位置,从该点开始增量变为增量,其索引为 8,这就是我想要的输出。
实现这一目标的优雅简单的方法是什么?这是我尝试过的:
>>> for (a,b) in zip(l, l[1:]):
... if b-a == 1:
... print(l.index(a))
... break
Run Code Online (Sandbox Code Playgroud)
更新:在这个特定的设置中,一旦增加变得增量,它将继续保持这种状态。增加可能永远不会增加。
假设我有一个列表如下:
l = [torch.randn(2,3), torch.randn(2,4),torch.randn(2,5)]
Run Code Online (Sandbox Code Playgroud)
我想在第二个维度中对所有元素进行零填充,这样它们将扩展到 5 个元素(5 是第二个维度中三个元素之间的最大数量)。我怎样才能做到这一点。我尝试过这个但失败了:
from torch.nn.utils.rnn import pad_sequence
pad_sequence(l, batch_first=True, padding_value=0)
Run Code Online (Sandbox Code Playgroud)
这导致了以下错误:
RuntimeError: The expanded size of the tensor (3) must match the existing size (4) at non-singleton dimension 1. Target sizes: [2, 3]. Tensor sizes: [2, 4]
Run Code Online (Sandbox Code Playgroud)
Numpy 中的等效答案也将受到赞赏。
假设我有一个像这样的numpy数组:
arr = np.array([[1,1,1,1], [2,2,2,2], [3,3,3,3], [4,4,4,4])
Run Code Online (Sandbox Code Playgroud)
我也有一个确定预期长度的列表:
lens = [1,2,3,4]
Run Code Online (Sandbox Code Playgroud)
是否有一种优雅而Pythonic的方式来返回一个新数组,并使用该lens变量选择了每个对应的元素?
输出应为:
[[1], [2,2],[3,3,3], [4,4,4,4]]
Run Code Online (Sandbox Code Playgroud) li = ['a', '#b', 'c']
repl = ['1', '2', '3']
Run Code Online (Sandbox Code Playgroud)
我想改变'#b'与repl子表,意义,这是我想要的输出:
['a', ['1', '2', '3'], 'c']
Run Code Online (Sandbox Code Playgroud)
这是我到目前为止的尝试:
for i in li:
if i.startswith('#'):
i.replace(i, repl)
Run Code Online (Sandbox Code Playgroud)
我收到此错误:
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-8-151b36fac37c> in <module>()
1 for i in li:
2 if i.startswith('#'):
----> 3 i.replace(i, repl)
TypeError: Can't convert 'list' object to str implicitly
Run Code Online (Sandbox Code Playgroud) python ×9
python-3.x ×4
numpy ×3
string ×3
arrays ×2
list ×2
indexing ×1
numpy-random ×1
padding ×1
pandas ×1
pytorch ×1
random ×1
regex ×1
replace ×1
scikit-learn ×1