为什么不更新?

时间:2015-12-24 17:01:31

标签: python python-3.x

通过其他功能将联系人添加到此lst = [] 我正在构建一个地址簿,其功能允许您通过提及名称从列表列表中删除联系人:

def deletec(lst): 
    name=str(input("Name:"))
    lst = sum(lst, [])
    names = lst[::3]
    numbers = lst[1::3]
    emails = lst[2::3]
    dct = {name: (number,email) for name, number, email in zip(names, numbers, emails)}
    if name in dct:
        del dct[name]
        print("Deleted!")
    else:
        print("Name Doesn´t Exists!")
    list_key_value=[ [k,v] for k,v in dct.items()]
    list_key_value = [[name, *tp] for (name, tp) in list_key_value]
    lst=list_key_value
    return lst

如果你的清单是这样的话:

[['Bruno', '44444', 'bruno@hotmail.com'], ['Mariah', '333', 'mariah@hotmail.com'], ['Oliver', '3333', 'oliver@yahoo.co.uk']]

你想只使用它返回的函数删除Bruno:

[['Mariah', '333', 'mariah@hotmail.com'], ['Oliver', '3333', 'oliver@yahoo.co.uk']]

但是当你问另一个按字母顺序打印它的函数时,它会使用旧的lst:

def arrangebyorder(lst):
  from operator import itemgetter
        print(sorted(lst, key=itemgetter(0)))
        return lst

打印此内容:

[['Bruno', '44444', 'bruno@hotmail.com'], ['Mariah', '333', 'mariah@hotmail.com'], ['Oliver', '3333', 'oliver@yahoo.co.uk']]

我正在考虑这样做来解决这个问题,因为list_key_value是空的,如果你没有删除任何来自lst的名字你做一个函数,当这个变量为空时打印旧的lst {1}}按字母顺序排序,如果定义了list_key_value,则按字母顺序打印list_key_value

def list_sorted(lst,lst_key_value):
  try:
    list_key_value
except NameError:
    from operator import itemgetter
    print(sorted(lst, key=itemgetter(0)))
    return lst
else:
    from operator import itemgetter
    print(sorted(list_key_value, key=itemgetter(0)))
    return list_key_value

问题在于,当我没有定义lst_key_value时,它会带来此错误:

TypeError: list_sorted(lst,lst_key_value) missing 1 required positional argument: 'lst_key_value'

有人可以在其他解决方案中思考吗?

仅允许/使用列表列表的解决方案

1 个答案:

答案 0 :(得分:3)

您的代码似乎不必要地复杂化了。您可以使用简单的列表解析来从地址列表中删除条目。您可能还应该为您的地址引入一个类,否则您的代码将很快变得难以阅读。以下代码似乎可以提供您想要的更好的可读性:

class Address(object):
    def __init__(self, name, number, email):
        self.name = name
        self.number = number
        self.email = email

    def __repr__(self):
        return "Address(%s, %d, %s)" % (self.name, self.number, self.email)

# your list of addresses
addresses = [
    Address('Bruno', 44444, 'bruno@hotmail.com'),
    Address('Mariah', 333, 'mariah@hotmail.com'),
    Address('Oliver', 3333, 'oliver@yahoo.co.uk')
]

# Print your addresses
# prints: [Address(Bruno, 44444, bruno@hotmail.com), Address(Mariah, 333, mariah@hotmail.com), Address(Oliver, 3333, oliver@yahoo.co.uk)]
print(addresses)

# Remove "Bruno" from your addresses
# replacement for your "deletec()" function
# prints: [Address(Mariah, 333, mariah@hotmail.com), Address(Oliver, 3333, oliver@yahoo.co.uk)]
print([address for address in addresses if address.name != "Bruno"])

# Sort your addresses descending by name
# replacement for your "arrangebyorder()" function
# prints: [Address(Oliver, 3333, oliver@yahoo.co.uk), Address(Mariah, 333, mariah@hotmail.com), Address(Bruno, 44444, bruno@hotmail.com)]
print(sorted(addresses, key=lambda address: address.name, reverse=True))

列表解决方案的类似列表,根据您的编辑要求:

addresses = [
    ['Bruno', 44444, 'bruno@hotmail.com'],
    ['Mariah', 333, 'mariah@hotmail.com'],
    ['Oliver', 3333, 'oliver@yahoo.co.uk']
]

# prints: [['Bruno', 44444, 'bruno@hotmail.com'], ['Mariah', 333, 'mariah@hotmail.com'], ['Oliver', 3333, 'oliver@yahoo.co.uk']]
print(addresses)

# Remove "Bruno" from your addresses
# prints: [['Mariah', 333, 'mariah@hotmail.com'], ['Oliver', 3333, 'oliver@yahoo.co.uk']]
print([address for address in addresses if address[0] != "Bruno"])

# Sort your addresses descending by name
# prints: [['Oliver', 3333, 'oliver@yahoo.co.uk'], ['Mariah', 333, 'mariah@hotmail.com'], ['Bruno', 44444, 'bruno@hotmail.com']]
print(sorted(addresses, key=lambda address: address[0], reverse=True))