尐籹人

文章 评论 浏览 32

尐籹人 2025-02-21 00:54:40

为此,您可以创建一个服务来共享两个或多个组件之间的数据。

您将需要一个类型的变量作为主体behavior,可以在其中使用variable.next(true/false)从子组件中更改值。

另一方面,将其订阅为variable.asobservable()。subscribe(value => avancar())以侦听父零件的更改。

在这里,您可以检查如何添加和使用服务: https://angular.io/tutorial/tutorial/toh---- pt4

To do that you can create a service to share data between two or more components.

You will need a variable of type as subjectsBehavior where you can change the value with variable.next(true/false) from child component.

On the other hand subscribe it with as variable.asObservable().subscribe(value => avancar()) to listen changes from parent component.

Here you can check how to add and use a service: https://angular.io/tutorial/toh-pt4

如何通过路由器输出来聆听活动?

尐籹人 2025-02-20 02:51:38

基于您的错误消息,请在您的启动JSON 文件中添加以下配置:

            "classPaths": [your_path_here],
            "modulePaths": [your_path_here]

Based on your error message, please add the following configuration to your launchjson file:

            "classPaths": [your_path_here],
            "modulePaths": [your_path_here]

春季+ Gradle在Vscode中找不到类Path [它不是重复的,已经检查了类似的Path]

尐籹人 2025-02-20 02:05:40

正如@codebod所说,您的大部分代码都是多余的。

这是使用三元运营商解决它的一种方法:

public boolean cigarParty(int cigars, boolean isWeekend) {
    return (isWeekend) ? (cigars >= 40): (cigars >= 40 && cigars <= 60);
}

这是在没有三元运营商的情况下解决它的一种方法:

public boolean cigarParty(int cigars, boolean isWeekend) {
    if (isWeekend){
        return (cigars >= 40);
    }else{
        return (cigars >= 40 && cigars <= 60);
    }
}

As @codebod said, much of your code is redundant.

This is one way to solve it with ternary operators:

public boolean cigarParty(int cigars, boolean isWeekend) {
    return (isWeekend) ? (cigars >= 40): (cigars >= 40 && cigars <= 60);
}

This is one way to solve it without ternary operators:

public boolean cigarParty(int cigars, boolean isWeekend) {
    if (isWeekend){
        return (cigars >= 40);
    }else{
        return (cigars >= 40 && cigars <= 60);
    }
}

编码蝙蝠雪茄三元操作员解决方案

尐籹人 2025-02-19 16:24:33
return JSX from the map

    import data from "../data/section1";
    
    const Section1 = () => {
    
    
      return (
        <>
            <div class="mx-20">
                {data.map((vals) => {
                  return (
                    <div class="">
                      <img src={vals.image}/>
                      <p>{vals.text}</p>
                    </div> 
                  )
                })}
            </div>
        </>
      )
    }
    
    export default Section1;
return JSX from the map

    import data from "../data/section1";
    
    const Section1 = () => {
    
    
      return (
        <>
            <div class="mx-20">
                {data.map((vals) => {
                  return (
                    <div class="">
                      <img src={vals.image}/>
                      <p>{vals.text}</p>
                    </div> 
                  )
                })}
            </div>
        </>
      )
    }
    
    export default Section1;

在访问Next.js中映射中的数据对象的值时,要变得不确定

尐籹人 2025-02-19 15:57:07

用以下代码替换您的cumbtbtn onclick。问题是您没有分配最新的答案价值。

submitBtn.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        num = Integer.parseInt(answer.getText().toString());
        if (num == 4){
            Toast.makeText(MainActivity.this, "Answer is Correct!", Toast.LENGTH_SHORT).show();
        }
        else
        {
            Toast.makeText(MainActivity.this, "Answer is Incorrect", Toast.LENGTH_SHORT).show();
        }
    }
});

Replace your submitBtn onclick with below code. Issue is that you are not assigning latest value of answer.

