简体   繁体   English

我正在尝试打印出我的字典列表中出价最高的人

[英]I am trying to print out the highest bidder in my list of dictionaries

I am trying to loop through my list of dictionaries and print out the highest bidder in my list of dictionaries.我正在尝试遍历我的字典列表并打印出我的字典列表中出价最高的人。 In a for loop with python is 'i' not automatically an integer?在带有 python 的 for 循环中,“我”不会自动成为 integer?

Example: for i in dictionary: Does i not equal 0, 1, 2 so on?示例:对于字典中的 i:i 不等于 0、1、2 等等吗?


bidders = [
{
  "name": "nicholas",
  "bid": 12,
},
{
  "name": "jack",
  "bid": 69,
},
]

for i in bidders:
        bid = bidders[i]["bid"]
        name = bidders[i]["name"]
        
        if bid > bid:
            print(f"Congratulations {name}! Your bid of ${bid} wins!")
        else:
            print(f"Congratulations {name}! Your bid of ${bid} wins!")

You can use enumerate to range through a list and get the current index.您可以使用enumerate来遍历列表并获取当前索引。 Fixing your for loop, it would look like this:修复你的for循环,它看起来像这样:

for idx, _ in enumerate(bidders):
    bid = bidders[idx]["bid"]
    name = bidders[idx]["name"]

No, since you are not calling the range function i is not default a integer.不,因为您没有调用范围 function,所以我不是默认的 integer。 Leonardo's answer looks more correct, however what I wrote down below would also work.莱昂纳多的答案看起来更正确,但是我在下面写下的内容也可以。

bidders = [
    {
      "name": "nicholas",
      "bid": 12,
    },
    {
      "name": "jack",
      "bid": 69,
    },
]

maxBid = -1
maxBidName = "";

for i in range(0, len(bidders)):

    bid = bidders[i]["bid"]
    name = bidders[i]["name"]

    if bid > maxBid:
        maxBid = bid
        maxBidName = name
        

print(f"Congratulations {maxBidName}! Your bid of ${maxBid} wins!")

I believe what you're looking for is bid = i['bid'] .我相信您正在寻找的是bid = i['bid'] i in this case is simply part of the dictionary.在这种情况下, i只是字典的一部分。 So for each entry, you want the value of 'bid' .因此,对于每个条目,您都需要'bid'的值。

there are few mistakes, i think this will works几乎没有错误,我认为这会起作用

bidders = [
{
  "name": "nicholas",
  "bid": 12,
},
{
  "name": "jack",
  "bid": 69,
},
]

highestBid = 0
highestBidData = {}

for i in bidders:
    if i['bid'] > highestBid:
        highestBid = i['bid']
        highestBidData = i
        
print("Congratulations {0}! Your bid of {1} `enter code 
here`wins!".format(highestBidData['name'],highestBidData['bid']))
bidders = [
{
  "name": "nicholas",
  "bid": 12,
},
{
  "name": "jack",
  "bid": 69,
},
]

maxBidder = max(bidders, key=lambda x:x['bid'])

print("Congratulations {}! Your bid of {} wins!".format(maxBidder['name'],maxBidder['bid']))

In a for loop with python is 'i' not automatically an integer?在带有 python 的 for 循环中,“我”不会自动成为 integer?

Example: for i in dictionary: Does i not equal 0, 1, 2 so on?示例:对于字典中的 i:i 不等于 0、1、2 等等吗?

No

A very easy way to find out what it is is simply:一个非常简单的方法来找出它是什么:

for i in bidders:
    print(i)
    print(type(i))

Output: Output:

{'name': 'nicholas', 'bid': 12}
<class 'dict'>
{'name': 'jack', 'bid': 69}
<class 'dict'>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我正在尝试打印出最高出价者第 2 部分 *初学者* - I am trying to print out the highest bidder Part 2 *Beginner* 我试图打印最高的奇数,但给我的 y 和 z 值最高的数字(即使是偶数)会产生问题吗? - I am trying to print the highest odd number but giving my y and z value the highest number (even if its even) creates an issue? 打印出带有条件列表的字典 - Print out dictionaries with list with a condition 我正在尝试更新 python 中的字典列表 - I am trying to do an update for a list of dictionaries in python 我正在尝试从字典列表中获取两个键 - I am trying to get both keys from a list of dictionaries 我正在尝试将字符所说的行添加到空列表中并将其打印出来 - I am trying to add lines said by a character to an empty list and print it out 我试图弄清楚如何添加到字典列表中,而不是创建一个字典列表列表 - I'm trying to figure out how to add to a list of dictionaries, rather than create a list of lists of dictionaries 打印字典中最小列表中的最大值 - Print the highest value out of the minimums of list in dictionary 我正在尝试使用 random.randint 打印列表的随机索引,但我让列表索引超出范围 - I am trying to print a random index of the list using random.randint, but Im getting list index out of range 我正在尝试打印出带有各自乐谱的琴弦 - I am trying to print out the strings with their respective scores
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM