2015-11-09 180 views
0

我需要一些帮助来解决我的问题与python。我的任务是生成我的列表“石头”的4个元素,我想把它放在我的列表“L”中。这些字母是颜色和+, - ,..是形式,我可以使用每种颜色,并在我的4个元素中形成一次,这就是为什么我要与这个任务斗争。到目前为止我的代码:比较字符串列表元素python

L = [] 
stones = ["R+", "R-", "R*", "R_", "G+", "G-", "G*", "G_", "B+", "B-", "B*", "B_", "W+", "W-", "W*", "W_"] 
stone1 = random.choice(stones) 
L.append(stone1) 
stones.remove(stone1) 
#if stone1[1] in stones: 
#del stones 

,你可以看到希望,我得到的“石头”的随机值,可以把它放在我的名单L和我删除它从石头名单。现在的问题是,我不知道如何比较stone1和其他stones.elements。 F.E.如果我得到stone1 = R +,我想删除包含R和+的“宝石”中的所有项目。最后2行是垃圾,所以不要担心..谢谢你的帮助!

回答

3

既然你可以有每种颜色和形式之一,就把它们分开洗。有没有必要有每一个组合的首发名单:

def get_stones(): 
    colors = list('RGBW') 
    forms = list('+-*_') 
    random.shuffle(colors) 
    random.shuffle(forms) 
    return [c+f for c,f in zip(colors,forms)] 

for i in range(5): 
    print(get_stones()) 

输出:

['B*', 'R-', 'W_', 'G+'] 
['W*', 'R+', 'G_', 'B-'] 
['B+', 'R_', 'G-', 'W*'] 
['B+', 'G*', 'W-', 'R_'] 
['G_', 'B-', 'W*', 'R+'] 

注意:如果为了不要紧,你可以删除洗牌的一个。

+0

感谢您的想法!我是编程新手,所以这就是为什么我不明白,这就是那么简单。再次学到新东西:-) –

+0

Golf'd ftw:'print [a + b for a,b in zip(* map(lambda l:random.shuffle(l)or l,[list('RGBW'),list ('+ - * _')]))]' – smassey

+1

@smassey,与'或'很好的诀窍,但为了清晰起见,yuck:^)太糟糕了,没有像'sort'这样的'shuffled'与'sorted '。 –

0

如果你得到你的石头,我想它

color = stone1[0] 
form = stone1[1] 

分裂,然后通过自己的石头列表循环,消除使用一个包含colorform每一块石头。继续,直到len(L) = 4,你就完成了。

0

您将值保存为stone1进行比较。

你可以分成两个字符串,其中string1是第一个字符,string2是第二个字符。然后使用1循环,并将每个项目与您感兴趣的两个字符串/字符进行比较(在您的示例中为“R”, +),并且对于每个匹配,从列表中删除该项目。

另外, 。数最少需要比较和一个环路/通。例如,如果你找到了一个匹配,然后删除项目,并继续循环,避免第二比较

L = [] 
stones = ["R+", "R-", "R*", "R_", "G+", "G-", "G*", "G_", "B+", "B-", "B*", "B_", "W+", "W-", "W*", "W_"] 
stone1 = random.choice(stones) 
L.append(stone1) 
stones.remove(stone1) 

foo1 = stone1[0] 
foo2 = stone1[1]  

for x in stones: 
    if x.startswith(foo1): 
    stones.remove(x) 
    elif x.endswith(foo2): 
    stones.remove(x) 
+0

非常感谢!但在我的实施中,它并没有消除所有类型的错误石头。看起来,每跑完一次,“石头”中仍然有一个相似的颜色和形式。但我想我现在用这个线程的另一个解决方案修复它! –

+0

你说得对。我修正了比较。它现在有效。 – jmugz3

0

你可以不喜欢它:

import random 
L = [] 
stones = ["R+", "R-", "R*", "R_", "G+", "G-", "G*", "G_", "B+", "B-", "B*", "B_", "W+", "W-", "W*", "W_"] 
stone1 = random.choice(stones) 
L.append(stone1) 
for character in stone1: 
    copy_stones=stones[:] # You need to make a copy, if not the for fails 
    for stone in copy_stones: 
     if character in stone: 
      stones.remove(stone) 
0
>>> L = [] 
>>> stones = ["R+", "R-", "R*", "R_", "G+", "G-", "G*", "G_", "B+", "B-", "B*", "B_", "W+", "W-", "W*", "W_"] 
>>> while stones: #continue until stones is empty since empty lists return False 
     stone1 = stones.pop(random.randrange(len(stones))) #pop returns and removes an item from a list 
     L.append(stone1) 
     stones = [stone for stone in stones if stone1[0] not in stone and stone1[1] not in stone] #list comp that only copies values if neither characters are in the item 


>>> L 
['W_', 'R+', 'G*', 'B-'] 
0
stones = filter(lambda x: stone1[0] not in x and stone1[1] not in x, stones) 

在上面的过滤器中调整条件以适合您的需要。

0
from itertools import product 
from random import sample 

# Define your colours and forms (easy to add more later) 
colours = ['R', 'G', 'B', 'W'] 
forms = ['+', '-', '*', '_'] 

# Combine the colours and forms to show all combinations 
possible_stones = [''.join(stone) for stone in product(colours, forms)] 
print possible_stones 
>>> ['R+', 'R-', 'R*', 'R_', 'G+', 'G-', 'G*', 'G_', 'B+', 'B-', 'B*', 'B_', 'W+', 'W-', 'W*', 'W_'] 

# Get a random sample of the forms and colours and then combine them 
stones = zip(sample(colours, len(colours)), sample(forms, len(forms))) 
print [''.join(stone) for stone in stones] 
>>> ['B+', 'G*', 'R-', 'W_']