submitBtn.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        num = Integer.parseInt(answer.getText().toString());
        if (num == 4){
            Toast.makeText(MainActivity.this, "Answer is Correct!", Toast.LENGTH_SHORT).show();
        }
        else
        {
            Toast.makeText(MainActivity.this, "Answer is Incorrect", Toast.LENGTH_SHORT).show();
        }
    }
});

仅&#x27; else&#x27;如果其他&#x27;我的Android应用中的声明

尐籹人 2025-02-19 10:36:54

链接到共享库时,请确保使用的符号不是隐藏的。

GCC的默认行为是所有符号都是可见的。但是,当使用选项 -fvisibility = hidden 构建翻译单元时,只有用 __属性标记的函数/符号__((vistibility(vistibility(“ default”))))是外部的由此产生的共享对象。

您可以通过调用来检查您正在寻找的符号是外部的:

# -D shows (global) dynamic symbols that can be used from the outside of XXX.so
nm -D XXX.so | grep MY_SYMBOL 

隐藏/本地符号由 nm 带有小写符号类型,例如 t 而不是`t对于代码段:

nm XXX.so
00000000000005a7 t HIDDEN_SYMBOL
00000000000005f8 T VISIBLE_SYMBOL

您还可以在选项 -c 中使用 nm 来删除名称(如果使用C ++)。

与Windows-dll类似,一个人会用定义标记公共功能,例如 dll_public 定义为:

#define DLL_PUBLIC __attribute__ ((visibility ("default")))

DLL_PUBLIC int my_public_function(){
  ...
}

大致对应于Windows'/msvc-version:

#ifdef BUILDING_DLL
    #define DLL_PUBLIC __declspec(dllexport) 
#else
    #define DLL_PUBLIC __declspec(dllimport) 
#endif

更多


当翻译单元用 -fvisibility = hidden 所产生的符号仍然具有外部链接时(显示 nm >),可以在没有外部链接的情况下使用外部链接问题如果对象文件成为静态库的一部分。仅当将对象文件链接到共享库时,链接才会成为本地。

查找对象文件中的哪些符号隐藏运行:

>>> objdump -t XXXX.o | grep hidden
0000000000000000 g     F .text  000000000000000b .hidden HIDDEN_SYMBOL1
000000000000000b g     F .text  000000000000000b .hidden HIDDEN_SYMBOL2

When linking against shared libraries, make sure that the used symbols are not hidden.

The default behavior of gcc is that all symbols are visible. However, when the translation units are built with option -fvisibility=hidden, only functions/symbols marked with __attribute__ ((visibility ("default"))) are external in the resulting shared object.

You can check whether the symbols your are looking for are external by invoking:

# -D shows (global) dynamic symbols that can be used from the outside of XXX.so
nm -D XXX.so | grep MY_SYMBOL 

the hidden/local symbols are shown by nm with lowercase symbol type, for example t instead of `T for code-section:

nm XXX.so
00000000000005a7 t HIDDEN_SYMBOL
00000000000005f8 T VISIBLE_SYMBOL

You can also use nm with the option -C to demangle the names (if C++ was used).

Similar to Windows-dlls, one would mark public functions with a define, for example DLL_PUBLIC defined as:

#define DLL_PUBLIC __attribute__ ((visibility ("default")))

DLL_PUBLIC int my_public_function(){
  ...
}

Which roughly corresponds to Windows'/MSVC-version:

#ifdef BUILDING_DLL
    #define DLL_PUBLIC __declspec(dllexport) 
#else
    #define DLL_PUBLIC __declspec(dllimport) 
#endif

More information about visibility can be found on the gcc wiki.


When a translation unit is compiled with -fvisibility=hidden the resulting symbols have still external linkage (shown with upper case symbol type by nm) and can be used for external linkage without problem if the object files become part of a static libraries. The linkage becomes local only when the object files are linked into a shared library.

