在Python中检查列表长度并打印最长和最短列表的名称

2024-06-25 06:16:41 发布

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

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



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,"." )


Tags: fieldifasarelistthereprintelif
3条回答

您不能使用>;对于两个列表,您必须执行以下操作:

如果您有:

if list_a > list_b:

替换为:

if len(list_a)>len(list_b):
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

你的问题本质上是:

Given a bunch of lists, how I print out the smallest and biggest of them (by size)?

给你:

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

相关问题 更多 >