Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Python中检查列表长度并打印最长和最短列表的名称_Python_Python 3.x_List_Python 2.7 - Fatal编程技术网

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

在Python中检查列表长度并打印最长和最短列表的名称,python,python-3.x,list,python-2.7,Python,Python 3.x,List,Python 2.7,嘿,我有一个项目,我正在生成1到4个49次的随机数,并将它们添加到列表中。这些数字应该代表smartie的一种颜色。然后,我不得不将该列表的结果分成它们自己的列表,我也设法做到了这一点。但现在它要我按长度比较这些列表,并打印出最长和最短列表的名称。(哪种颜色最多,哪种颜色最少。)这是我试过的。我真的不知道该怎么做 list = open('list.txt', 'w') fields = None redList = [] blueList = [] yellowList = [] gree

嘿,我有一个项目,我正在生成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,"." )


list=open('list.txt','w')
字段=无
红色列表=[]
蓝名单=[]
黄色列表=[]
绿名单=[]
最大列表=0
smallestList=0
对于范围(49)内的计数:
randNum=random.randint(1,4)
如果randNum==1:
smartyColor=‘红色’
list.write('1')
elif randNum==2:
smartyColor=‘蓝色’
list.write('2')
elif randNum==3:
smartyColor=‘绿色’
list.write('3')
elif randNum==4:
smartyColor=‘黄色’
list.write('4')
list.close()
list=open('list.txt','r')
对于列表中的行:
fields=line.split()
对于字段中的字段:
如果字段==“1”:
redList.append(字段)
elif字段==“2”:
blueList.append(字段)
elif字段==“3”:
greenList.append(字段)
elif字段==“4”:
yellowList.append(字段)
如果红色列表==蓝色列表:
打印(“红色智能体和蓝色智能体一样多。”)
elif红色列表==绿色列表:
打印(“红色智能体和绿色智能体一样多。”)
elif redList==黄色列表:
打印(“红色智能体和黄色智能体一样多。”)
如果蓝名单==绿名单:
打印(“蓝色智能体和绿色智能体一样多。”)
elif蓝名单==黄名单:
打印(“蓝色智能体和黄色智能体一样多。”)
如果绿名单==黄名单:
打印(“绿色智能体的数量与黄色智能体的数量相同。”)
如果红色列表>最大列表:
biggestList=redList
elif blueList>最大列表:
biggestList=blueList
elif绿色列表>最大列表:
biggestList=绿名单
其他:
最大列表=黄色列表
打印(“最大的列表是”,最大的列表“.”)
如果redList
您不能将>用于两个列表,您必须执行以下操作:

如果您有:

if list_a > list_b:
替换为:

if len(list_a)>len(list_b):

你的问题本质上是:

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

给你:

def print_biggest_and_smallest(mylists):
    mylists.sort(key = lambda x:len(x))
    smallest = mylists[0]
    biggest = mylists[-1]
    print(smallest, biggest)
l=[]
对于范围(49)内的i:
l、 追加(random.randint(1,4))
颜色=[]、[]、[]、[]、[]
对于l中的i:
颜色[int(i)-1]。追加(i)
长度_颜色=[len(i)表示颜色中的i]
最小值,最大值=0,0
对于范围(1,透镜(颜色))中的i:
如果长度颜色[min]>长度颜色[i]:
min=i
elif length\u colors[max]

如果您可以使用numpy,那么您可以只使用np.argmax/np.argmin。

不应使用其他模块,如numpy和Pandas?您在比较列表,您应该比较长度@BrunoMello如何在比较中指定列表长度?请特别重复介绍教程。“我不知道如何做到这一点”建议您需要更多使用适用数据结构的练习,或者可能需要一位当地朋友帮助您完成问题分析。这不是堆栈溢出问题。也见,;这段代码的大部分内容与您的问题无关。您可以使用
len()
哦,天哪,我以前试过使用这个简单的XDD,但我没有得到括号。泰先生!是的,很管用。现在我只需要知道如何打印出存储在变量中的列表的名称,而不是整个列表。有什么想法吗?你可以在论坛上再问一个问题,这样会更有条理
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")