To find which symbols in an object file are hidden run:

>>> objdump -t XXXX.o | grep hidden
0000000000000000 g     F .text  000000000000000b .hidden HIDDEN_SYMBOL1
000000000000000b g     F .text  000000000000000b .hidden HIDDEN_SYMBOL2

什么是未定义的参考/未解决的外部符号错误,我该如何修复?

尐籹人 2025-02-19 04:50:08
.container {
    display: flex;
}
.navProfileShoppingContainer {
    display:flex;
    float: right;
    background-color: #E5B3B3;
    gap : 30px
    justify-content: center;
    border-radius: 10px;
    width: 130px;
    height: 50px;
}
.navLogo {
    font-family: 'Inter';
    font-style: normal;
    font-weight: 500;
    font-size: 36px;
    line-height: 48px;
    color: #CF7E7E;
    
}

.container {
    display: flex;
}
.navProfileShoppingContainer {
    display:flex;
    float: right;
    background-color: #E5B3B3;
    gap : 30px
    justify-content: center;
    border-radius: 10px;
    width: 130px;
    height: 50px;
}
.navLogo {
    font-family: 'Inter';
    font-style: normal;
    font-weight: 500;
    font-size: 36px;
    line-height: 48px;
    color: #CF7E7E;
    
}

有没有办法将Navbar中的三个项目对齐(左,右和中间)?

尐籹人 2025-02-18 18:03:29

是的,事实证明,可以通过更改查询来解决一切。最终请求看起来像这样:

"name" AND ("keyword1" OR "keyword2" OR "keyword3" OR ...)

Yes, it turned out that everything can be solved by changing the query. The final request looks something like this:

"name" AND ("keyword1" OR "keyword2" OR "keyword3" OR ...)

使用Google自定义搜索使用全名作为一个单词

尐籹人 2025-02-18 11:08:06

我创建了一个小提琴,我为每个年度国家对创建 optgroup ,每个optgroup都有两个元素,具体取决于年度和国家/地区的ID(您当然可以更改每个选项 optgroup ,这种选项定义的唯一原因是简化我的测试)。更改 yearge laydatalist 触发 ChangeOptions 的的值,找到这些元素 optgroup 要显示,然后循环optgroup,并隐藏所有与 ID 匹配的内容。将显示匹配的 optgroup ,并且,由于任何先前选择在逻辑上都是无效的,因此我更新 itemselection.value to所选第一个孩子的值 optgroup < /代码>。

最后,我无论如何我都会调用更改,以便甚至在触发任何触发器之前初始化适当的状态。

function changeOptions() {
    let yearRange = document.getElementById("yearRange").value;
    let countrySelect = document.getElementById("countrySelect").value;
    let id = `options-${yearRange}-${countrySelect}`;
    let itemSelection = document.getElementById("itemSelection");
    for (let optgroup of itemSelection.children) {
        let match = optgroup.id === id;
        optgroup[(match ? "remove" : "set") + "Attribute"]("hidden", "hidden");
        if (match) {
            itemSelection.value = optgroup.children[0].value;
        }
    }
}

changeOptions();
<input type="range" id="yearRange" min="0" max="3" list="yearsDatalist" value="3"
onChange="changeOptions()" onInput="changeOptions()">
    <datalist id="yearsDatalist">
        <option value="0" label="1918"></option>
        <option value="1" label="1936"></option>
        <option value="2" label="1950"></option>
        <option value="3" label="1981"></option>
    </datalist>

<select id="countrySelect" onchange="changeOptions()">
    <option value="0">Sweden</option>
    <option value="1">Germany</option>
    <option value="2">USA</option>
</select>

