【问题标题】:Is there a way to find an element in a list with a unique criteria Python?有没有办法在具有唯一标准 Python 的列表中查找元素?
【发布时间】:2021-06-21 22:06:13
【问题描述】:

由于某种原因 .index 和 .find 在我的程序中不起作用。

基本上,我希望程序根据设置的条件找到列表的索引号。

例如这个列表:

['Synonyms: Pocket Monsters, Indigo League, Adventures on the Orange Islands, The Johto Journeys, Johto League Champions, Master Quest', 'Japanese: ポケットモンスター', 'Type: TV', 'Episodes: 276', 'Status: Finished Airing', 'Aired: Apr 1, 1997 to Nov 14, 2002', 'Premiered: Spring 1997', 'Broadcast: Thursdays at 19:00 (JST)', 'Producers: TV Tokyo, TV Tokyo Music, Studio Jack', 'Licensors: VIZ Media, 4Kids Entertainment', 'Studios: OLM', 'Source: Game', 'Genres: Action, Adventure, Comedy, Kids, Fantasy', 'Duration: 24 min. per ep.', 'Rating: PG - Children', 'Score: 7.341 (scored by 291,570 users)', 'Ranked: #21572', 'Popularity: #287', 'Members: 504,076', 'Favorites: 4,076', '']

我想找到“流派”位置的索引号。我尝试这样做 .index("Genres:") 但没有找到索引号并返回错误。 我需要这个来查找索引号,因为对于本网站上的其他页面,“流派”处于不同的位置

这是我尝试过的,只是返回了一个错误

GIndex = Information.index("Genres")
print (Information[GIndex])
Genre = (Information[GIndex])

【问题讨论】:

  • 虽然列表中没有值为"Genres"的元素。
  • 您的数据表明您最好首先使用 dict。

标签: python


【解决方案1】:

您也可以使用列表推导:

word = 'Genres'
results = [i for i, l in enumerate(lst) if word in l]

【讨论】:

    【解决方案2】:

    要使索引正常工作,它需要完全匹配。这将检查“流派”是否包含在列表中的任何字符串中并打印其索引

    word = 'Genres'
    for i, item in enumerate(lst):
        if word in item:
            print(i, item)
    

    你可以很容易地把它变成一个函数来返回 i 是索引

    【讨论】:

      【解决方案3】:

      你可以使用enumerate()

      l=['Synonyms: Pocket Monsters, Indigo League, Adventures on the Orange Islands, The Johto Journeys, Johto League Champions, Master Quest', 'Japanese: ポケットモンスター', 'Type: TV', 'Episodes: 276', 'Status: Finished Airing', 'Aired: Apr 1, 1997 to Nov 14, 2002', 'Premiered: Spring 1997', 'Broadcast: Thursdays at 19:00 (JST)', 'Producers: TV Tokyo, TV Tokyo Music, Studio Jack', 'Licensors: VIZ Media, 4Kids Entertainment', 'Studios: OLM', 'Source: Game', 'Genres: Action, Adventure, Comedy, Kids, Fantasy', 'Duration: 24 min. per ep.', 'Rating: PG - Children', 'Score: 7.341 (scored by 291,570 users)', 'Ranked: #21572', 'Popularity: #287', 'Members: 504,076', 'Favorites: 4,076', '']
      for i, s in enumerate(l):
              if "Genres" in s:
                    print(i)
      >>>12
      

      【讨论】:

      • 是的,就是这样
      【解决方案4】:

      在您的列表中没有值为"Genres" 的元素。 "Genres""Genres: Action, Adventure, Comedy, Kids, Fantasy" 不相等。如果你想找到以"Genres"开头的元素,你可以写一个非常简单的for循环,比如

      for index, item in enumerate(information_list):
         if item.startswith('Genres'):
            print(index, item)
            break
      

      @9769953 建议的更好方法是使用字典。在 dict 中查找的效率几乎是恒定的时间,您的数据将整齐地组织,键对应于属性名称,值对应于属性值。

      字典应该是这样的

      information_dict = {
        "Synonyms": "Pocket Monsters, Indigo League, Adventures on the Orange Islands, The Johto Journeys, Johto League Champions, Master Quest",
        "Japanese": "ポケットモンスター",
        "Type": "TV",
        "Episodes": "276",
        "Status": "Finished Airing",
        "Aired": "Apr 1, 1997 to Nov 14, 2002",
        "Premiered": "Spring 1997",
        "Broadcast": "Thursdays at 19:00 (JST)",
        "Producers": "TV Tokyo, TV Tokyo Music, Studio Jack",
        "Licensors": "VIZ Media, 4Kids Entertainment",
        "Studios": "OLM",
        "Source": "Game",
        "Genres": "Action, Adventure, Comedy, Kids, Fantasy",
        "Duration": "24 min. per ep.",
        "Rating": "PG - Children",
        "Score": "7.341 (scored by 291,570 users)",
        "Ranked": "#21572",
        "Popularity": "#287",
        "Members": "504,076",
        "Favorites": "4,076"
      }
      

      通过information_dict["Genres"],你可以获得"Action, Adventure, Comedy, Kids, Fantasy"的值。

      【讨论】:

      • 这是我的目标,但我不知道,让 python 自动完成,因为我正在使用网络抓取来获取信息
      • @ametefe stackoverflow.com/questions/51659976/… 也许这会有所帮助。
      • 我仍然对提供的链接有点困惑。我不必在标签和信息之前滑动字符串吗?如果是这样,我会遇到问题,因为在那个网站上,不同的节目的 HTML 略有不同,这可能会导致问题。不管怎么说,多谢拉。我可能会在一个单独的线程中寻求字典方面的帮助。
      • @ametefe 我们需要更多的上下文。但我确信这绝对是可行的。
      • 这里有什么方法可以私信你吗?
      猜你喜欢
      • 2015-04-10
      • 2013-08-24
      • 1970-01-01
      • 1970-01-01
      • 2010-12-11
      • 2013-02-17
      • 2020-06-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多