有没有更好的方法从列表中选择两个不同的元素?
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) 我在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声明为包含行的缓存集......
这有什么不对?
在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知道列表没有属性拆分.