打印嵌套列表中的重复项

2024-09-24 00:23:10 发布

您现在位置:Python中文网/ 问答频道 /正文

系统要求用户输入糖果的名称。如果该甜味剂不存在于列表中,则应通知用户。如果该甜味剂在列表中,则每当列表中提到该甜味剂时,列表都会打印出列表的所有详细信息。例如,如果我输入吉百利:

# List of transactions [[customer, sweet, amount, money, day, month]
lst_sweets = [
    ["Tom", "Cadbury", 2, 9, 1, 10],
    ["Rachel", "Galaxy", 5, 10, 1, 9],
    ["Rachel", "Smarties", 4, 10, 1, 8],
    ["Lisa", "Gum drops", 10, 9, 1, 7],
    ["Donald", "Cadbury", 1, 5, 1, 6],
    ["Marie", "Smarties", 5, 10, 1, 5]
]
def sweet_purchases():
    x = input("Enter the sweet name")

    p = -1
    sum = 0

    for i in range(len(lst_sweets)):

        item = lst_sweets[i]
        name = item[1]

        if name == x:
            p = i

    if p == -1:
        print("customer not in list")
    else:

        sweetx = lst_sweets[p]
        print("Name:", sweetx[0], "Sweet:", sweetx[1], "amount:", sweetx[2], "money:", sweetx[3], "day:", sweetx[4], "month:", sweetx[5])

期望输出: 姓名:,汤姆,斯威特:,吉百利,金额:,2,金额:,9,日期:,1,月份:,10 姓名:,唐纳德,斯威特:,吉百利,金额:,1,金额:,5,日期:,1,月份:,6

实际产量: 姓名:,汤姆,斯威特:,吉百利,金额:,2,金额:,9,日期:,1,月份:,10

有人知道为什么它不同时打印出来吗?谢谢


Tags: 用户name列表customer金额amount姓名sweet
3条回答

在每个循环上设置p的方式是清除上一个匹配项

def sweet_purchases():
  x = input("Enter the sweet name")
  cnt=0
  for entry in lst_sweets:
    if entry[1] == x:
      print(entry) # or whatever
      cmt+=1
  if not cmt:
    print('No joy')

如果您只想打印一些输出,这是最简单的方法。如果要在找到所有行后执行某些操作,则需要累积结果。大概是这样的:

def sweet_purchases():
  x = input("Enter the sweet name")
  hits=[]
  for entry in lst_sweets:
    if entry[1] == x:
      hits.append(entry)
   return hits

但是“pythonic”方法只是找到列表而不进行处理,就是使用理解:

def sweet_purchases():
  x = input("Enter the sweet name")
  return [e for e in lst_sweets
            if e[1] == x]
# List of transactions [[customer, sweet, amount, money, day, month]
lst_sweets = [
    ["Tom", "Cadbury", 2, 9, 1, 10],
    ["Rachel", "Galaxy", 5, 10, 1, 9],
    ["Rachel", "Smarties", 4, 10, 1, 8],
    ["Lisa", "Gum drops", 10, 9, 1, 7],
    ["Donald", "Cadbury", 1, 5, 1, 6],
    ["Marie", "Smarties", 5, 10, 1, 5]
]
def sweet_purchases():
    x = input("Enter the sweet name")

    p = [] # make a list that stores the matching index
    sum = 0

    for i in range(len(lst_sweets)):

        item = lst_sweets[i]
        name = item[1]

        if name == x:
            p.append(i)

    if p == None: # if the list is empty
        print("customer not in list")
    else:
        # Now iterate over the correct list of index and print the values
        for i in p:
            sweetx = lst_sweets[i]
            print("Name:", sweetx[0], "Sweet:", sweetx[1], "amount:", sweetx[2], "money:", sweetx[3], "day:", sweetx[4], "month:", sweetx[5])

如果找到多个匹配项,则覆盖p,只打印一个匹配项。您应该在循环中打印:

lst_sweets = [
    ["Tom", "Cadbury", 2, 9, 1, 10],
    ["Rachel", "Galaxy", 5, 10, 1, 9],
    ["Rachel", "Smarties", 4, 10, 1, 8],
    ["Lisa", "Gum drops", 10, 9, 1, 7],
    ["Donald", "Cadbury", 1, 5, 1, 6],
    ["Marie", "Smarties", 5, 10, 1, 5]
]

fields = ["name", "sweet", "amount", "money", "day", "month"]

x = input("Enter the sweet name: ")
found = False
for sweet in lst_sweets:
    if sweet[1] == x:
        found = True
        print(", ".join(f"{field}: {val}" for field, val in zip(fields, sweet)))
if not found:
    print("sweet not found")

相关问题 更多 >