比较字符串与列表元素python

时间:2015-11-09 22:21:49

标签: python

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

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和其他stone.elements。 F.E.如果我得到stone1 = R +,我想删除“stone”中包含R和+的所有项目。最后两行是垃圾,所以不要担心..感谢您的帮助!

7 个答案:

答案 0 :(得分: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+']

注意:如果订单不重要,你可以放弃其中一个洗牌。

答案 1 :(得分:0)

如果你得到了你的石头,我会把它拆分成

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

然后遍历您的石头列表并消除包含colorform的每块石头。继续,直到len(L) = 4,然后你就完成了。

答案 2 :(得分: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)

答案 3 :(得分: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)

答案 4 :(得分: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-']

答案 5 :(得分:0)

stones = filter(lambda x: stone1[0] not in x and stone1[1] not in x, stones)

调整上面过滤条件中的条件以满足您的需要。

答案 6 :(得分: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_']
相关问题