473,379 Members | 1,367 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,379 software developers and data experts.

lists of lists

brand new to python so please be gentle! i have a function that returns a list of lists, but now i need to access and print the biggest list. i have started off with this:

for list in listoflists:
>>>size = len(list)

and then i want to do something like compare all the sizes and just print the biggest one, but i can't get my head around the syntax. or should i be going about this a completely different way?

any advice gratefully received, thanks :)
Jun 14 '07 #1
7 1459
bvdet
2,851 Expert Mod 2GB
brand new to python so please be gentle! i have a function that returns a list of lists, but now i need to access and print the biggest list. i have started off with this:

for list in listoflists:
>>>size = len(list)

and then i want to do something like compare all the sizes and just print the biggest one, but i can't get my head around the syntax. or should i be going about this a completely different way?

any advice gratefully received, thanks :)
Expand|Select|Wrap|Line Numbers
  1. print max([(len(i), i) for i in listOfLists])[1]
Jun 14 '07 #2
r035198x
13,262 8TB
brand new to python so please be gentle! i have a function that returns a list of lists, but now i need to access and print the biggest list. i have started off with this:

for list in listoflists:
>>>size = len(list)

and then i want to do something like compare all the sizes and just print the biggest one, but i can't get my head around the syntax. or should i be going about this a completely different way?

any advice gratefully received, thanks :)
Hi I'm a beginner as well, here's what I did
Expand|Select|Wrap|Line Numbers
  1. >>> a = [2, 4, 6, 6]
  2. >>> b = [7, 8, 9]
  3. >>> c = ["66", "77", "88", "9000"]
  4. >>> d = [b, c, a]
  5. >>> largest = d[0]
  6. for items in d:
  7.     x = len(items)
  8.     y = len(largest)
  9.     if x > y:
  10.         largest = items
  11.  
  12.  
  13. >>> print largest
  14. ['66', '77', '88', '9000']
Jun 14 '07 #3
r035198x
13,262 8TB
Expand|Select|Wrap|Line Numbers
  1. print max([(len(i), i) for i in listOfLists])[1]
Neat. These lists can get ugly at times but once you get it becomes fun.
Jun 14 '07 #4
ghostdog74
511 Expert 256MB
Expand|Select|Wrap|Line Numbers
  1. >>> alist=[[1,2],[4,5,6,7],[1],[5,6,7,8]]
  2. >>> max(alist)
  3. [5, 6, 7, 8]
  4.  
Jun 14 '07 #5
r035198x
13,262 8TB
Expand|Select|Wrap|Line Numbers
  1. >>> alist=[[1,2],[4,5,6,7],[1],[5,6,7,8]]
  2. >>> max(alist)
  3. [5, 6, 7, 8]
  4.  
I did the following

Expand|Select|Wrap|Line Numbers
  1. >>> a = [5, 6, 7]
  2. >>> b = ["6", "6"]
  3. >>> c = [a, b]
  4. >>> max(c)
  5. ['6', '6']
  6. >>>
Does it work only for lists with the same type of data?
Jun 14 '07 #6
bvdet
2,851 Expert Mod 2GB
Expand|Select|Wrap|Line Numbers
  1. >>> alist=[[1,2],[4,5,6,7],[1],[5,6,7,8]]
  2. >>> max(alist)
  3. [5, 6, 7, 8]
  4.  
I think the OP is looking for the longest list.
Expand|Select|Wrap|Line Numbers
  1. >>> listOfLists = [[1,2,3], [3,4], [5,6,7,8], [0,0,0,0,0,0,0,0,0]]
  2. >>> max(listOfLists)
  3. [5, 6, 7, 8]
  4. >>> print max([(len(i), i) for i in listOfLists])[1]
  5. [0, 0, 0, 0, 0, 0, 0, 0, 0]
Jun 14 '07 #7
ghostdog74
511 Expert 256MB
I think the OP is looking for the longest list.
Expand|Select|Wrap|Line Numbers
  1. >>> listOfLists = [[1,2,3], [3,4], [5,6,7,8], [0,0,0,0,0,0,0,0,0]]
  2. >>> max(listOfLists)
  3. [5, 6, 7, 8]
  4. >>> print max([(len(i), i) for i in listOfLists])[1]
  5. [0, 0, 0, 0, 0, 0, 0, 0, 0]
oh my bad for misreading.
Jun 14 '07 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

9
by: Dave H | last post by:
Hello, I have a query regarding definition lists. Is it good practice semantically to use the dt and dd elements to mark up questions and answers in a frequently asked questions list, or FAQ? ...
41
by: Odd-R. | last post by:
I have to lists, A and B, that may, or may not be equal. If they are not identical, I want the output to be three new lists, X,Y and Z where X has all the elements that are in A, but not in B, and...
3
by: s_subbarayan | last post by:
Dear all, 1)In one of our implementation for an application we are supposed to collate two linked lists.The actual problem is like this: There are two singularly linked lists, the final output...
1
by: Simon Forman | last post by:
I've got a function that I'd like to improve. It takes a list of lists and a "target" element, and it returns the set of the items in the lists that appear either before or after the target...
17
by: Gal Diskin | last post by:
Hi, I am writing a code that needs to iterate over 3 lists at the same time, i.e something like this: for x1 in l1: for x2 in l2: for x3 in l3: print "do something with", x1, x2, x3 What I...
16
by: Michael M. | last post by:
How to find the longst element list of lists? I think, there should be an easier way then this: s1 = s2 = s3 = if len(s1) >= len(s2) and len(s1) >= len(s3): sx1=s1 ## s1 ist längster
51
by: Joerg Schoen | last post by:
Hi folks! Everyone knows how to sort arrays (e. g. quicksort, heapsort etc.) For linked lists, mergesort is the typical choice. While I was looking for a optimized implementation of mergesort...
0
by: Joeyeti | last post by:
Hi fellow VB knowers (I am but a learner still). I have a question for you which I struggle with. I need to convert nested Lists in MS WORD (whether numbered or bulleted or mixed) from their...
3
by: =?Utf-8?B?QWRhbSBN?= | last post by:
Hi all, What is the syntax for a method that accepts a list of generic lists? For example, I have a List that contains the following lists: List<Car> List<Boat> List<Plane>
3
by: Qbert16 | last post by:
Hi, I'm quite new to python and am looking for help with lists inside lists This is an example I'm trying to do. I have the following thesaurus set... thesaurus = , ...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.