是否可以更改Rstudio中的背景颜色?我知道可以改变编辑器的主题,这是一个非常好的功能,我已经使用,但是可以改变其他窗口的背景颜色(环境,历史,文件等... )除了白色以外的东西和文字更暗的东西?太轻的背景让我头疼.
我尝试将文本对齐方式更改为2个合并销售的中心,我发现了一些对我的案例不起作用的答案
currentCell = ws.cell('A1')
currentCell.style.alignment.horizontal = 'center' #TypeError: cannot set horizontal attribute
#or
currentCell.style.alignment.vertical = Alignment.HORIZONTAL_CENTER #AttributeError: type object 'Alignment' has no attribute 'HORIZONTAL_CENTER'
Run Code Online (Sandbox Code Playgroud)
两者都没有用,还有其他办法吗?
我正在尝试创建一个简单的程序,从文件中删除重复的行.但是,我被卡住了.我的目标是最终删除除1个重复行之外的所有行,与建议的副本不同.所以,我仍然有这些数据.我也想这样做,它采用相同的文件名并输出相同的文件名.当我试图使文件名都相同时,它只输出一个空文件.
input_file = "input.txt"
output_file = "input.txt"
seen_lines = set()
outfile = open(output_file, "w")
for line in open(input_file, "r"):
if line not in seen_lines:
outfile.write(line)
seen_lines.add(line)
outfile.close()
Run Code Online (Sandbox Code Playgroud)
input.txt中
I really love christmas
Keep the change ya filthy animal
Pizza is my fav food
Keep the change ya filthy animal
Did someone say peanut butter?
Did someone say peanut butter?
Keep the change ya filthy animal
Run Code Online (Sandbox Code Playgroud)
预期产出
I really love christmas
Keep the change ya filthy animal
Pizza is my fav …
Run Code Online (Sandbox Code Playgroud) 我想做这样的事情:在循环中,第一次迭代将一些内容写入名为file0.txt的文件,第二次迭代file1.txt等等,只需增加数量即可.
FILE *img;
int k = 0;
while (true)
{
// here we get some data into variable data
file = fopen("file.txt", "wb");
fwrite (data, 1, strlen(data) , file);
fclose(file );
k++;
// here we check some condition so we can return from the loop
}
Run Code Online (Sandbox Code Playgroud) 我正在使用numpy生成一个随机整数的2D数组:
import numpy
arr = numpy.random.randint(16, size = (4, 4))
Run Code Online (Sandbox Code Playgroud)
这只是一个例子.我生成的数组实际上是巨大的,并且大小可变.由于数字总是从0到16,我想节省一些空间并使数组类型uint8
.我尝试了以下内容
arr = numpy.random.randint(16, size = (width, height), dtype = numpy.uint8)
Run Code Online (Sandbox Code Playgroud)
在尝试匹配的行为zeros
和ones
,但我得到了以下错误:
Traceback (most recent call last):
File "<ipython-input-103-966a510df1e7>", line 1, in <module>
maze = numpy.random.randint(16, size = (width, height), dtype = numpy.uint8)
File "mtrand.pyx", line 875, in mtrand.RandomState.randint (numpy/random/mtrand/mtrand.c:9436)
TypeError: randint() got an unexpected keyword argument 'dtype'
Run Code Online (Sandbox Code Playgroud)
文档中randint()
没有提到任何关于能够设置类型的内容.如何创建具有特定整数类型的随机数组?我没有绑定任何一个函数,只是从0到16类型的均匀分布uint8
.
我想使用由夹具动态创建的列表来参数化测试,如下所示:
@pytest.fixture
def my_list_returning_fixture(depends_on_other_fixtures):
return ["a dynamically created list which", depends_on_other_fixtures]
Run Code Online (Sandbox Code Playgroud)
我怎样才能做到这一点?或者,我如何确保首先调用某个夹具- 这将在它发生之前解决这个问题。
我试图用夹具参数化测试(这只会导致错误,因为 python 认为我想交出函数本身):
@pytest.mark.parametrize(
"an_element_from_the_list_of_my_fixture",
my_list_returning_fixture
)
def test_the_list(an_element_from_the_list_of_my_fixture):
print(an_element_from_the_list_of_my_fixture)
Run Code Online (Sandbox Code Playgroud)
像普通函数一样调用夹具my_list_returning_fixture()
也会导致错误!Python 不知道如何填充夹具的“参数”(实际上只是其他夹具)并显示关于传递的参数太少的错误消息......
因此我需要 pytest 来注入depends_on_other_fixtures
依赖项,所以我不能像普通函数一样调用它。
我还尝试在列表夹具和测试之间插入另一个夹具,如下所示:
@pytest.fixture(params=my_list_returning_fixture)
def my_interposed_parametrized_fixture(request):
return request.param
def test_the_list(my_interposed_parametrized_fixture):
...
Run Code Online (Sandbox Code Playgroud)我也尝试使用间接参数化,但它也不起作用......
我知道可以很容易地使用给定的参数对测试进行参数化,如下list
所示:
the_list = [1, 2, 3]
@pytest.mark.parametrize("an_element_from_the_list", the_list)
def test_the_list(an_element_from_the_list):
print(an_element_from_the_list)
Run Code Online (Sandbox Code Playgroud)
这将导致三个测试。列表中的每个元素一个。
我有一些数据,我使用以下代码操作数据帧:
import pandas as pd
import numpy as np
data = pd.DataFrame([[0,0,0,3,6,5,6,1],[1,1,1,3,4,5,2,0],[2,1,0,3,6,5,6,1],[3,0,0,2,9,4,2,1],[4,0,1,3,4,8,1,1],[5,1,1,3,3,5,9,1],[6,1,0,3,3,5,6,1],[7,0,1,3,4,8,9,1]], columns=["id", "sex", "split", "group0Low", "group0High", "group1Low", "group1High", "trim"])
data
#remove all where trim == 0
trimmed = data[(data.trim == 1)]
trimmed
#create df with columns to be split
columns = ['group0Low', 'group0High', 'group1Low', 'group1High']
to_split = trimmed[columns]
to_split
level_group = np.where(to_split.columns.str.contains('0'), 0, 1)
# output: array([0, 0, 1, 1])
level_low_high = np.where(to_split.columns.str.contains('Low'), 'low', 'high')
# output: array(['low', 'high', 'low', 'high'], dtype='<U4')
multi_level_columns = pd.MultiIndex.from_arrays([level_group, level_low_high], names=['group', 'val'])
to_split.columns …
Run Code Online (Sandbox Code Playgroud) 从Python 3.2开始,logging.Logger.setLevel
接受字符串级别,例如'INFO'而不是相应的整数常量.这非常方便,除非您无法以数字方式比较这些级别,而大多数其他日志记录方法只接受整数.如何使用logging
包提供的功能将级别字符串转换为数字级别?具体来说,我想要做到这一点:
>>> logging.???('INFO') == logging.INFO
True
Run Code Online (Sandbox Code Playgroud) 在C中,如果我有:
char *reg = "[R5]";
Run Code Online (Sandbox Code Playgroud)
而且我要
char *reg_alt = "R5"
(相同的东西,但没有括号),我该怎么做?
我试过了
*char reg_alt = reg[1:2];
Run Code Online (Sandbox Code Playgroud)
但这不起作用.
如果我错了,请纠正我.在Java 8中,出于性能原因,当通过"+"运算符连接多个字符串时,调用了StringBuffer.并且"解决"了创建一堆中间字符串对象和污染字符串池的问题.
Java 9怎么样?Invokedynamic增加了一项新功能.还有一个新的类可以更好地解决问题,StringConcatFactory.
String result = "";
List<String> list = Arrays.asList("a", "b", "c");
for (String n : list) {
result+=n;
}
Run Code Online (Sandbox Code Playgroud)
我的问题是:在这个循环中创建了多少个对象?有中介对象吗?我该如何验证呢?