自动填充dict python 2.x

时间:2017-07-10 14:47:15

标签: python-2.7

我对Python和编程一般都很陌生,所以如果这是非常基本的或者之前已经被问过和回答,那么道歉。以下是我正在使用的数据示例:

ffmpeg version 2.4.3-1ubuntu1~trusty6 Copyright (c) 2000-2014 the FFmpeg developers
  built on Nov 22 2014 17:07:19 with gcc 4.8 (Ubuntu 4.8.2-19ubuntu1)
  configuration: --prefix=/usr --extra-version='1ubuntu1~trusty6' --build-suffix=-ffmpeg --toolchain=hardened --extra-cflags= --extra-cxxflags= --libdir=/usr/lib/x86_64-linux-gnu --shlibdir=/usr/lib/x86_64-linux-gnu --incdir=/usr/include/x86_64-linux-gnu --enable-gpl --enable-shared --disable-stripping --enable-avresample --enable-avisynth --enable-fontconfig --enable-gnutls --enable-ladspa --enable-libass --enable-libbluray --enable-libbs2b --enable-libcaca --enable-libcdio --enable-libflite --enable-libfreetype --enable-libfribidi --enable-libgme --enable-libgsm --enable-libmodplug --enable-libmp3lame --enable-libopenjpeg --enable-libopus --enable-libpulse --enable-librtmp --enable-libschroedinger --enable-libshine --enable-libspeex --enable-libssh --enable-libtheora --enable-libtwolame --enable-libvorbis --enable-libvpx --enable-libwavpack --enable-libwebp --enable-opengl --enable-x11grab --enable-libxvid --enable-libx265 --enable-libdc1394 --enable-libiec61883 --enable-libzvbi --enable-libzmq --enable-frei0r --enable-libx264 --enable-libsoxr --enable-openal --enable-libopencv
  libavutil      54.  7.100 / 54.  7.100
  libavcodec     56.  1.100 / 56.  1.100
  libavformat    56.  4.101 / 56.  4.101
  libavdevice    56.  0.100 / 56.  0.100
  libavfilter     5.  1.100 /  5.  1.100
  libavresample   2.  1.  0 /  2.  1.  0
  libswscale      3.  0.100 /  3.  0.100
  libswresample   1.  1.100 /  1.  1.100
  libpostproc    53.  0.100 / 53.  0.100
Hyper fast Audio and Video encoder

--------------------------------------

我希望自动填充已定义的名称,就像我在这里手动完成的那样:

    {
    "homeTeam": {
     "formation": [
      "4",
      "4",
      "2"
    ],
    "lineupsSorted": [
      {
    "player": {
      "name": "Scott P. Brown",
      "slug": "scott-p-brown",
      "shortName": "S. P. Brown",
      "id": 19889,
      "hasImage": true
    },
    "position": 1,
    "shirtNumber": 1,
    "substitute": false,
    "positionName": "Goalkeeper",
    "positionNameshort": "G",
    "captain": false
  },
  {
    "player": {
      "name": "Carl Winchester",
      "slug": "carl-winchester",
      "shortName": "C. Winchester",
      "id": 110785,
      "hasImage": true
    },
    "position": 2,
    "shirtNumber": 27,
    "substitute": false,
    "positionName": "Midfielder",
    "positionNameshort": "M",
    "captain": false
  },

由于我将使用不同的数据(相同的结构)重复此过程数百次,我想知道是否有人可以给我一些关于自动构建这些范围的提示?

谢谢,

彼得

1 个答案:

答案 0 :(得分:0)

I'm not sure I understood what is the problem you are trying to solve but I'll try to help.

Assuming you have a dictionary team_dict and you want to create 2 list: hometeamPositions and hometeamPlayers you can use the following code:

hometeamPlayers = []
hometeamPositions = []

for player_dict in teams_dict['homeTeam']['lineupsSorted']:
    hometeamPlayers.append(player_dict['player']['shortName'])
    hometeamPositions.append(player_dict['positionName'])

The output on your example will be:

hometeamPlayers = ['S. P. Brown', 'C. Winchester']
hometeamPositions = ['Goalkeeper', 'Midfielder']