简体   繁体   English

迭代包含 Python 中字符串的大型列表的最快方法?

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

I have a large list that contains 30000 other lists.我有一个包含 30000 个其他列表的大列表。 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.).我想遍历这个 large_list 并通过查找名称(maj、maj7 等)找到较小的列表。

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.我曾想过将列表放在 a.txt 文件中并逐行读取,但我真的不知道这是否更快。 All I know is it takes less of a toll on my IDE, as having a list of 30000 lines makes any coding sluggish.我所知道的是它对我的 IDE 造成的损失更少,因为拥有 30000 行的列表会使任何编码变得迟缓。

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:我对 Python 很陌生,但是,在这种情况下,我最好的猜测是在这个名为 large_list 的巨大怪物中找到所有子列表中最小的一个,方法是使用下面共享的代码:

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!干杯!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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