我没有Python经验,我经常编写(简化)代码如下:
accumulationList = []
for x in originalList:
y = doSomething(x)
accumulationList.append(y)
return accumulationList
Run Code Online (Sandbox Code Playgroud)
然后在我的测试通过后,我重构了
return [doSomething(x) for x in originalList]
Run Code Online (Sandbox Code Playgroud)
但是假设结果有点不同,我的循环看起来像这样:
accumulationList = []
for x in originalList:
y = doSomething(x)
accumulationList.extend(y)
return accumulationList
Run Code Online (Sandbox Code Playgroud)
在doSomething列表返回一个列表.什么是最恐怖的方式来实现这一目标?显然,之前的列表理解会给出一个列表列表.
我有一个操作,我想为数据帧的每一行运行,更改一列.我是一个apply/ddply/sqldf人,但是当它们有意义时我会使用循环,我认为这是其中之一.这种情况很棘手,因为要更改的列取决于按行更改的信息; 根据一个单元格中的信息,我应该只更改该行中的十个其他单元格中的一个.对于75列和20000行,操作需要10分钟,当我的脚本中的每个其他操作需要0-5秒,最多10秒.我已经将问题解决了下面非常简单的测试用例.
n <- 20000
t.df <- data.frame(matrix(1:5000, ncol=10, nrow=n) )
system.time(
for (i in 1:nrow(t.df)) {
t.df[i,(t.df[i,1]%%10 + 1)] <- 99
}
)
Run Code Online (Sandbox Code Playgroud)
这需要70秒,十列,当ncol = 50时需要360.太疯狂了.循环是错误的方法吗?有没有更好,更有效的方法来做到这一点?
我已经尝试将嵌套术语(t.df [i,1] %% 10 + 1)初始化为for循环外的列表.它节省了大约30秒(10分钟内),但使上面的示例代码更加复杂.所以它有所帮助,但它不是解决方案.
在准备这个测试用例时,我目前最好的想法来了.对我来说,只有10列是相关的(75-11列是无关紧要的).由于运行时间在很大程度上取决于列数,因此我可以在排除不相关列的数据框上运行上述操作.那会让我失望一分钟.但是"使用嵌套索引进行循环"甚至是考虑我的问题的最佳方式吗?
我正在寻找一种方法来对嵌套的对象数组进行排序.这是一个例子:
answers: [
{name: 'paul', state: 'RU'},
{name: 'steve', state: 'US'},
{name: 'mike', state: 'DE'},
...
]
Run Code Online (Sandbox Code Playgroud)
假设现在我要找到所有name的中,answers阵列,但在方兴未艾的顺序排序的话,我怎么能做到呢?
是否有一种简单的方法(即函数)来确定列表中的嵌套级别?我知道有str哪些可以用来获取这些信息.但有什么东西可以简单地回馈结果吗?我可以使用这样的函数来获取所有级别的alist(递归)的名称吗?
我有两个制表符分隔的文件,我需要测试第一个文件中的每一行与另一个文件中的所有行.例如,
文件1:
row1 c1 36 345 A
row2 c3 36 9949 B
row3 c4 36 858 C
Run Code Online (Sandbox Code Playgroud)
文件2:
row1 c1 3455 3800
row2 c3 6784 7843
row3 c3 10564 99302
row4 c5 1405 1563
Run Code Online (Sandbox Code Playgroud)
假设我想输出(file1)中的所有行,其中file1的col [3]小于file2的任何(不是每个)col [2],因为col [1]是相同的.
预期产量:
row1 c1 36 345 A
row2 c3 36 9949 B
Run Code Online (Sandbox Code Playgroud)
由于我在Ubuntu工作,我希望输入命令看起来像这样:
python code.py [file1] [file2] > [output]
我写了以下代码:
import sys
filename1 = sys.argv[1]
filename2 = sys.argv[2]
file1 = open(filename1, 'r')
file2 = open(filename2, 'r')
done = False
for x in file1.readlines(): …Run Code Online (Sandbox Code Playgroud) 使用简单的字典,如:
myDict{'key1':1, 'key2':2}
Run Code Online (Sandbox Code Playgroud)
我可以安全地使用:
print myDict.get('key3')
Run Code Online (Sandbox Code Playgroud)
即使'key3'不存在也不会抛出错误,因为.get()仍会返回None.
现在,我将如何使用嵌套键字典实现相同的简单性:
myDict={}
myDict['key1'] = {'attr1':1,'attr2':2}
Run Code Online (Sandbox Code Playgroud)
以下将给出一个KeyError:
print myDict.get('key1')['attr3']
Run Code Online (Sandbox Code Playgroud)
这将通过:
print myDict.get('key1').get('attr3')
Run Code Online (Sandbox Code Playgroud)
但它会因adn AttributeError而失败:'NoneType'对象没有属性'get':
print myDict.get('key3').get('attr1')
Run Code Online (Sandbox Code Playgroud) 我试图用数组更新文档中的字段.我想在"产品"字段中添加一个数组.我试过这个:
POST /index/type/1/_update
{
"doc" :{
"products": [
{
"name": "A",
"count": 1
},
{
"name": "B",
"count": 2
},
{
"name": "c",
"count": 3
}
]
}
}
Run Code Online (Sandbox Code Playgroud)
这是我尝试运行代码时得到的错误响应:
{
"error": {
"root_cause": [
{
"type": "mapper_parsing_exception",
"reason": "failed to parse [products]"
}
],
"type": "mapper_parsing_exception",
"reason": "failed to parse [products]",
"caused_by": {
"type": "illegal_state_exception",
"reason": "Can't get text on a START_OBJECT at 1:2073"
}
},
"status": 400
}
Run Code Online (Sandbox Code Playgroud)
谁知道我做错了什么?
任何人都知道使 GoogleFinance 真正起作用的解决方法吗?它可以工作一段时间,所以我的公式没有问题,但随后显示股票报价的单元格会定期突然显示“#N/A”,如果您将鼠标悬停在它们上方,则表明 GoogleFinance 遇到了内部错误。真的很坑 在任一方向更改刷新间隔都无济于事。任何人都知道避免错误的解决方法,并让电子表格简单地显示(并继续显示)没有错误的股票报价数据?
我有一个像这样的字符串:
a;b;c;d;e
f;g;h;i;j
1;2;3;4;5
Run Code Online (Sandbox Code Playgroud)
我想逐个元素地解析它.我使用嵌套的strtok函数,但它只是拆分第一行并使令牌指针为null.我怎么能克服这个?这是代码:
token = strtok(str, "\n");
while(token != NULL && *token != EOF)
{
char a[128], b[128];
strcpy(a,token);
strcpy(b,a);
printf("a:%s\n",a);
char *token2 = strtok(a,";");
while(token2 != NULL)
{
printf("token2 %s\n",token2);
token2 = strtok(NULL,";");
}
strcpy(token,b);
token = strtok(NULL, "\n");
if(token == NULL)
{
printf("its null");
}
}
Run Code Online (Sandbox Code Playgroud)
输出:
token 2 a
token 2 b
token 2 c
token 2 d
token 2 e
Run Code Online (Sandbox Code Playgroud) 我有一个多维数组,我试图找出如何简单地"回显"数组的元素.数组的深度未知,因此可以深度嵌套.
在下面的数组的情况下,回显的正确顺序是:
This is a parent comment
This is a child comment
This is the 2nd child comment
This is another parent comment
Run Code Online (Sandbox Code Playgroud)
这是我正在谈论的数组:
Array
(
[0] => Array
(
[comment_id] => 1
[comment_content] => This is a parent comment
[child] => Array
(
[0] => Array
(
[comment_id] => 3
[comment_content] => This is a child comment
[child] => Array
(
[0] => Array
(
[comment_id] => 4
[comment_content] => This is the 2nd child comment
[child] => Array …Run Code Online (Sandbox Code Playgroud)