python - 列表列表,我似乎无法正确缩进

标签 python indentation

现在没有缩进错误了:

def best_wild_hand(hand):
  #Try all values for jokers in all 5-card selections.
  blackJoker= "?B"
  redJoker = "?R"


  dictSuit = {'2':2, '3':3, '4':4, '5':5, '6':6, '7':7, '8':8, '9':9, 'T':10, 'J':11, 'Q':12, 'K':13, 'A':14 }
  listofLists = []

  if blackJoker in hand:

    newHand = hand.remove(blackJoker)
    for d in dictSuit:
      listofLists.append(newHand.append(d + "S"))
    return listofLists

我正在尝试获取一个列表列表,其中是否在传递给 best_wild_hand 方法的手形参数列表中找到 blackJoker。如果发现黑手,我们将其移除并将 2..13 + C(牌组中发现的所有三叶草牌)附加到手上。我正在尝试制作一个包含 1 个三叶草的手牌列表(因此手牌列表 + nC)n 为数字 2- 13

我的预期输出是列表列表,每个列表中都有 2C...13C,它取代了黑色扑克

没有更多错误,但是当我在此打印语句中运行时,代码不会返回任何错误

print best_wild_hand(['6C', '7C', '8C', '9C', 'TC', '5C', '?B'])  

最佳答案

进行编辑以匹配您的编辑

您的问题是您正在将变量分配给方法。这导致变量为 None:

>>> y = ['6C'].remove('6C')
>>> print y
None
>>> 

相反,改变

newHand = hand.remove(blackJoker)

newHand = hand
newHand.remove(blackJoker)

因此:

def best_wild_hand(hand):
  #Try all values for jokers in all 5-card selections.
  blackJoker= "?B"
  redJoker = "?R"


  dictSuit = {'2':2, '3':3, '4':4, '5':5, '6':6, '7':7, '8':8, '9':9, 'T':10, 'J':11, 'Q':12, 'K':13, 'A':14 }
  listofLists = []

  if blackJoker in hand:

    newHand = hand
    newHand.remove(blackJoker)
    for d in dictSuit:
      listofLists.append(newHand.append(d + "S"))
    return listofLists

现在当我运行你的代码时:

bash-3.2$ python safd.py
[None, None, None, None, None, None, None, None, None, None, None, None, None]
bash-3.2$ 

也许不是您想要的,但它仍然在打印

关于python - 列表列表,我似乎无法正确缩进,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28183269/

相关文章:

python - 搜索并复制数据框中列出的文件

python - 如何读取二进制文件?

python - sklearn 的 accuracy_score 函数的类型错误

python - 展平 pandas 数据框的嵌套 Json?

html - 如何使用 CSS/HTML 在文本和文本旁边的框之间添加常规空格?

Emacs 缩进难度

python - SQLALCHEMY/PANDAS - SQLAlchemy 读取列作为 Pandas to_sql 的 CLOB

python - 缩进错误 : unexpected indent error

vim - 成对使用自动关闭时,如何在 Vim 中设置自动插入新行和缩进

objective-c - Xcode block (NSMallocBlock) 缩进(大括号自动从新行开始)