Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 将字典添加到字典中元组列表的末尾_Python_Python 3.x_List_Dictionary - Fatal编程技术网

Python 将字典添加到字典中元组列表的末尾

Python 将字典添加到字典中元组列表的末尾,python,python-3.x,list,dictionary,Python,Python 3.x,List,Dictionary,我正在解决一个问题,我有点不知道从哪里开始 以下是我正在处理的列表: customers = list(); customer_list.append( {{'id':12345, 'first_name':'John', 'last_name':'Anderson','orders':[('order_1',300),('order_2',255),('order_3',79)]}) customer_list.append( {{'id':12346, 'first_name':'Mary'

我正在解决一个问题,我有点不知道从哪里开始

以下是我正在处理的列表:

customers = list();
customer_list.append( {{'id':12345, 'first_name':'John', 'last_name':'Anderson','orders':[('order_1',300),('order_2',255),('order_3',79)]})
customer_list.append( {{'id':12346, 'first_name':'Mary', 
'last_name':'Smith','orders':[('Order_1' 400),('order_2',199),('order_3',49)]})
我需要通过我的列表传递一本带有订单名称和编号的字典。如果字典中的订单号在列表中不存在,则应将其添加到列表的末尾。如果订单号确实存在,则不应添加该订单号。此外,如果添加了赋值,则应返回true,否则应返回false

我真的很难开始这项工作,任何帮助都将不胜感激。这是我到目前为止所做的

order_dict = {order_1: 29, order_4, 99} 
#I want to add order_4 to the end of my list of tuples
def add_order(customers)
    for c in customers:

您可以遍历dict的
客户
列表,以查找与订单匹配的客户名称。如果找到了匹配的客户名称,则查找匹配的订单号,如果找到,则将
for else
构造中中断
以返回
False
;如果找不到匹配的订单号,请将订单号附加到匹配名称的客户的
orders
项中。如果未找到具有匹配名称的客户,请将新订单详细信息和增量
id
附加到
客户
列表中

def add_order(customers, order):
    for customer in customers:
        if order['first_name'] == customer['first_name'] and order['last_name'] == customer['last_name']:
            for _, order_number in customer['orders']:
                if order['number'] == order_number:
                    break
            else:
                customer['orders'].append(('order_%d' % (len(customer['orders']) + 1), order['number']))
                return True
            break
    else:
        customers.append({
            'id': customers[-1]['id'] + 1 if customers else 1,
            'first_name': order['first_name'],
            'last_name': order['last_name'],
            'orders': [('order_1', order['number'])]
        })
        return True
    return False
以便:

customers = [{'id': 12345, 'first_name': 'John', 'last_name': 'Anderson',
              'orders': [('order_1', 300), ('order_2', 255), ('order_3', 79)]},
             {'id': 12346, 'first_name': 'Mary', 'last_name': 'Smith',
              'orders': [('order_1', 400), ('order_2', 199), ('order_3', 49)]}]
print(add_order(customers, {'first_name': 'Mary', 'last_name': 'Smith', 'number': 199}))
print(add_order(customers, {'first_name': 'Mary', 'last_name': 'Smith', 'number': 200}))
print(add_order(customers, {'first_name': 'Mary', 'last_name': 'Brown', 'number': 201}))
from pprint import pprint
pprint(customers)
将输出:

False
True
True
[{'first_name': 'John',
  'id': 12345,
  'last_name': 'Anderson',
  'orders': [('order_1', 300), ('order_2', 255), ('order_3', 79)]},
 {'first_name': 'Mary',
  'id': 12346,
  'last_name': 'Smith',
  'orders': [('order_1', 400),
             ('order_2', 199),
             ('order_3', 49),
             ('order_4', 200)]},
 {'first_name': 'Mary',
  'id': 12347,
  'last_name': 'Brown',
  'orders': [('order_1', 201)]}]

请正确格式化您的问题,并添加一个简单的Python标记。这实际上是一个需求转储。如果你有任何尝试,就把它们贴出来。如果没有,请解释你对令人痛苦的细节的想法,包括对这里令人困惑的地方的精确解释。我正努力理解从哪里开始。我刚刚了解了不同的数据结构,但所有这些嵌套都让我感到厌烦。此外,您在这里发布的内容根本不是有效的python代码。你应该在做其他事情之前先解决这个问题。使用所有不匹配的大括号很难理解您的数据结构。我认为这是你问题的一大部分,我同意。这是我用来完成这个问题的数据集。