小编tea*_*eef的帖子

随机选择列表中的两个元素

有没有更好的方法从列表中选择两个不同的元素?

foo = ['1','a','3','f','ed']

elt1 = random.choice(foo)
elt2 = random.choice(foo)

while elt2 == elt1:
    elt2 = random.choice(foo)
Run Code Online (Sandbox Code Playgroud)

python random

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

在C中比较整数的结果

如果xyintx-y < 0始终返回相同的结果x < y

c

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

在c中使用结构定义

我在C中定义了这样的结构:

struct cache_line{
    char valid;
    int tag;
    int LRU;
};


struct cache_set{
    struct cache_line *lines;
};


struct cache{
    struct cache_set *sets;
};
Run Code Online (Sandbox Code Playgroud)

在这些结构定义之后,我定义了一个函数:

void do_something(struct cache_set *a_cache_set, int number){
            *a_cache_set.lines[next].tag = tag;
            *a_cache_set.lines[next].LRU = *current_count;
            *a_cache_set.lines[next].valid = 1;
}
Run Code Online (Sandbox Code Playgroud)

在main中,我定义了一个cache_set并执行:

struct cache_set a_cache_set = my_cache.sets[setid];
int a_number  = 10;

do_something(&a_cache_set, a_number);
Run Code Online (Sandbox Code Playgroud)

但是,在编译时,我在do_something定义中得到以下错误(at

        *a_cache_set.lines[next].tag = tag;
        *a_cache_set.lines[next].LRU = *current_count;
        *a_cache_set.lines[next].valid = 1;
Run Code Online (Sandbox Code Playgroud)

):

error: request for member ‘lines’ in something not a structure or union
Run Code Online (Sandbox Code Playgroud)

然而,我明确地将a_cache_set声明为包含行的缓存集......

这有什么不对?

c

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

将列表连接到python中的字符串

在Python中将列表"拆分"为字符串的最简单方法是什么?

要从字符串创建列表:

s='1\t2\t3\t4'
Run Code Online (Sandbox Code Playgroud)

我们用:

s.split('\t',-1)
Run Code Online (Sandbox Code Playgroud)

我尝试将split函数用于列表:

l=[1,2,3,4]
Run Code Online (Sandbox Code Playgroud)

得到一个字符串:

s='1\t2\t3\t4'
Run Code Online (Sandbox Code Playgroud)

但我AttributeError知道列表没有属性拆分.

python string list

0
推荐指数
1
解决办法
220
查看次数

标签 统计

c ×2

python ×2

list ×1

random ×1

string ×1