循环遍历轨道关键词,并跳转到循环结构的适当部分

2024-06-01 09:42:54 发布

您现在位置:Python中文网/ 问答频道 /正文

我的repl

(针对各种语言用例的各种JSON文件。)

import json

# v = "64457.json" #jp
# v = "35777.json" #en, jp, ro
v = "66622.json" #ge, jp, ro
# v = "50900k.json" #ko
# v = "25364c.json" #ch, en

with open(v) as f:
    data = json.load(f)

track_list = data['discs'][0]['tracks']
langs = ('German', 'French' , 'Korean', 'Chinese')

for x, track in enumerate(track_list):
  for i in track['names']:
    print i, len(track["names"].keys())
    if i not in langs:
      print "NOT I"
      if len(track["names"].keys()) == 3 and i in ('Romaji', 'Japanese', 'English'):
        d = track["names"]["Romaji"]
        s = track["names"]["Japanese"]
        y = track["names"]["English"]
        print '-', x+1, y, d, s
        break
    elif len(track["names"].keys()) == 3 and i in langs and i in ('Romaji', 'Japanese'):
        d = track["names"]["Romaji"]
        s = track["names"]["Japanese"]
        y = track["names"][i]
        print '@', x+1, y, d, s
        break
    elif len(track["names"].keys()) == 2 and i in langs:
      y = track["names"][i]
      e = track["names"]["English"]
      print '+', x+1, y, e
      break
  else:
    print '~', x+1, track["names"].values()[0]

打印错误

German 3
Japanese 3
NOT I
Traceback (most recent call last):
  File "main.py", line 25, in <module>
    y = track["names"]["English"]
KeyError: 'English'

我想做的

我正在尝试遍历track['names']中的每个键。一旦它命中了一个同样在langs元组中的键,我希望它停止并转到if节中相应的节。如果它不在langs元组中,它应该输出它所拥有的信息,特别是如果键是Eng、Rom、Jpn。我想所有的循环都把我搞糊涂了


Tags: andinjsonlenifenglishnamestrack
2条回答

我不完全理解您关心哪些条件,但这里有一些代码可以修改以满足您的需要。你有太多for循环,所以我拿出一个,然后设置一些布尔变量,你可以使用

track_list = data['discs'][0]['tracks']
langs = ('German', 'French' , 'Korean', 'Chinese')

for x, track in enumerate(track_list):
  # below variable is an int with the number of names the track has
  num_names = len(track["names"].keys())
  # below variable is true if one of the track's names is in a lang language
  name_in_lang_bool = any([lang in langs for lang in track["names"].keys()])
  # below variable is true if one of the track's names is in Romaji or English
  name_english_romaji_bool = any([lang in ("English", "Romaji") for lang in track["names"].keys()])

  # you can combine these variables for various conditionals like so: 
  if name_in_lang_bool and num_names == 3 and name_english_romaji_bool:
    print "this track has three names, one of them is in langs, and one of them is either English OR Romaji"

    # this is how you print out all the track's names: 
    for (language, name) in track["names"].iteritems():
      print "name in", language, "is", name

  # here's another example condition 
  elif name_in_lang_bool and num_names == 2:
    print "this track has two names, one of them is in langs"
    for (language, name) in track["names"].iteritems():
      print "name in", language, "is", name

我认为看到一种完全不同的方法来解决这个问题可能会有所帮助:

  • track["names"]中有键(我假设这是一本字典)
  • 那你想要什么
    • 属于langs的键,以及
    • 不属于langs的键

考虑一下:

keys_in_langs = filter(lambda key: key in langs, track["names"].keys())
vice_versa = filter(lambda key: key not in langs, track["names"].keys())

filter将lambda函数应用于keys中的每个项,返回匿名函数返回true的列表。这是你需要的吗

相关问题 更多 >