简体   繁体   中英

Fastest way to iterate over a large list containing strings in Python?

I have a large list that contains 30000 other lists. The large list contains a list of a string and two other lists. Here is an example:

large_list = [
    ...
    ['maj', [4, 7], ['3', '5']],
    ['maj7', [4, 7, 11], ['3', '5', '7']],
    ...
    ]

I want to iterate through this large_list and find the smaller list by looking for the name (maj, maj7, etc.).

What is the fastest way of doing this?

I had thought to put the list in a.txt file and reading line by line, but I don't really know if that's faster. All I know is it takes less of a toll on my IDE, as having a list of 30000 lines makes any coding sluggish.

import pandas
# convert it to a dataframe
df = pandas.DataFrame(largeList)
#select all the rows where the zeroth element starts with "maj"
mask = df[0].str.startswith("maj")
print(df[mask]) 

Hello Bud,

I am pretty new to Python, however, in this case, my best guess to find the smallest of them all sublist in this gigantic monster called large_list is by using the code shared below:

large_list=[['maj', 4, 7], ['3', '5'], ['maj9', 7], ['maj3', 100], ['maj4', 1360], ['maj7', 4, 7, 11], ['3', '5', '7']]
def getTheMinGuy(mylist):
    minimum = min(mylist)
    return print(f"The smaller sublist on this list is => {minimum}")
getTheMinGuy(large_list)

I certainly hope this helps finding the best solution for this issue, champion. Cheers!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM