如何识别另一个列表中列表中的元素

发布于 2024-10-22 11:39:07 字数 2770 浏览 0 评论 0原文

我一直在尝试编写一段代码来查找每个项目的最大出价的索引。然后我打算使用索引来识别支付那么多钱的人的名字。然而,无论我如何尝试,我都无法将这个人和他们从拍卖中获得的东西联系在一起。这是我一直在编写的代码:它必须能够处理输入的任何信息,

def sealedBids():
    n = int(input('\nHow many people are in the group? '))#determines loop lengths
    z = 0#meant to act as a counter in while loops
    g = []#meant to store the names of all the people/players
    s = []#meant to store name of all items being bidded on
    b = []#meant to store all bids made
    f = []#meant to store the each persons fair share
    w = []#meant to store the highest bid for each item
    q = []#trying to get it to store person linked to whatever they won

    while z < n:#Process: to make the list nest lists
        b.append([])
        z = z + 1
    z = 0

    while z < n:
        g.append(input('Enter a bidders name: '))#Input: Persons name
        z = z + 1                                #Process: store name in g[] list
    z = 0

    i = int(input('How many items are being bid on?'))#determines so loop lengths

    while z < i:
        s.append(input('Enter the name of an item: '))#input: Name of item
                                                          #process: stores names in s[] list
        w.append(z)#was going to swap the info inside with the info I wanted
        z = z + 1
    z = 0
    for j in range(n):#specifies which persons bid your taking
        for k in range(i):#specifies which item is being bid on
            b[j].append(int(input('How much money has {0} bid on the {1}? '.format(g[j], s[k]))))#input: takes bid for certain item
                                                                                                       #process: stores bid in b[] list
        print(' ')#adds a space when questions are being asked so it doesn't look crunched up

    for j in range(n):#calculates fair share
        f.append(sum(b[j])/n)#add a persons total bids then divides it by number of people bidding

    for j in range(i):#meant to change the item after every bid is compared to stored highest bid
        for k in range(n):#meant to change to a different persons bid to find out who bid the most money on a particular item
            if w[j] < b[k][j]:#compares stored highest bid for particular item to each persons bid
                w[j] = b[k][j]#if highest bid is smaller then highest bid changes to the bid that was larger
                q.append(k)#trying to get it to store indentifier for whoever has highest bid so that I can use it later to link it with highest bid somehow
    print(g)#meant to check outcome
    print(s)#meant to check outcome
    print(w)#meant to check outcome
    print(q)#meant to check outcome
    print(b)#meant to check outcome

print(f)#meant to check outcome

非常感谢任何建议。

I have been trying to make a block of code that finds the index of the largest bid for each item. Then I was going to use the index as a way to identify the person who paid that much moneys name. However no matter what i try I can't link the person and what they have gained from the auction together. Here is the code I have been writing: It has to be able to work with any information inputted

def sealedBids():
    n = int(input('\nHow many people are in the group? '))#determines loop lengths
    z = 0#meant to act as a counter in while loops
    g = []#meant to store the names of all the people/players
    s = []#meant to store name of all items being bidded on
    b = []#meant to store all bids made
    f = []#meant to store the each persons fair share
    w = []#meant to store the highest bid for each item
    q = []#trying to get it to store person linked to whatever they won

    while z < n:#Process: to make the list nest lists
        b.append([])
        z = z + 1
    z = 0

    while z < n:
        g.append(input('Enter a bidders name: '))#Input: Persons name
        z = z + 1                                #Process: store name in g[] list
    z = 0

    i = int(input('How many items are being bid on?'))#determines so loop lengths

    while z < i:
        s.append(input('Enter the name of an item: '))#input: Name of item
                                                          #process: stores names in s[] list
        w.append(z)#was going to swap the info inside with the info I wanted
        z = z + 1
    z = 0
    for j in range(n):#specifies which persons bid your taking
        for k in range(i):#specifies which item is being bid on
            b[j].append(int(input('How much money has {0} bid on the {1}? '.format(g[j], s[k]))))#input: takes bid for certain item
                                                                                                       #process: stores bid in b[] list
        print(' ')#adds a space when questions are being asked so it doesn't look crunched up

    for j in range(n):#calculates fair share
        f.append(sum(b[j])/n)#add a persons total bids then divides it by number of people bidding

    for j in range(i):#meant to change the item after every bid is compared to stored highest bid
        for k in range(n):#meant to change to a different persons bid to find out who bid the most money on a particular item
            if w[j] < b[k][j]:#compares stored highest bid for particular item to each persons bid
                w[j] = b[k][j]#if highest bid is smaller then highest bid changes to the bid that was larger
                q.append(k)#trying to get it to store indentifier for whoever has highest bid so that I can use it later to link it with highest bid somehow
    print(g)#meant to check outcome
    print(s)#meant to check outcome
    print(w)#meant to check outcome
    print(q)#meant to check outcome
    print(b)#meant to check outcome

print(f)#meant to check outcome

any advice is much appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

撧情箌佬 2024-10-29 11:39:07

您可以使用其他结构进行出价。您可以使用字典和 python 元组,而不是使用按索引同步的不同列表。也许是这样的:

items_bids = { 
  item1:  [ (bidder1, amount), (some_other_bidder, amount), ... ],
  item2:  [ (bidder1, amount), (some_other_bidder, amount), ... ],
  .
  .
  .
}

