为什么.append()在此程序中不起作用?

时间:2018-07-12 23:04:33

标签: python python-3.x

one_count = []
two_count = []
three_count = []
four_count = []
five_count = []
six_count = []
seven_count = []

counter = Counter(hand)
for d in counter:
    if counter[d] == 1:
        one_count.append(d)
    if counter[d] == 2:
        two_count.append(d)
    if counter[d] == 3:
        three_count.append(d)
    if counter[d] == 4:
        four_count.append(d)
    if counter[d] == 5:
        five_count.append(d)
    if counter[d] == 6:
        six_count.append(d)
    if counter[d] == 7:
        seven_count.append(d)

one_dict = Counter(one_count)
two_dict = Counter(two_count)
three_dict = Counter(three_count)
four_dict = Counter(four_count)
five_dict = Counter(five_count)
six_dict = Counter(six_count)
seven_dict = Counter(seven_count)

print one_dict
print two_dict
print three_dict
print four_dict
print five_dict
print six_dict
print seven_dict

你好!运行上面的程序后,其打印的内容是Counter()七次。我似乎无法弄清楚出了什么问题,也无法弄清楚眼睛是否新鲜,也许有人能够找出出什么问题了。请记住,此代码只是程序的一部分。

编辑:我意识到我可以只使用Counter(hand),而不用嵌套很多if和for语句。谢谢大家的帮助!

2 个答案:

答案 0 :(得分:4)

无论hand是什么,one_used都是一个空列表,因此您的行

for e in one_used:

对一个空列表进行迭代,换句话说,什么都不做。

答案 1 :(得分:2)

您正在定义Private pNumber As Integer Private pProperties As New Collection Private pWealth As Long Property Get Number() As Integer Number = pNumber End Property Property Let Number(pNum As Integer) pNumber = pNum End Property Property Get Properties() As Collection Properties = pProperties End Property Property Get Wealth() As Long Number = pWealth End Property Property Let Wealth(w As Long) pWealth = w End Property Public Sub addProperty(property As PropertyUnit) pProperties.Add (property) End Sub ,这是一个空列表。 然后,您遍历空白列表,因此one_used = []甚至不存在。这样会抛出所有循环并进行检查,因此没有要追加的内容。

e

问题:for d in hand: for e in one_used: 为空。