<select id="itemSelection">
    <optgroup id="options-0-0">
        <option value="0-0-1">0-0-1</option>
        <option value="0-0-1">0-0-2</option>
    </optgroup>
    <optgroup id="options-0-1">
        <option value="0-1-1">0-1-1</option>
        <option value="0-1-1">0-1-2</option>
    </optgroup>
    <optgroup id="options-0-2">
        <option value="0-2-1">0-2-1</option>
        <option value="0-2-1">0-2-2</option>
    </optgroup>
    <optgroup id="options-0-3">
        <option value="0-3-1">0-3-1</option>
        <option value="0-3-1">0-3-2</option>
    </optgroup>
    <optgroup id="options-1-0">
        <option value="1-0-1">1-0-1</option>
        <option value="1-0-1">1-0-2</option>
    </optgroup>
    <optgroup id="options-1-1">
        <option value="1-1-1">1-1-1</option>
        <option value="1-1-1">1-1-2</option>
    </optgroup>
    <optgroup id="options-1-2">
        <option value="1-2-1">1-2-1</option>
        <option value="1-2-1">1-2-2</option>
    </optgroup>
    <optgroup id="options-1-3">
        <option value="1-3-1">1-3-1</option>
        <option value="1-3-1">1-3-2</option>
    </optgroup>
    <optgroup id="options-2-0">
        <option value="2-0-1">2-0-1</option>
        <option value="2-0-1">2-0-2</option>
    </optgroup>
    <optgroup id="options-2-1">
        <option value="2-1-1">2-1-1</option>
        <option value="2-1-1">2-1-2</option>
    </optgroup>
    <optgroup id="options-2-2">
        <option value="2-2-1">2-2-1</option>
        <option value="2-2-1">2-2-2</option>
    </optgroup>
    <optgroup id="options-2-3">
        <option value="2-3-1">2-3-1</option>
        <option value="2-3-1">2-3-2</option>
    </optgroup>
    <optgroup id="options-3-0">
        <option value="3-0-1">3-0-1</option>
        <option value="3-0-1">3-0-2</option>
    </optgroup>
    <optgroup id="options-3-1">
        <option value="3-1-1">3-1-1</option>
        <option value="3-1-1">3-1-2</option>
    </optgroup>
    <optgroup id="options-3-2">
        <option value="3-2-1">3-2-1</option>
        <option value="3-2-1">3-2-2</option>
    </optgroup>
    <optgroup id="options-3-3">
        <option value="3-3-1">3-3-1</option>
        <option value="3-3-1">3-3-2</option>
    </optgroup>
</select>

I have created a fiddle where I have created an optgroup for each year-country pair, each optgroup has two elements, depending on the ids of the year and country (you can of course change the options of each optgroup, the lone reason for this kind of option definition was to ease my testing). Changing the value of yearRange or yearsDatalist triggers a call to changeOptions, which finds these elements, defines the id of the optgroup to show, then loops the optgroups and hides everything except the one that matches the id. The matching optgroup will be shown and, since any prior selection is logically invalidated, I update itemSelection.value to the value of the first child of the chosen optgroup.

Finally I call changeOptions anyway, in order to initialize a proper state even before any of the triggers are triggered.

function changeOptions() {
    let yearRange = document.getElementById("yearRange").value;
    let countrySelect = document.getElementById("countrySelect").value;
    let id = `options-${yearRange}-${countrySelect}`;
    let itemSelection = document.getElementById("itemSelection");
    for (let optgroup of itemSelection.children) {
        let match = optgroup.id === id;
        optgroup[(match ? "remove" : "set") + "Attribute"]("hidden", "hidden");
        if (match) {
            itemSelection.value = optgroup.children[0].value;
        }
    }
}

changeOptions();
<input type="range" id="yearRange" min="0" max="3" list="yearsDatalist" value="3"
onChange="changeOptions()" onInput="changeOptions()">
    <datalist id="yearsDatalist">
        <option value="0" label="1918"></option>
        <option value="1" label="1936"></option>
        <option value="2" label="1950"></option>
        <option value="3" label="1981"></option>
    </datalist>

