您可以尝试 lstrip
最后三列名称,然后添加第一个列名或 .str.replace
df2.columns = df2.columns[:1].tolist() + df2.columns[1:].str.lstrip("d").tolist()
# or
df2.columns = df2.columns.str.replace('^d(\d.*)', r'\1', regex=True)
这是一个潜在的dplyr解决方案:
library(dplyr)
df <- read.table(text = "ind yyyymm cumperc name
1 202006 0.196 CHILD
2 202006 0.327 WOMAN
3 202006 0.401 MAN
4 202006 0.461 PET
5 202006 0.504 FRIEND
6 202006 0.604 ENEMY
7 202006 0.845 PLACE
8 202006 1.000 ITEM
9 202007 0.157 CHILD
10 202007 0.265 MAN
11 202007 0.369 WOMAN
12 202007 0.459 PET
13 202007 0.494 FRIEND
14 202007 0.519 ENEMY
15 202007 0.766 ITEM
16 202007 1.000 PLACE",
header = TRUE)
df %>%
group_by(yyyymm) %>%
filter(lag(cumperc, default = first(cumperc)) <= .50)
#> # A tibble: 11 × 4
#> # Groups: yyyymm [2]
#> ind yyyymm cumperc name
#> <int> <int> <dbl> <chr>
#> 1 1 202006 0.196 CHILD
#> 2 2 202006 0.327 WOMAN
#> 3 3 202006 0.401 MAN
#> 4 4 202006 0.461 PET
#> 5 5 202006 0.504 FRIEND
#> 6 9 202007 0.157 CHILD
#> 7 10 202007 0.265 MAN
#> 8 11 202007 0.369 WOMAN
#> 9 12 202007 0.459 PET
#> 10 13 202007 0.494 FRIEND
#> 11 14 202007 0.519 ENEMY
那会适合您的用例吗?
我似乎发现了我的问题。问题在于我的一种产品之一出于某种原因,其中包含它的引号,但是一旦删除了OnClick多次工作,同时将产品名称发送到函数以保持跟踪。
给出的答案的问题还在于我不想在按钮本身内部显示所有名称&lt; button class =“ test”&gt; item&lt;/button&gt;
而不是我需要的&lt; button onClick ='func($ {passname})&gt;&lt;/button&gt;
,因此在尝试时不会工作,但它给了我一个未来参考的一般思想。谢谢!
您也可以使用默认为0的get()
newdict = {x: dict1.get(x, 0) + dict2.get(x, 0) for x in set(dict1) | set(dict2)}
我认为您将需要做的一些文档(也许有一些更改以适合您的配置)
httpd docker官方图像文档建议首先导出配置
$ docker run --rm httpd:2.4 cat /usr/local/apache2/conf/httpd.conf > ./apache/my-httpd.conf # Dumping to your apache folder
,然后在编辑后,因为您要复制用于容器的编辑零件。在这种情况下,您将需要告诉Apache包括VHOSTS配置
# Include conf/extra/httpd-vhosts.conf
在您的虚拟主机配置之后,即使不匹配 servername
(您都看不到该服务器)(即使您看不到该服务器的任何请求)(您将看不到该请求) htdocs默认值不再)
docker -compose.yml - 示例
services:
apache:
image: httpd:alpine # Using the image directly since everything is accessible through the volumes
container_name: dev_apache
ports:
- 80:80
volumes:
- ./wordpress:/var/www/html:delegated
- ./apache/my-httpd.conf:/usr/local/apache2/conf/httpd.conf
- ./apache/httpd-vhosts.conf:/usr/local/apache2/conf/extra/httpd-vhosts.conf
编辑
您也需要一个目录指令在/var/var/www/html
目录中授予许可,因为 my-httpd.conf
是拒绝整个服务器文件系统。这样的事情:
<VirtualHost *:80>
<...> Your stuff <...>
<Directory /var/www/html>
Require all granted
</Directory>
</VirtualHost>
看来您在计算机上安装了多个Python版本,并且计算机的默认Python版本应为 Python2
(也许与系统环境变量有关)。您可以使用 py -v
检查版本。
建议您使用虚拟环境,并且在每个虚拟环境中仅保留一个Python版本。当您选择解释器时( ctrl + shift + p - &gt; python:选择解释器),新的终端( ctrl + shift + `)将自动激活虚拟环境。
虚拟环境对您使用解释器和特定软件包的特定版本非常有用。
为了防止这种混乱,开发人员经常为项目创建虚拟环境。虚拟环境是包含特定解释器的副本(或符号链接)的文件夹。当您安装到虚拟环境中时,您安装的任何软件包仅在该子文件夹中安装。然后,当您在该环境中运行Python程序时,您就会知道它仅针对那些特定的软件包。
如何创建虚拟环境:
-
创建虚拟环境
python -m venv&lt; name_of_envirnment&gt;
-
激活虚拟环境
&lt; name_of_envirnment&gt; \ scripts \ activate
官方文档:创建一个虚拟环境
您对 _source
字段是正确的,但是如果您不定义字段的映射,则Elasticsearch会生成默认映射,并使用这些字段的默认参数。您可以阅读有关映射param param 在您的情况下,如果您想搜索字段,则需要成为倒置索引的一部分,并且由 index param param 。
您可以通过定义自己的映射来覆盖这些参数的值(建议否则Elasticsearch将根据您在字段中索引的第一个数据猜测字段类型)。
希望这有帮助并清除您的疑问,简而言之,如果您没有定义映射,则 text
默认情况下可以搜索数据。
您正在问如何将多个对象放在一个数组中,但是所需的输出是字符串对象。因此,我将所需的输出视为实际问题:)
您只需要通过 Datalist
迭代并将值合并到字符串对象中。
StringObject = ''
for(let i=0; i < DataList.length; i++){
if(i===0)
StringObject += DataList[i]
else
StringObject += ',' + DataList[i]
}
console.log(StringObject)
或只是
StringObject = String(DataList)
console.log(StringObject)
使用:
#filter only S columns
df1 = df.filter(like='S')
#compare for greater or equal by division nlargestsum with N and if match replace values
df.update(df1.mask(df1.ge(df['nlargestsum'].div(N), axis=0), df['nlargestsum'], axis=0))
print (df)
S1 S2 S3 S4 S5 S6 nlargestsum
ID
1 0.9 0.0 0.0 0.0 0.0 0.0 0.9
2 0.9 0.9 0.9 0.0 0.0 0.1 0.9
3 0.0 0.0 0.0 0.2 0.6 0.0 0.6
4 1.0 0.0 0.0 0.0 0.0 0.0 1.0
5 0.1 0.1 0.9 0.9 0.0 0.0 0.9
具有接受 const char*
的构造函数。
因此,您要做的就是使用 gcnew
创建一个新的 system :: string
将 const char*
传递为参数(a <代码> char*也可以传递)。
请参阅下面的代码:
System::String^ cts(const char *aa)
{
return gcnew System::String(aa);
}
int main()
{
System::String ^ system_str = cts("abcd");
System::Console::WriteLine(system_str);
return 0;
}
输出:
abcd
在克隆的存储库中首先尝试
git ls-files -eol
(git 2.8(2016年3月) ))
在 git config -global core.autocrlf false
之后也尝试同一克隆。
如果这与EOL相关,则不应有差异,并且 git状态
应该干净。
您可以这样写它:
use num::Complex;
macro_rules! vec_cplx {
($(($re:expr, $im:expr)),*) => {
vec![$(Complex::new($re, $im)),*]
}
}
fn main() {
let v: Vec<Complex<f32>> = vec_cplx![(0.0, -25.0), (0.0, -9.0)];
dbg!(v);
let v: Vec<Complex<f64>> = vec_cplx![(0.0, -25.0), (0.0, -9.0)];
dbg!(v);
}
这将适用于复杂:: new()
接受的任何类型,因为它是宏,并且类型检查在宏扩展后发生。
这是一个简单的解决方案,它使用 fetch()
从这两个URL中获取数据,然后插入所有返回到网页的人和一个星球:
function myFetch(...args) {
return fetch(...args).then(response => {
if (!response.ok) {
throw new Error(`fetch failed with status ${response.status}`);
}
return response.json();
});
}
Promise.all([
myFetch("https://swapi.dev/api/people/"),
myFetch("https://swapi.dev/api/planets/2/")
]).then(([people, planet]) => {
const peopleDiv = document.getElementById("people");
let peopleHTML = "";
for (let p of people.results) {
peopleHTML += `<div>${p.name}</div>`;
}
peopleDiv.innerHTML = peopleHTML;
const planetDiv = document.getElementById("planets");
let planetHTML = `<div>${planet.name}</div>`;
planetDiv.innerHTML = planetHTML;
}).catch(err => {
console.log(err);
});
<div id="people"></div>
<hr>
<div id="planets"></div>
至于使用结果,人们URL返回一个看起来像这样的结构:
{
count: 82,
next: 'https://swapi.dev/api/people/?page=2',
previous: null,
results: [
{
name: 'Luke Skywalker',
height: '172',
mass: '77',
hair_color: 'blond',
skin_color: 'fair',
eye_color: 'blue',
birth_year: '19BBY',
gender: 'male',
homeworld: 'https://swapi.dev/api/planets/1/',
films: [Array],
species: [],
vehicles: [Array],
starships: [Array],
created: '2014-12-09T13:50:51.644000Z',
edited: '2014-12-20T21:17:56.891000Z',
url: 'https://swapi.dev/api/people/1/'
},
{
name: 'C-3PO',
height: '167',
mass: '75',
hair_color: 'n/a',
skin_color: 'gold',
eye_color: 'yellow',
birth_year: '112BBY',
gender: 'n/a',
homeworld: 'https://swapi.dev/api/planets/1/',
films: [Array],
species: [Array],
vehicles: [],
starships: [],
created: '2014-12-10T15:10:51.357000Z',
edited: '2014-12-20T21:17:50.309000Z',
url: 'https://swapi.dev/api/people/2/'
}
}
因此,您有 people.results
,这是一个数组,您可以访问 people.results [n]代码>从该数组中获取项目。 的对象。
该项目将是一个
{
name: 'Alderaan',
rotation_period: '24',
orbital_period: '364',
diameter: '12500',
climate: 'temperate',
gravity: '1 standard',
terrain: 'grasslands, mountains',
surface_water: '40',
population: '2000000000',
residents: [
'https://swapi.dev/api/people/5/',
'https://swapi.dev/api/people/68/',
'https://swapi.dev/api/people/81/'
],
films: [
'https://swapi.dev/api/films/1/',
'https://swapi.dev/api/films/6/'
],
created: '2014-12-10T11:35:48.479000Z',
edited: '2014-12-20T20:58:18.420000Z',
url: 'https://swapi.dev/api/planets/2/'
}
具有 .name
,的属性 您可以在 planet.name
中访问该对象上的属性。
请注意,人们的结果是分页的。总共有82个,但第一个结果只有10个。其余的带有其他页面的结果,例如 https://swapi.dev/api/people/?page=2
。
(
:
)(可订阅[subscriptArg]
)使切片表示法,其中具有可选的参数start ,
stop
和步骤
:Python切片是一种计算快速的方法,可有条不紊地访问数据的一部分。我认为,即使是中级Python程序员,这是必须熟悉的语言的一个方面。
一开始的重要定义
,让我们定义几个术语:
索引的工作原理
您可以使这些正数或负数中的任何一个。正数的含义很简单,但是对于负数,就像python中的索引一样,您从末端倒数 start 和 stop ,而对于< em>步骤,您只需减少索引即可。此示例是从文档的教程中每个索引参考的序列中的哪一项:
切片的工作原理
以使用支持它的序列使用切片符号,您必须在正方形中至少包含一个结肠遵循序列的括号(实际上 >根据Python数据模型。序列的
。
方法 start , stop 和步骤有默认值,因此要访问默认值,只需删除参数即可。
切片符号从列表中获取最后的九个元素(或任何其他支持它的序列,如字符串)看起来像这样:
当我看到此元素时,我将零件读取为“从末端到第9,到结尾。” (实际上,我在心理上缩写为“ -9,on”)
说明:
完整的符号是
并替换默认值(实际上,当
step
为负面时,stop> stop
'默认值为-len(my_list)-1
,所以note
for stop for stop for Stop for Stop实际上只是意味着它可以转到该端步骤的任何一个步骤): colon ,:,是告诉Python您给它一个切片而不是常规索引的原因。这就是为什么在Python 2中制作列表浅副本的惯用方法
与众不同:(
Python 3获取
list.copy.copy
和list.list.clear.clear.clear
methot。 )当
步骤
为负面时,start
和stop
默认情况默认为默认情况(或
无
),将其分配给+1
。但是您可以通过负整数传递,并且从开始到开始,列表(或大多数其他标准切片)将被切成薄片。
因此,负面切片将更改
start
和stop
的默认值!在来源中证实这一点,
我喜欢鼓励用户阅读源以及文档。 slice对象的源代码,此逻辑在这里找到。首先,我们确定
步骤
是负面的:如果是这样,下限为
-1
,则表示我们一直将其切成序列和包括开始,并且上限是长度为负1,这意味着我们在末尾开始。 (请注意,此-1
的语义与-1
的不同是不同的,用户可以在python中传递索引,指示最后一项。)否则,
步骤
为正,下边界将为零,上限(我们要延长但不包括在内)切片列表的长度。然后,我们可能需要应用
start
和stop
的默认值 - 当开始
时,将默认值计算为时的上限。步骤
是负面的:和
停止
,下限:给您的切片一个描述性的名称!
您可能会发现将切片分开从传递到
list .__ getItem __
method(但是,您不能仅仅将某些由结肠隔开的整数分配给变量。您需要使用slice对象:
需要第二个参数, none ,以便将第一个参数解释为
start> start
参数,否则将是stop> stop
参数。然后,您可以将切片对象传递给您的顺序:
有趣的是,范围还需要切片:
内存注意事项:
由于python列表的切片在内存中创建新对象,因此要注意的另一个重要功能IS
itertools.islices.islices.islice
。通常,您需要在切片上迭代,而不仅仅是在内存中静态创建。islice
是完美的。警告,它不支持start
,stop
或步骤
,因此,如果这是问题,则可能需要计算索引或提前扭转峰值。现在:
列表切片制作副本的事实是列表本身的功能。如果您将诸如Pandas DataFrame的高级对象切片,则可能会返回原始视图,而不是副本。
In short, the colons (
:
) in subscript notation (subscriptable[subscriptarg]
) make slice notation, which has the optional argumentsstart
,stop
, andstep
:Python slicing is a computationally fast way to methodically access parts of your data. In my opinion, to be even an intermediate Python programmer, it's one aspect of the language that it is necessary to be familiar with.
Important Definitions
To begin with, let's define a few terms:
How Indexing Works
You can make any of these positive or negative numbers. The meaning of the positive numbers is straightforward, but for negative numbers, just like indexes in Python, you count backwards from the end for the start and stop, and for the step, you simply decrement your index. This example is from the documentation's tutorial, but I've modified it slightly to indicate which item in a sequence each index references:
How Slicing Works
To use slice notation with a sequence that supports it, you must include at least one colon in the square brackets that follow the sequence (which actually implement the
__getitem__
method of the sequence, according to the Python data model.)Slice notation works like this:
And recall that there are defaults for start, stop, and step, so to access the defaults, simply leave out the argument.
Slice notation to get the last nine elements from a list (or any other sequence that supports it, like a string) would look like this:
When I see this, I read the part in the brackets as "9th from the end, to the end." (Actually, I abbreviate it mentally as "-9, on")
Explanation:
The full notation is
and to substitute the defaults (actually when
step
is negative,stop
's default is-len(my_list) - 1
, soNone
for stop really just means it goes to whichever end step takes it to):The colon,
:
, is what tells Python you're giving it a slice and not a regular index. That's why the idiomatic way of making a shallow copy of lists in Python 2 isAnd clearing them is with:
(Python 3 gets a
list.copy
andlist.clear
method.)When
step
is negative, the defaults forstart
andstop
changeBy default, when the
step
argument is empty (orNone
), it is assigned to+1
.But you can pass in a negative integer, and the list (or most other standard sliceables) will be sliced from the end to the beginning.
Thus a negative slice will change the defaults for
start
andstop
!Confirming this in the source
I like to encourage users to read the source as well as the documentation. The source code for slice objects and this logic is found here. First we determine if
step
is negative:If so, the lower bound is
-1
meaning we slice all the way up to and including the beginning, and the upper bound is the length minus 1, meaning we start at the end. (Note that the semantics of this-1
is different from a-1
that users may pass indexes in Python indicating the last item.)Otherwise
step
is positive, and the lower bound will be zero and the upper bound (which we go up to but not including) the length of the sliced list.Then, we may need to apply the defaults for
start
andstop
—the default then forstart
is calculated as the upper bound whenstep
is negative:and
stop
, the lower bound:Give your slices a descriptive name!
You may find it useful to separate forming the slice from passing it to the
list.__getitem__
method (that's what the square brackets do). Even if you're not new to it, it keeps your code more readable so that others that may have to read your code can more readily understand what you're doing.However, you can't just assign some integers separated by colons to a variable. You need to use the slice object:
The second argument,
None
, is required, so that the first argument is interpreted as thestart
argument otherwise it would be thestop
argument.You can then pass the slice object to your sequence:
It's interesting that ranges also take slices:
Memory Considerations:
Since slices of Python lists create new objects in memory, another important function to be aware of is
itertools.islice
. Typically you'll want to iterate over a slice, not just have it created statically in memory.islice
is perfect for this. A caveat, it doesn't support negative arguments tostart
,stop
, orstep
, so if that's an issue you may need to calculate indices or reverse the iterable in advance.and now:
The fact that list slices make a copy is a feature of lists themselves. If you're slicing advanced objects like a Pandas DataFrame, it may return a view on the original, and not a copy.
Python中的切片如何工作