我有一个大约17,000键的字典.我想一次选择一个键 - 哪个键无关紧要,我不需要以任何特定顺序发生(随机就好了).但是,在我选择一个键之后,我会在选择另一个键之前更改字典,可能是通过添加或删除键.因此,我没有可以迭代的密钥集.
由于我不需要以任何特定顺序访问它们,我可以每次将dict键转换为列表,然后弹出第一个元素.但是,由于有17,000个键,因此在每次迭代时制作一个列表大约需要0.0005-7秒,这将花费我太多的时间来满足我的需要.有没有我可以采取的快捷方式,以便每次我想选择一个密钥时,我不必用dict键编译一个庞大的列表?
试图将.tsv转换为.csv.这个:
import csv
# read tab-delimited file
with open('DataS1_interactome.tsv','rb') as fin:
cr = csv.reader(fin, delimiter='\t')
filecontents = [line for line in cr]
# write comma-delimited file (comma is the default delimiter)
with open('interactome.csv','wb') as fou:
cw = csv.writer(fou, quotechar='', quoting=csv.QUOTE_NONE)
cw.writerows(filecontents)
Run Code Online (Sandbox Code Playgroud)
给我这个错误:
File "tsv2csv.py", line 11, in <module>
cw.writerows(filecontents)
_csv.Error: need to escape, but no escapechar set
Run Code Online (Sandbox Code Playgroud) // set all values in the hash table to null
for(int i = 0; i < HASH_SIZE; i++)
{
hashtable[i] = NULL;
}
Run Code Online (Sandbox Code Playgroud)
我一直收到此错误消息以响应hashtable [i]:
赋值在没有强制转换的情况下从指针生成整数[-Werror]
为什么?
试图生成一个伪随机浮点数 0 和 1。运行代码时出现此错误:
'RAND_MAX' 未声明(首次在此函数中使用)
#include <stdio.h>
#include <time.h>
int main(test, numpoints, numtrials, dimension){
//generate a random number
srand(time(NULL));
float r = (float)(rand()/RAND_MAX +1);
printf("%.6f", r);
printf("\n");
}
Run Code Online (Sandbox Code Playgroud) switch(ch)
{
//input a number
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
if(check_original())
{
int y = g.y;
int x = g.x;
g.board[g.y][g.x] = ch - '0';
draw_numbers();
g.y = y;
g.x = x;
show_cursor();
}
// delete an input from the board
case '0':
case KEY_BACKSPACE:
case KEY_DC:
if(check_original())
{
int y = g.y;
int x = g.x;
g.board[y][x] = 0;
draw_numbers();
g.y = y;
g.x = x; …Run Code Online (Sandbox Code Playgroud) 我读过“梅森扭曲器的计算复杂度是 O(p 2 ),其中 p 是多项式的次数”。
for(int a = 0, b = 1; b < n; a++; b++)
{
if (compare(values[a], values[b]))
counter++;
else
{
int x = values[a];
values[a] = values[b];
values[b] = x;
}
}
Run Code Online (Sandbox Code Playgroud)
当我尝试编译时,第一行[for(int ...])出现此错误:
helpers.c:68:41: error: expected ')' before ';' token
Run Code Online (Sandbox Code Playgroud)
为什么我需要添加另一个')'?
有没有办法简化这个功能?具体来说,我想用更少的缩进行重写它.
# split string (first argument) at location of separators (second argument, should be a string)
def split_string(text, separators):
text = ' ' + text + ' '
words = []
word = ""
for char in text:
if char not in separators:
word += char
else:
if word:
words.append(word)
word = ""
if not words:
words.append(text)
return words
Run Code Online (Sandbox Code Playgroud) 由于Python没有指针,我想知道如何将对象的引用传递给函数而不是复制整个对象.这是一个非常人为的例子,但我说我正在写一个这样的函数:
def some_function(x):
c = x/2 + 47
return c
y = 4
z = 12
print some_function(y)
print some_function(z)
Run Code Online (Sandbox Code Playgroud)
根据我的理解,当我调用some_function(y)时,Python会分配新的空间来存储参数值,然后在函数返回c并且不再需要它时删除这些数据.由于我实际上并没有改变some_function中的参数,我怎样才能简单地从函数中引用y而不是在我传递时复制y?在这种情况下,它并不重要,但如果y非常大(比如一个巨大的矩阵),复制它可能会占用一些重要的时间和空间.
在for for循环中(即:for(len ...和for(wid ...),我收到相同的错误消息:
错误:预期';' 在')'令牌之前
void
init(void)
{
//fills board up with numbers
int tile = (d*d - 1);
int len = 0;
int wid = 0;
for(len < d; len++)
{
for(wid < d; wid++)
{
board[len][wid] = tile;
tile--;
}
}
}
Run Code Online (Sandbox Code Playgroud)
很抱歉像以前一样问一个类似的问题,但我是一个非常困惑的新手!
// get user's input
int ch = getch();
switch (ch)
{
//input a number
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
{
int i = atoi(ch);
g.board[g.y][g.x] = i;
}
}
Run Code Online (Sandbox Code Playgroud)
在我添加的代码中,ch被声明为int.但是,函数getch将输入保存为字符串,对吗?如何将字符串ch转换为int,以便我可以使用它?我试图使用atoi()函数,但我不断收到这些错误消息.
sudoku.c: In function 'main':
sudoku.c:247:17: error: passing argument 1 of 'atoi' makes pointer from integer without a cast [-Werror]
/usr/include/stdlib.h:148:12: note: expected 'const char *' but argument is of type 'int'
sudoku.c:252:17: error: expected ';' before 'g' …Run Code Online (Sandbox Code Playgroud) // a cursor variable, for positioning purposes
int cursor = 0;
// declare a counter
int counter = 0;
// start a loop
while (counter <= 0)
{
// get the cursor positioned correctly
fseek(fp, cursor, SEEK_SET);
// read the file and search for the jpeg key
JPG_KEY key;
fread(&key, sizeof(JPG_KEY), 4, fp);
// check the key to see if you are at the start of a jpeg
if( check_jpg_key(key) )
counter++;
cursor++;
}
Run Code Online (Sandbox Code Playgroud)
出于某种原因,我的"光标"和"计数器"变量在这个程序的中间跳转到可笑的高位,而不是在每个循环中递增1.使用gdb,我发现光标的值从0跳到2099202,并且计数器的值在此行从0跳转到3419700:fread(&key,sizeof(JPG_KEY),4,fp);
为什么?