英文:
Python check for other rows while iterating through for loop
问题
我有以下这个元组列表:
[(21, 2, 10.0),
(21, 104, 20.0),
(22, 1, 371.0),
(22, 104, 742.0),
(23, 1, 114.0),
(23, 104, 228.0),
(25, 1, 2.0),
(25, 104, 2.0)]
每个数字的上下文,按顺序,分别是一个id、sku_id和数量。目标是遍历具有相同ID的每批元组,并执行以下操作:
- 检查具有sku_id为104的任何条目是否在同一quote_id中具有另一个条目
- 其他条目的数量必须是前一行的一半。
在上面的示例中,具有id为25的行将匹配,因为sku_id为1的行的数量不是sku_id为104的行的数量的一半。这应该附加到最终集合中。
我该如何实现这个目标?
英文:
So I have this list of tuples as shown below:
[(21, 2, 10.0),
(21, 104, 20.0),
(22, 1, 371.0),
(22, 104, 742.0),
(23, 1, 114.0),
(23, 104, 228.0),
(25, 1, 2.0),
(25, 104, 2.0)]
The context of each number, in order, is an id, sku_id and quantity. The goal is to go through each batch of tuples with the same ID and do the following:
- Check that any entry with a sku_id of 104 has another entry within the same quote_id
- That other entry must be half the quantity as the previous row.
In the example above, the rows with the id of 25 would match as the row with sku_id being 1 does not have half the quantity as the row with sku_id 104. This should be appended to a final set.
How can I accomplish this?
答案1
得分: 1
尝试使用字典来编写此代码:
lis = [(21, 2, 10.0),
(21, 104, 20.0),
(22, 1, 371.0),
(22, 104, 742.0),
(23, 1, 114.0),
(23, 104, 228.0),
(25, 1, 2.0),
(25, 104, 2.0)]
print(lis)
out = {}
for i in lis:
temp = out.get(i[0], False)
if temp:
if i[1] == 104 and i[2] == temp * 2:
pass
else:
print(i[0])
else:
out[i[0]] = i[2]
英文:
Try this code using dictionaries:
lis = [(21, 2, 10.0),
(21, 104, 20.0),
(22, 1, 371.0),
(22, 104, 742.0),
(23, 1, 114.0),
(23, 104, 228.0),
(25, 1, 2.0),
(25, 104, 2.0)]
print(lis)
out = {}
for i in lis:
temp = out.get(i[0],False)
if temp:
if i[1]==104 and i[2]==temp*2:
pass
else:
print(i[0])
else:
out[i[0]] = i[2]
答案2
得分: 1
You could use collections.defaultdict
to make a dictionary of the orders:
all_orders = defaultdict(dict)
for i, sku, qty in data:
all_orders[i][sku] = qty
all_orders
is now:
{
21: {2: 10.0, 104: 20.0},
22: {1: 371.0, 104: 742.0},
23: {1: 114.0, 104: 228.0},
25: {1: 2.0, 104: 2.0}
}
Then just loop through that and find if they meet the criteria:
for i, orders in all_orders.items():
if 104 not in orders:
continue
if len(orders) == 1:
print("Needs to be more than one order if sku is 104")
continue
half_104_qty = orders[104] / 2
for qty in orders.values():
if qty < half_104_qty:
continue
else:
print("There must be another order half the quantity of the order with sku 104")
英文:
You could use collections.defaultdict
to make a dictionary of the orders:
all_orders = defaultdict(dict)
for i, sku, qty in data:
all_orders[i][sku] = qty
all_orders
is now:
{
21: {2: 10.0, 104: 20.0},
22: {1: 371.0, 104: 742.0},
23: {1: 114.0, 104: 228.0},
25: {1: 2.0, 104: 2.0}
}
Then just loop through that and find if they meet the criteria
for i, orders in all_orders.items():
if 104 not in orders:
continue
if len(orders) == 1:
print("Needs to be more than one order if sku is 104")
continue
half_104_qty = orders[104] / 2
for qty in orders.values():
if qty < half_104_qty:
continue
else:
print("There must be another order half the quantity of the order with sku 104")
答案3
得分: 0
代码:
data = [(21, 2, 10.0),
(21, 104, 20.0),
(22, 1, 371.0),
(22, 104, 742.0),
(23, 1, 114.0),
(23, 104, 228.0),
(25, 1, 2.0),
(25, 104, 2.0)]
data_map = {}
for item in data:
id = item[0]
if id not in data_map:
data_map[id] = [item]
else:
data_map[id].append(item)
for key, value in data_map.items():
for index, item in enumerate(value ,1):
id = item[0]
if index != len(value):
for next_item in value[index:]:
if item[2] * 2 != next_item[2]:
print(f"{item} does not match {next_item}")
输出:
英文:
Code:
data = [(21, 2, 10.0),
(21, 104, 20.0),
(22, 1, 371.0),
(22, 104, 742.0),
(23, 1, 114.0),
(23, 104, 228.0),
(25, 1, 2.0),
(25, 104, 2.0)]
data_map = {}
for item in data:
id = item[0]
if id not in data_map:
data_map[id] = [item]
else:
data_map[id].append(item)
for key, value in data_map.items():
for index, item in enumerate(value ,1):
id = item[0]
if index != len(value):
for next_item in value[index:]:
if item[2] * 2 != next_item[2]:
print(f"{item} does not match {next_item}")
Output:
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论