python - 仅将列表中的某些单词分配给变量

标签 python list

我正在尝试将 playerdeck 列表中 cards 列表中的单词分配给一个变量。这是我尝试使用的代码,但它返回 False

playerdeck = ['Five of Spades', 'Eight of Spades',
              'Eight of Clubs', 'Four of Clubs', 'Ace of Spades',
              'Eight of Hearts', 'Four of Diamonds'] 

cards = ['King', 'Queen', 'Jack', 'Ace',
     'Two', 'Three', 'Four', 'Five',
     'Six', 'Seven', 'Eight', 'Nine',
     'Ten']

new = cards in playerdeck
print(new)

有人可以帮忙吗?

最佳答案

你可以试试:

>>> playerdeck = ['Five of Spades', 'Eight of Spades',
              'Eight of Clubs', 'Four of Clubs', 'Ace of Spades',
              'Eight of Hearts', 'Four of Diamonds']
>>> cards = ['King', 'Queen', 'Jack', 'Ace',
     'Two', 'Three', 'Four', 'Five',
     'Six', 'Seven', 'Eight', 'Nine',
     'Ten']
>>> 
>>> for pd in playerdeck:
    temp = pd.split(" ")
    for data in temp:
        if data in cards:
            print data


Five
Eight
Eight
Four
Ace
Eight
Four

关于python - 仅将列表中的某些单词分配给变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42618950/

相关文章:

Java OO设计: interfaces and lists

python - Theano 缺少 signal.conv 模块

python - 为 GPU 编译 Tensor Flow 示例自定义操作

python - 无法在 macOS 上的 ipython3 中创建多行语句 block

python - 正常列表 'type' 对象不可订阅

python - 生成每个索引具有单独限制的所有排列

c - 在 C 中模拟列表

python - 以字符串为 x 轴绘制数据

python - 停止 Pandas ExcelWriter() 创建额外的列

python - 对多维列表的特定元素使用拆分