1

各アイテムの最大入札額のインデックスを見つけるコード ブロックを作成しようとしています。次に、その金額を支払った人の名前を特定する方法としてインデックスを使用するつもりでした。しかし、私が何をしようとしても、その人物と彼らがオークションで得たものを結びつけることはできません. 私が書いているコードは次のとおりです。入力された情報を処理できる必要があります

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

どんなアドバイスも大歓迎です。

4

3 に答える 3

1

入札には他の構造を使用できます。インデックスによって同期されたさまざまなリストを使用する代わりに、辞書と Python のタプルを使用できます。多分このようなもの:

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

次に、最大を取得します。各アイテムの入札は簡単です:

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

これは入札の挿入が高速ですが、最高入札額を取得するにはより多くの時間を必要とするため、データ構造を別の方法で設計することができます。また、1 人の入札者が行ったすべての入札を取得することは、コンピュータにとってより多くの作業になります。

また、コードをより保守しやすくするために、タプルの代わりに名前付きフィールドを持ついくつかのオブジェクトを使用できます。

于 2011-03-17T16:34:45.273 に答える
1

名前をキーとして辞書を使用するのが最も簡単だと思います。そうすれば、何が起こっているのかがわかります。

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
于 2011-03-17T16:36:51.807 に答える
0

これは恐ろしいコードです - これを試してください:

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)
于 2011-03-17T16:51:20.383 に答える