2015-02-08 26 views
0

我用几个简单的方法创建了一个列表类。列表类保留新列表的旧值Python

class ListQ(object): 
    """List object creation""" 
    def __init__(self, earlier_list=[]): 
     self.list=earlier_list 

    def isEmpty(self): 
     """checks if list is empty""" 
     if len(self.list)==0: 
      return(True) 
     else: 
      return(False) 

    def put(self, new_item): 
     """puts new_item last in list""" 
     self.list.append(new_item) 

    def get(self): 
     """returns the next item in line""" 
     self.first_item = self.list.pop(0) 
     return self.first_item 

我也有一个函数来帮助创建这些列表。 (帮助解决BTW魔法卡招)

def create_card_list(card_list=None): 
    """creates listQ of cards from user input""" 
    if card_list!=None: #if user already has list of cards 
     for item in card_list: 
      item=replace_card_strings(item) #replaces any strings in list 
      if item == False: 
       print('One of your cards does not exist, list creation failed') 
       return(False) 
     cards=ListQ(card_list) 

()函数已经得到很好的测试,我不相信这是有过错的,但在这里它是无论如何的replace_card_strings。

def replace_card_strings(word): 
    """replaces strings of words with their numbers and returns edited list""" 
    if type(word)!=int: 
     if word.lower()=='one': 
      word=1 
     elif word.lower()=='two': 
      word=2 
     elif word.lower()=='three': 
      word=3 
     elif word.lower()=='four': 
      word=4 
     elif word.lower()=='five': 
      word=5 
     elif word.lower()=='six': 
      word=6 
     elif word.lower()=='seven': 
      word=7 
     elif word.lower()=='eight': 
      word=8 
     elif word.lower()=='nine': 
      word=9 
     elif word.lower()=='ten': 
      word=10 
     elif word.lower()=='jack': 
      word=11 
     elif word.lower()=='queen': 
      word=12 
     elif word.lower()=='king': 
      word=13 
     elif word.lower()=='ace': 
      word=1 
     else: 
      word=False #if card doesnt exist 
    return(word) 

当我运行下面的测试的时候是问题出现的时候。

cards=create_card_list(['one', 2, 3]) 
one=cards.get() 
two=cards.get() 
three=cards.get() 
print(one, two, three) 
if one==1 and two==2 and three==3: 
    print("create_card_list gives the correct answer") 
else: 
    print("create_card_list gives the wrong answer") 

它打印出该功能已给出错误的答案和 的print(one, two, three)打印one 2 3这是最初的名单。

任何人有一个想法,我已经搞砸了?

+0

您不应该首先创建列表类,因为那里已经有Python列表。 – 2015-02-08 09:12:30

+2

是的,我知道,但分配是创建这个类,所以我没有选择。 – rickri 2015-02-08 09:13:38

+1

此外,您*需要*复制ListQ构造函数中的列表! – 2015-02-08 09:14:05

回答

1

你不在列表中替换的项目,只有在循环的上下文:

更换整个循环:

for item in card_list: 
     item=replace_card_strings(item) #replaces any strings in list 
     .... 

这并不做任何事情到列表(它 “替换”该项目)。

与Python的map这是为了使用的功能应用到迭代和创建结果的列表:

card_list = map(replace_card_strings, card_list) 

如果你不想使用map你可以使用list comprehension

card_list = [replace_card_strings(card_str) for card_str in card_list] 

现在你必须:

def create_card_list(card_list=None): 
    """creates listQ of cards from user input""" 
    if card_list != None: 
     card_list = map(replace_card_strings, card_list) 
    cards=ListQ(card_list) 

提示:

可以代替冗长的功能replace_card_strings

def replace_card_strings(word): 
    """replaces strings of words with their numbers and returns edited list""" 

    word = word.lower() 

    string_to_int = {"one":1, "two":2 ...} 

    return word in string_to_int and string_to_int[word] 

这工作,因为and返回最后Truth -y结果或第一False -y结果。

您还可以使用dict.get

# return word in string_to_int and string_to_int[word] 
# try and read value from dict, return False otherwise 
return string_to_int.get(word, False) 

这是清洁剂(感谢@padraic)。

提示2:你不希望两个地方引用,并可能突变同一个列表。看起来好像你保留了一个用于在生成的对象之外构建卡列表的相同列表的引用。这很容易出错。你应该做的,而不是被复制的清单时,它的传递:

def __init__(self, earlier_list=[]): 
    self.list=earlier_list[:] 

提示3: Python有一个known "gotcha" with mutable default argument,这是你在这里(earlier_list=[])使用什么。这很容易出错,因为python绑定默认参数一次。您应该改为:

def __init__(self, earlier_list=None): 
    # copy passed list or create a new one if none passed. 
    self.list=earlier_list[:] if earlier_list else [] 
+1

感谢您对问题和额外提示的帮助! – rickri 2015-02-08 09:33:12

+2

我认为'返回string_to_int.get(单词,假)'会更好一点 – 2015-02-08 09:58:01

+1

@PadraicCunningham谢谢,添加 – 2015-02-08 10:36:39