<select id="countrySelect" onchange="changeOptions()">
    <option value="0">Sweden</option>
    <option value="1">Germany</option>
    <option value="2">USA</option>
</select>

<select id="itemSelection">
    <optgroup id="options-0-0">
        <option value="0-0-1">0-0-1</option>
        <option value="0-0-1">0-0-2</option>
    </optgroup>
    <optgroup id="options-0-1">
        <option value="0-1-1">0-1-1</option>
        <option value="0-1-1">0-1-2</option>
    </optgroup>
    <optgroup id="options-0-2">
        <option value="0-2-1">0-2-1</option>
        <option value="0-2-1">0-2-2</option>
    </optgroup>
    <optgroup id="options-0-3">
        <option value="0-3-1">0-3-1</option>
        <option value="0-3-1">0-3-2</option>
    </optgroup>
    <optgroup id="options-1-0">
        <option value="1-0-1">1-0-1</option>
        <option value="1-0-1">1-0-2</option>
    </optgroup>
    <optgroup id="options-1-1">
        <option value="1-1-1">1-1-1</option>
        <option value="1-1-1">1-1-2</option>
    </optgroup>
    <optgroup id="options-1-2">
        <option value="1-2-1">1-2-1</option>
        <option value="1-2-1">1-2-2</option>
    </optgroup>
    <optgroup id="options-1-3">
        <option value="1-3-1">1-3-1</option>
        <option value="1-3-1">1-3-2</option>
    </optgroup>
    <optgroup id="options-2-0">
        <option value="2-0-1">2-0-1</option>
        <option value="2-0-1">2-0-2</option>
    </optgroup>
    <optgroup id="options-2-1">
        <option value="2-1-1">2-1-1</option>
        <option value="2-1-1">2-1-2</option>
    </optgroup>
    <optgroup id="options-2-2">
        <option value="2-2-1">2-2-1</option>
        <option value="2-2-1">2-2-2</option>
    </optgroup>
    <optgroup id="options-2-3">
        <option value="2-3-1">2-3-1</option>
        <option value="2-3-1">2-3-2</option>
    </optgroup>
    <optgroup id="options-3-0">
        <option value="3-0-1">3-0-1</option>
        <option value="3-0-1">3-0-2</option>
    </optgroup>
    <optgroup id="options-3-1">
        <option value="3-1-1">3-1-1</option>
        <option value="3-1-1">3-1-2</option>
    </optgroup>
    <optgroup id="options-3-2">
        <option value="3-2-1">3-2-1</option>
        <option value="3-2-1">3-2-2</option>
    </optgroup>
    <optgroup id="options-3-3">
        <option value="3-3-1">3-3-1</option>
        <option value="3-3-1">3-3-2</option>
    </optgroup>
</select>

多个嵌套选择下拉列表依赖于JavaScript中的输入元素

尐籹人 2025-02-18 08:36:31

我也有类似的问题,您不需要制作全局变量。这就是我解决这个问题的方式,我在制作本地属性之后称之为超级,例如,您可以使用一个助手方法,该方法获取所有不应该是 simpledialog.dialog class的参数。

在帮手内部,您将这些参数分配给类属性,例如 init 方法的左右按钮的文本

def helper(self, b_left, b_right, message):
   self.mes = message
   self.b_left = b_left
   self.b_right = b_right

方法,请确保您调用 self.helper(带有args)在调用super()。 init ()之前,

class MyDialog(simpledialog.Dialog):
  def __init__(self, master, b_left, b_right, message):
    self.helper(b_left, b_right, message) # call it before super! important
    super().__init__(parent=master)

  def helper(self, b_left, b_right, message):
    self.mes = message
    self.b_left = b_left
    self.b_right = b_right

  def body(self, master):
    # do your changes

  def buttonbox(self):
    # do your changes

在制作班级本地属性后,请致电超级。如果您不这样的类,则将从 simpledialog.dialog 类属性接触并引起错误。

