Python提供字符串格式
s="{a}|{b}.{c}.{a}"
s.format(a=2, b=3, c=4)
Run Code Online (Sandbox Code Playgroud)
哪个输出
'2|3.4.2'
Run Code Online (Sandbox Code Playgroud)
我正在寻找一种方法来获取字符串中的"变量"列表.
所以在我的例子中
list_of_var(s)
Run Code Online (Sandbox Code Playgroud)
应该输出
['a', 'b', 'c', 'a']
Run Code Online (Sandbox Code Playgroud) 我正在寻找一种使用Python Pandas在没有窗口的情况下滚动(*)分钟的方法。
让我们考虑以下 Series
In [26]: s = pd.Series([10, 12, 14, 9, 10, 8, 16, 20])
Out[26]:
0 10
1 12
2 14
3 9
4 10
5 8
6 16
7 20
dtype: int64
Run Code Online (Sandbox Code Playgroud)
我想要一个像
0 10
1 10
2 10
3 9
4 9
5 8
6 8
7 8
dtype: int64
Run Code Online (Sandbox Code Playgroud)
我试过了
s.rolling().min()
Run Code Online (Sandbox Code Playgroud)
但出现以下错误
TypeError: rolling() missing 1 required positional argument: 'window'
Run Code Online (Sandbox Code Playgroud)
我做了这个
r = s.copy()
val_min = r.iloc[0]
for i, (idx, val) in enumerate(r.iteritems()):
if i …Run Code Online (Sandbox Code Playgroud) 我正在寻找一种无限重复序列的方法。类似的东西
julia> repeat(1:3, outer=2)
9-element Array{Int64,1}:
1
2
3
1
2
3
Run Code Online (Sandbox Code Playgroud)
但外部是无限的,结果是迭代器(不是数组)
我试过
for i in repeatedly([1:3])
@show i
end
Run Code Online (Sandbox Code Playgroud)
重复使用IterTools 但会引发错误。
我正在寻找一种方式来获得(使用Python)(即C型整数最大值和最小值uint8,int8,uint16,int16,uint32,int32,uint64,int64从Python的...)。
我期待在ctypes模块中找到它
In [1]: import ctypes
In [2]: ctypes.c_uint8(256)
Out[2]: c_ubyte(0)
In [3]: ctypes.c_uint8(-1)
Out[3]: c_ubyte(255)
Run Code Online (Sandbox Code Playgroud)
但我找不到
朱莉娅(Julia)具有出色的功能:
julia> typemax(UInt8)
0xff
julia> typemin(UInt8)
0x00
julia> typemin(Int8)
-128
julia> typemax(Int8)
127
Run Code Online (Sandbox Code Playgroud)
我很确定Python具有类似的功能。
理想情况下,我什至在寻找一种方法来确保可以将给定的Python整数(据说是无界的)安全地转换为给定大小的C类型整数。如果数字不在预期的间隔内,则应引发异常。
当前溢出不会引发异常:
In [4]: ctypes.c_uint8(256)
Out[4]: c_ubyte(0)
Run Code Online (Sandbox Code Playgroud)
我看到了这样的整数值的最大值和最小值,但是由于作者正在寻找Python整数的最小值/最大值,所以有点不同……而不是C整数(来自Python)
我还注意到在python中检测C类型限制(“ limits.h”)?但是,即使它很相关,也不能真正回答我的问题。
我有熊猫系列s = pd.Series({1: 10, 2:11, 4:5, 7:10})
1 10
2 11
4 5
7 10
dtype: int64
Run Code Online (Sandbox Code Playgroud)
我想重新采样这个系列以获得这样的系列
1 10
2 11
3 0
4 5
5 0
6 0
7 10
dtype: int64
Run Code Online (Sandbox Code Playgroud) Python Pandas DataFrame可以具有分层索引(MultiIndex)或分层列.
我正在寻找一种方法来了解索引和列的级别(深度).
len(df.index.levels)
Run Code Online (Sandbox Code Playgroud)
似乎只适用于MultiIndex,但它不适用于普通索引.
是否有一个属性(这将适用MultiIndex但也很简单Index)?
df.index.depth
Run Code Online (Sandbox Code Playgroud)
要么
df.columns.depth
Run Code Online (Sandbox Code Playgroud)
会很棒.
MultiIndex列和索引的一个示例:
import pandas as pd
import numpy as np
def mklbl(prefix,n):
return ["%s%s" % (prefix,i) for i in range(n)]
def mi_sample():
miindex = pd.MultiIndex.from_product([mklbl('A',4),
mklbl('B',2),
mklbl('C',4),
mklbl('D',2)])
micolumns = pd.MultiIndex.from_tuples([('a','foo'),('a','bar'),
('b','foo'),('b','bah')],
names=['lvl0', 'lvl1'])
dfmi = pd.DataFrame(np.arange(len(miindex)*len(micolumns)).reshape((len(miindex),len(micolumns))),
index=miindex,
columns=micolumns).sortlevel().sortlevel(axis=1)
return(dfmi)
df = mi_sample()
Run Code Online (Sandbox Code Playgroud)
所以df看起来像:
lvl0 a b
lvl1 bar foo bah foo
A0 B0 C0 D0 1 0 3 2
D1 5 4 …Run Code Online (Sandbox Code Playgroud) 在一些Julia代码中何时可以看到条件表达式,例如
if val !== nothing
dosomething()
end
Run Code Online (Sandbox Code Playgroud)
val类型变量在哪里Union{Int,Nothing}
条件val !== nothing和之间有什么区别val != nothing?
我是 Modelica 的新手。
我使用 OpenModelica 连接编辑器完成了此操作。
用下面的代码
model chariot_portique
Modelica.Blocks.Sources.CombiTimeTable combiTimeTable(extrapolation = Modelica.Blocks.Types.Extrapolation.HoldLastPoint, table = [0, 0; 4, 3; 8, 3; 12, 0; 16, 0; 20, -3; 24, -3; 28, 0], tableOnFile = false) annotation(
Placement(transformation(extent = {{-80, 20}, {-60, 40}})));
Modelica.Blocks.Continuous.Derivative derivative(T = 1, k = 1) annotation(
Placement(visible = true, transformation(origin = {-26, 30}, extent = {{-10, -10}, {10, 10}}, rotation = 0)));
equation
connect(combiTimeTable.y, derivative.u) annotation(
Line(points = {{-58, 30}, {-38, 30}}, color = {0, 0, 127})); …Run Code Online (Sandbox Code Playgroud) python ×5
pandas ×3
julia ×2
accumulate ×1
comparison ×1
ctypes ×1
derivative ×1
generator ×1
integer ×1
iterator ×1
max ×1
min ×1
modelica ×1
nothing ×1
openmodelica ×1
regex ×1
size ×1
string ×1