然后检索 max。每个项目的出价都很简单:

for item, bids in items_bids.iteritems():
    print max(bids, key=lambda x: x[1])

您可以以不同的方式设计数据结构,因为该数据结构可以快速插入出价,但需要更多时间来检索最高出价。此外,检索一个投标人的所有投标对计算机来说也将是更多的工作。

对于更易于维护的代码,您可以使用一些带有命名字段的对象而不是元组。

You can use other structure for your bids. Instead of using different lists synchronized by index, you can use dictionary and python tuples. Maybe something like this:

items_bids = { 
  item1:  [ (bidder1, amount), (some_other_bidder, amount), ... ],
  item2:  [ (bidder1, amount), (some_other_bidder, amount), ... ],
  .
  .
  .
}

Then retrieving max. bids for each item is easy:

for item, bids in items_bids.iteritems():
    print max(bids, key=lambda x: x[1])

You may design your data structure differently as this one has fast insert of bids but needs more time to retrieve the highest bid. Also retrieving all bids made by one bidder would be more work for the computer.

And for more maintainable code, you may use some objects with named fields instead of tuples.

飞烟轻若梦 2024-10-29 11:39:07

我认为使用以名称作为键的字典是最简单的,这样你就可以看到发生了什么:

group_size = 3
bidders = ('Alice', 'Bob', 'Eve')
items = ('Pipe', 'Wrench')

bids = dict(((item ,dict(((bidder, 0) for bidder in bidders))) for item in items))

#>>> print bids
#{'Pipe': {'Bob': 0, 'Alice': 0, 'Eve': 0},
# 'Wrench': {'Bob': 0, 'Alice': 0, 'Eve': 0}}

#get the money amounts
for item in bids:
    for bidder in bids[item]:
        bids[item][bidder] = int(raw_input('How much money has {0} bid on the {1}?'.format(bidder, item)))

highest_bidders = dict((item, bidder) for item in bids for bidder in bids[item]
                        if bids[item][bidder] == max(bids[item].itervalues()))
print highest_bidders

I think it would be easiest to use dictionaries with the names as keys, that way you can see what's going on:

group_size = 3
bidders = ('Alice', 'Bob', 'Eve')
items = ('Pipe', 'Wrench')

bids = dict(((item ,dict(((bidder, 0) for bidder in bidders))) for item in items))

#>>> print bids
#{'Pipe': {'Bob': 0, 'Alice': 0, 'Eve': 0},
# 'Wrench': {'Bob': 0, 'Alice': 0, 'Eve': 0}}

#get the money amounts
for item in bids:
    for bidder in bids[item]:
        bids[item][bidder] = int(raw_input('How much money has {0} bid on the {1}?'.format(bidder, item)))

highest_bidders = dict((item, bidder) for item in bids for bidder in bids[item]
                        if bids[item][bidder] == max(bids[item].itervalues()))
print highest_bidders
苯莒 2024-10-29 11:39:07

这是可怕的代码 - 试试这个:

def sealedBids():
  n = input('\nHow many people are in the group? ')
  assert isinstance(n, int)
  bidders = {}
  for i in range(n):
    bidders[raw_input('Enter a bidders name: ')] = {}
  n = input('How many items are being bid on?')
  assert isinstance(n, int)
  bid_items = {}
  for i in range(n):
    bid_items[raw_input('Enter a item name: ')] = {}
  del n
  f = []
  for bidder, bidder_bids in bidders.items():
    for bid_item, bid_item_bids in bid_items.items():
      bid = input('How much money has %s bid on the %s? '%(bidder, bid_items)
      assert isinstance(bid, int)
      bidder_bids[bid_item] = bid
      bid_item_bids[bidder] = bid
    print ''
    f.append(sum(bidder_bids.values())/len(bidders)) # what is this for?
  for bid_item, bid_item_bids in bid_items.items():
    inv_bid_item_bids = dict(map(lambda item: (item[1],item[0]),bid_item_bids.items()))
    high_bid = max(inv_bid_item_bids.keys())
    high_bidder = inv_bid_item_bids[high_bid]
    bid_items[bid_item] = (high_bidder, high_bid)

This is horrible code - try this:

def sealedBids():
  n = input('\nHow many people are in the group? ')
  assert isinstance(n, int)
  bidders = {}
  for i in range(n):
    bidders[raw_input('Enter a bidders name: ')] = {}
  n = input('How many items are being bid on?')
  assert isinstance(n, int)
  bid_items = {}
  for i in range(n):
    bid_items[raw_input('Enter a item name: ')] = {}
  del n
  f = []
  for bidder, bidder_bids in bidders.items():
    for bid_item, bid_item_bids in bid_items.items():
      bid = input('How much money has %s bid on the %s? '%(bidder, bid_items)
      assert isinstance(bid, int)
      bidder_bids[bid_item] = bid
      bid_item_bids[bidder] = bid
    print ''
    f.append(sum(bidder_bids.values())/len(bidders)) # what is this for?
  for bid_item, bid_item_bids in bid_items.items():
    inv_bid_item_bids = dict(map(lambda item: (item[1],item[0]),bid_item_bids.items()))
    high_bid = max(inv_bid_item_bids.keys())
    high_bidder = inv_bid_item_bids[high_bid]
    bid_items[bid_item] = (high_bidder, high_bid)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文