I had a similar problem you do not need to make a global variable. This is how I solved this I called the super after I made my local attributes for example you can have a helper method which takes all the arguments which are not supposed to be part of simpledialog.Dialog class.

Inside the helper you assign those arguments to they class attributes like the text for the button on left and right

def helper(self, b_left, b_right, message):
   self.mes = message
   self.b_left = b_left
   self.b_right = b_right

Inside the init method make sure you call the self.helper(with the args) before you call the super().init()

class MyDialog(simpledialog.Dialog):
  def __init__(self, master, b_left, b_right, message):
    self.helper(b_left, b_right, message) # call it before super! important
    super().__init__(parent=master)

  def helper(self, b_left, b_right, message):
    self.mes = message
    self.b_left = b_left
    self.b_right = b_right

  def body(self, master):
    # do your changes

  def buttonbox(self):
    # do your changes

It is very important that you call the super after you make your local attributes for the class. If you don't the class you are inheriting from simpledialog.Dialog class attribute's will interfare and cause errors.

Python/tkinter:按对话框中的确定会破坏其中的信息

尐籹人 2025-02-18 07:58:41

您需要使用 futorbuilder 代替 streambuilder

cream> streambuilder 是一个基于与流的交互的最新快照。

取而代之的是,将流转换为 tolist(tolist()) 方法并在 futurebuilder.future.future.future so:

    ...

    return FutureBuilder<List<QuerySnapshot>>(
      future: _recipesStream.toList(),
      builder: (BuildContext context, snapshot) {

    ...

snapshot.data 现在是 list> list QUERYSNAPSHOT 。因此,它将与迭代上的迭代有所不同:

      ...

      return ListView(
        shrinkWrap: true,
        children: snapshot.data!.map((query) {
          Map<String, dynamic> data = query.docs.first;

      ...

结果将是这样的东西:

“在此处输入图像说明”

You'd need to use FutureBuilder instead of StreamBuilder.

From the docs, the StreamBuilder is a Widget that builds itself based on the latest snapshot of interaction with a Stream. So, only the last item is going to show up on the screen.

Instead, transform the stream to a List with the toList() method and use it on the FutureBuilder.future property like so:

    ...

    return FutureBuilder<List<QuerySnapshot>>(
      future: _recipesStream.toList(),
      builder: (BuildContext context, snapshot) {

    ...

snapshot.data now is a List of QuerySnapshot. So, it's going to be a bit different to iterate over it:

      ...

      return ListView(
        shrinkWrap: true,
        children: snapshot.data!.map((query) {
          Map<String, dynamic> data = query.docs.first;

      ...

The result is going to be something like this:

enter image description here

listView赢得了滚动中的滚动

尐籹人 2025-02-18 06:20:41

首先,传统的射线示踪剂产生的图像总是受噪声的影响。噪声主要来自计算渲染方程的间接闪光部分。回想一下一般渲染方程是:

”方程“

在这里,整体部分与间接照明相对应,并使用重要性采样Algorithm 。这要求我们在相交点的随机方向 p 在随机方向上采样多个射线。这可以通过:

  1. 在每个相交分支的几个方向上,以求解间接照明
  2. 的每个像素的多射线,并在每个相交点处采样随机方向 p
  3. 两者的组合。

现在,传统上,通过用像素对随机方向进行抽样来完成求解。请注意,这不是解决此问题的唯一方法。您还可以渲染高分辨率图像并之后将其下调。

First of al, images generated by a traditional ray tracers are always affected by noise. The noise mainly originates from calculating the indirect illimitation part of the render equation. Recall the general render equation is:

equation

Here, the integral part correspond to the indirect illumination and is is solved using Importance sampling algorithm. Which requires us to sample multiple rays in random directions at an intersection point p. This can be done by:

  1. At every intersection branch in several directions to solve the indirect illumination
  2. Trace multiple rays per pixel and sample random directions at each intersection point p
  3. A combination of both.

Now, solving aliasing is traditionally done by sampling random directions with a pixel. Note, that this is not the only way to solve this problem. You can also render a high resolution image and down-scale it afterwards.

每个像素的采样多射线的重点

尐籹人 2025-02-18 02:54:40

您可以使用 - 无exit 标志来防止执行后退出。有关更多信息,请参见 NPX Cypress运行-Help 。这是问题
示例: NPX柏树运行-NO-EXIT

You can use the --no-exit flag to prevent an exit after execution. See npx cypress run --help for more information. Here is the issue of the feature.
Example: npx cypress run --no-exit

柏树在所有块完成后退出测试执行环境

尐籹人 2025-02-17 10:14:54

选择钥匙的最佳方法是使用唯一标识其兄弟姐妹中列表项目的字符串。

因此,密钥不需要随机:它们只需要在兄弟姐妹中是唯一的。因此,将普通整数用于固定密钥是可以的:

const count = Math.floor(Math.random() * 10);

const dataArray = [...new Array(count).keys()].map(key => ({...data, key}));

From the React docs for Lists and Keys:

The best way to pick a key is to use a string that uniquely identifies a list item among its siblings.

So keys don't need to be random: they just need to be unique amongst siblings. And for that reason, using a plain integer for a fixed key is just fine:

const count = Math.floor(Math.random() * 10);

const dataArray = [...new Array(count).keys()].map(key => ({...data, key}));

JavaScript如何在将被推入数组的对象上创建唯一的ID

尐籹人 2025-02-17 03:15:55
share_dict = {0: 0, 500: 0.1, 1000: 0.2, 1500: 0.5, 2000: 0.75, 2500: 1}  # map for Rate1


def rates(key_list, total):  # recursive function to return Rate1 and Rate shares
    key_list = sorted(key_list)
    if total >= key_list[-1]:
        return share_dict[key_list[-1]], 1 - share_dict[key_list[-1]]
    return rates(key_list[:-1], total)


# creating new calculated column with lambda
merged['calculated'] = merged.apply(lambda x: x['Rate1'] * rates(share_dict.keys(), x['Quantity Total'])[0] +
                                              x['Rate'] * rates(share_dict.keys(), x['Quantity Total'])[1], axis=1)

输出:

Rate  Rate1  Quantity Total  calculated
2.5    1.5            3527        1.50
10.0   13.0               0       10.00
12.5    7.5            2190        8.75
11.5    0.5             967       10.40
4.5    0.5             516        4.10
9.0    5.5              20        9.00
share_dict = {0: 0, 500: 0.1, 1000: 0.2, 1500: 0.5, 2000: 0.75, 2500: 1}  # map for Rate1


def rates(key_list, total):  # recursive function to return Rate1 and Rate shares
    key_list = sorted(key_list)
    if total >= key_list[-1]:
        return share_dict[key_list[-1]], 1 - share_dict[key_list[-1]]
    return rates(key_list[:-1], total)


# creating new calculated column with lambda
merged['calculated'] = merged.apply(lambda x: x['Rate1'] * rates(share_dict.keys(), x['Quantity Total'])[0] +
                                              x['Rate'] * rates(share_dict.keys(), x['Quantity Total'])[1], axis=1)

output:

Rate  Rate1  Quantity Total  calculated
2.5    1.5            3527        1.50
10.0   13.0               0       10.00
12.5    7.5            2190        8.75
11.5    0.5             967       10.40
4.5    0.5             516        4.10
9.0    5.5              20        9.00

numpy“其中”在多个条件下添加pandas dataframe

更多

推荐作者

櫻之舞

文章 0 评论 0

弥枳

文章 0 评论 0

m2429

文章 0 评论 0

野却迷人

文章 0 评论 0

我怀念的。

文章 0 评论 0

更多

友情链接

    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文