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 :)
7 1452 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 :)
- print max([(len(i), i) for i in listOfLists])[1]
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 - >>> a = [2, 4, 6, 6]
-
>>> b = [7, 8, 9]
-
>>> c = ["66", "77", "88", "9000"]
-
>>> d = [b, c, a]
-
>>> largest = d[0]
-
for items in d:
-
x = len(items)
-
y = len(largest)
-
if x > y:
-
largest = items
-
-
-
>>> print largest
-
['66', '77', '88', '9000']
- 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.
-
>>> alist=[[1,2],[4,5,6,7],[1],[5,6,7,8]]
-
>>> max(alist)
-
[5, 6, 7, 8]
-
-
>>> alist=[[1,2],[4,5,6,7],[1],[5,6,7,8]]
-
>>> max(alist)
-
[5, 6, 7, 8]
-
I did the following - >>> a = [5, 6, 7]
-
>>> b = ["6", "6"]
-
>>> c = [a, b]
-
>>> max(c)
-
['6', '6']
-
>>>
Does it work only for lists with the same type of data?
bvdet 2,851
Expert Mod 2GB -
>>> alist=[[1,2],[4,5,6,7],[1],[5,6,7,8]]
-
>>> max(alist)
-
[5, 6, 7, 8]
-
I think the OP is looking for the longest list. - >>> listOfLists = [[1,2,3], [3,4], [5,6,7,8], [0,0,0,0,0,0,0,0,0]]
-
>>> max(listOfLists)
-
[5, 6, 7, 8]
-
>>> print max([(len(i), i) for i in listOfLists])[1]
-
[0, 0, 0, 0, 0, 0, 0, 0, 0]
I think the OP is looking for the longest list. - >>> listOfLists = [[1,2,3], [3,4], [5,6,7,8], [0,0,0,0,0,0,0,0,0]]
-
>>> max(listOfLists)
-
[5, 6, 7, 8]
-
>>> print max([(len(i), i) for i in listOfLists])[1]
-
[0, 0, 0, 0, 0, 0, 0, 0, 0]
oh my bad for misreading.
Sign in to post your reply or Sign up for a free account.
Similar topics
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?
...
|
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...
|
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...
|
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...
|
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...
|
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
|
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...
|
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...
|
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>
|
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 = ,
...
|
by: Mushico |
last post by:
How to calculate date of retirement from date of birth
|
by: DJRhino |
last post by:
Was curious if anyone else was having this same issue or not....
I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM)
The start time is equivalent to 19:00 (7PM) in Central...
|
by: Aliciasmith |
last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
|
by: tracyyun |
last post by:
Hello everyone,
I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
|
by: NeoPa |
last post by:
Hello everyone.
I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report).
I know it can be done by selecting :...
|
by: NeoPa |
last post by:
Introduction
For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
|
by: NeoPa |
last post by:
Introduction
For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
|
by: isladogs |
last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, Mike...
| |