标签: subscript

如何修剪Python中的字符?

对Python来说很新,并且有一个非常简单的问题.我想修剪字符串中的最后3个字符.这样做的有效方法是什么?

例子 I am going变成了I am go

python string subscript

3
推荐指数
3
解决办法
4544
查看次数

重载特定读写操作的下标运算符

我目前正在尝试为读取和写入操作重载“[]”运算符。我创建了它们,如下所示:

V operator[] (K key) const; //Read
V& operator[] (K key);      //Write
Run Code Online (Sandbox Code Playgroud)

但是,仅从以下两个调用“写入”:

foo["test"] = "bar"; //Correct, will use 'write'
cout << foo["test"]; //Incorrect, will use 'write'
Run Code Online (Sandbox Code Playgroud)

这是什么原因,是否有可能的解决方案?

同样没有帮助的问题,在这里找到:C++: Overloading the [ ] operator for read and write access

尽管如此,所提供的解决方案并未按预期工作,并且仍然只访问了写重载。

c++ overloading subscript

3
推荐指数
1
解决办法
588
查看次数

如何使用python docx在上标或下标中添加文本

在 python docx 快速入门指南 ( https://python-docx.readthedocs.io/en/latest/ ) 中,您可以看到可以使用 add_run-command 并向句子添加粗体文本。

document = Document()
document.add_heading('Document Title', 0)
p = document.add_paragraph('A plain paragraph having some ')
p.add_run('bold').bold = True
Run Code Online (Sandbox Code Playgroud)

我会使用相同的 add_run-command 而是添加带有上标或下标的文本。

这有可能实现吗?

非常感谢任何帮助!

/V

python unicode docx superscript subscript

3
推荐指数
1
解决办法
3701
查看次数

在 Swift 中为 Collection 下标中的 Index 创建变异集

我正在尝试修改此扩展以获得可变性:

extension Collection where Element: Equatable {
    
    /// Returns the element at the specified index iff it is within count, otherwise nil.
    subscript (safe index: Index) -> Element? {
            indices.contains(index) ? self[index] : nil
    }
}
Run Code Online (Sandbox Code Playgroud)

我想实现这个工作:

var array = ["Hello", "World"]
array[safe: 5] = "Other word" // This will not be setted because index is out of bounds, but error won't be throwed.
Run Code Online (Sandbox Code Playgroud)

当我尝试修改下标扩展名时...

extension Collection where Element: Equatable {
    
    /// Returns the element at the specified index iff …
Run Code Online (Sandbox Code Playgroud)

collections subscript swift

3
推荐指数
1
解决办法
683
查看次数

ggplot2轴文本标签:下标+上标+方括号

我试图在 Ggplot2 y 轴中使用下标+上标+方括号。但是当我使用下面的代码时,它显示错误。

labs(y = parse(text = "S[in] ~~ group('[', W * ~~ m^-2, ']')"))
Run Code Online (Sandbox Code Playgroud)

错误是:

Error in parse(text = "S[in] ~~ group('[', W * ~~ m^-2, ']')") : 
  <text>:1:3: unexpected 'in'
1: S[in
      ^
Run Code Online (Sandbox Code Playgroud)

我需要一个标签:Sin [W m-2],其中 in 是下标,-2 是上标。

有人可以帮忙解决这个问题吗?

r superscript subscript ggplot2

3
推荐指数
1
解决办法
4747
查看次数

在django模板中订阅字符串

是否可以在django模板中下标字符串

我的意思是做什么

print "hello"[:3]
=> hel
Run Code Online (Sandbox Code Playgroud)

django模板中.

写作{{ stringVar[:10] }}抛出

TemplateSyntaxError: Could not parse the remainder: '[:10]'  from 'stringVar[:10]'
Run Code Online (Sandbox Code Playgroud)

python string django django-templates subscript

2
推荐指数
1
解决办法
184
查看次数

数据帧的r下标,带有矢量的条件值

这似乎相当容易,但它让我忙碌了一段时间.

我有一个带有n列的数据帧(df)和一个具有相同数量(n)值的向量.

向量中的值是数据帧中列中观察值的阈值.那么线索是,如何告诉R为每列使用不同的阈值?

我想保留数据框中的所有观察结果,这些观察结果满足每列的各种阈值(上面或下面,在示例中无关紧要).不满足阈值标准的观测值应设置为0.

我不想要数据帧的子集.

有人可以帮忙吗?非常感谢提前.

r subscript dataframe

2
推荐指数
1
解决办法
1741
查看次数

向量下标无法添加元素

在使用子脚本操作符编写一些非常简洁的C++之后,我在程序中出现了一个小错误 - 没有输出.

我输入这个(Linux)

54 73 89 43 38 90
Run Code Online (Sandbox Code Playgroud)

然后点击Cntrl + D进行EOF.程序不输出任何内容并停止执行.

资源:

#include <iostream>
#include <string>
#include <vector>

using std::cin;
using std::vector;
using std::cout;
using std::endl;

int main() {
vector<unsigned> scores(11, 0); //11 buckets, all initially 0
unsigned grade;
while(cin >> grade) //read the grades
{
   if(grade <=100) //handles only valid inputs
  {
   ++scores[grade/10]; //increment counter for the current cluster
  }
 }
}
Run Code Online (Sandbox Code Playgroud)

我没有在VIM中更改我的设置,因此编码风格略有偏差.我无法想象有什么问题,while循环非常标准.它会读入成绩,直到找到流无效为止.然后我检查输入是否小于100(含).最后一段代码(它非常简洁)在向量中找到正确的元素来递增计数器.

我有一种感觉,它可能是我的输入导致程序无法输出.

编辑1:我添加了输出语句,我通过使用dereferencing a来做到这一点,它始终是一个引用.

#include <iostream>
#include <string>
#include <vector>

using std::cin;
using std::vector;
using …
Run Code Online (Sandbox Code Playgroud)

c++ vector cin subscript

2
推荐指数
1
解决办法
181
查看次数

Javascript - 将字符串中的所有数字更改为下标

我有一个输入框,用户在其中输入化学式.我在表中将它们的输入显示回来,我的javascript文件中包含以下代码.

document.getElementById("entered").innerHTML = userIn;
Run Code Online (Sandbox Code Playgroud)

..."userIn"是输入框的"id","entered"是表格行中数据的id.

现在,这是我的问题:

有什么方法可以将公式中的所有数字更改为下标,但保留字母不变?那么如果输入H2O2,只有2个成为下标?

html javascript innerhtml subscript

2
推荐指数
1
解决办法
668
查看次数

将NSDictionary转换为Dictionary Swift

我已经看到这在其他问题中得到了解决 - 但我认为因为这个NSDictionary是通过下标访问的,所以它会抛出一些错误.

func pickRandomRecipe(arrayOfRecipes: NSArray) -> Dictionary<String,Any> {
let randomRecipeIndex = Int(arc4random_uniform(UInt32(arrayOfRecipes.count)))

//Could not cast value of type '__NSDictionaryI' (0x7fbfc4ce0208) to 'Swift.Dictionary<Swift.String, protocol<>>' (0x7fbfc4e44358)
let randomRecipe: Dictionary = arrayOfRecipes[randomRecipeIndex] as! Dictionary<String,Any>
return randomRecipe
}
Run Code Online (Sandbox Code Playgroud)

dictionary casting subscript ios swift

2
推荐指数
1
解决办法
6113
查看次数