【问题标题】:Checking length of lists and printing name of longest and shortest lists in Python在 Python 中检查列表的长度并打印最长和最短列表的名称
【发布时间】:2020-04-07 23:27:01
【问题描述】:

嘿,我有一个项目,我在其中生成 1 到 4 49 次之间的随机数并将它们添加到列表中。这些数字应该代表聪明人的颜色。然后我不得不将该列表的结果分成他们自己的列表,我也设法做到了。但现在它要我按长度比较这些列表并打印出最长和最短列表的名称。 (哪种颜色最多,哪种颜色最少。)这是我试过的。我真的不知道该怎么做。



list = open('list.txt', 'w')
fields = None
redList = []
blueList = []
yellowList = []
greenList = []
biggestList = 0
smallestList = 0

for count in range(49):
    randNum = random.randint(1, 4)
    if randNum == 1:
        smartyColor = 'Red'
        list.write('1 ')

    elif randNum == 2:
        smartyColor = 'Blue'
        list.write('2 ')

    elif randNum == 3:
        smartyColor = 'Green'
        list.write('3 ')

    elif randNum == 4:
        smartyColor = 'Yellow'
        list.write('4 ')

list.close()

list = open('list.txt', 'r')
for line in list:
    fields = line.split()
for field in fields:
    if field == '1':
        redList.append(field)
    elif field == '2':
        blueList.append(field)
    elif field == '3':
        greenList.append(field)
    elif field == '4':
        yellowList.append(field)

if redList == blueList:
    print("There are as many red smarties as blue smarties.")
elif redList  == greenList:
    print("There are as many red smarties as green smarties.")
elif redList == yellowList:
    print("There are as may red smarties as yellow smarties.")

if blueList == greenList:
    print("There are as many blue smarties as there are green smarties.")
elif blueList == yellowList:
    print("There are as many blue smarties as yellow smarties.")

if greenList == yellowList:
    print("There are as many green smarties as there are yellow smarties.")

if redList > biggestList:
    biggestList = redList
elif blueList > biggestList:
    biggestList = blueList
elif greenList > biggestList:
    biggestList = greenList
else:
    biggestList = yellowList

print("The biggest list was ",biggestList,"." )

if redList < smallestList:
    smallestList = redList.Length
elif blueList < smallestList:
    smallestList = blueList
elif greenList < smallestList:
   smallestList = greenList
else:
   smallestList = yellowList

print("The smallest list was ",smallestList,"." )

【问题讨论】:

  • Numpy、Pandas等其他模块不应该用吗?
  • 你在比较列表,你应该比较长度!
  • @BrunoMello 如何在比较中指定列表长度?
  • 请重复介绍,尤其是how to ask。 “我不知道该怎么做”表明您需要更多有关适用数据结构的练习,或者可能需要当地朋友帮助您完成问题分析。这不是堆栈溢出问题。另见MRE;大部分代码与您的问题无关。
  • 你可以使用len()

标签: python python-3.x list python-2.7


【解决方案1】:

您不能将 > 与 2 个列表一起使用,您必须执行以下操作:

你在哪里:

if list_a > list_b:

替换为:

if len(list_a)>len(list_b):

【讨论】:

  • 哦,我的天哪,我以前试过用那个简单的 XDD,但我没有得到括号。泰先生!
  • 是的,它有效。现在我只需要弄清楚如何打印出存储在变量中的列表的名称,而不是整个列表。有什么想法吗?
  • 你可以在论坛里再问一个问题,这样更有条理
【解决方案2】:

您的问题本质上是:

给定一堆列表,我如何打印出最小和最大的 它们(按大小)?

给你:

def print_biggest_and_smallest(mylists):
    mylists.sort(key = lambda x:len(x))
    smallest = mylists[0]
    biggest = mylists[-1]
    print(smallest, biggest)

【讨论】:

    【解决方案3】:
    l = []
    for i in range(49):
        l.append(random.randint(1,4))
    
    colors = [[],[],[],[]]
    for i in l:
            colors[int(i)-1].append(i)
    
    length_colors= [len(i) for i in colors]
    min, max = 0,0
    
    for i in range(1,len(colors)):
        if length_colors[min] > length_colors[i]:
            min = i
        elif length_colors[max] < length_colors[i]:
            max = i
    
    print(length_colors)
    print("Biggest list = ", colors[max], ",with ", length_colors[max], " elements")
    print("Smallest list = ", colors[min], "with ", length_colors[min], " elements")
    

    如果你可以使用 numpy 会有所帮助,那么你可以只使用 np.argmax / np.argmin。

    【讨论】:

      猜你喜欢
      • 2013-01-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-01-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多