呼叫功能超过列表

时间:2016-07-01 16:58:10

标签: python python-2.7 nested-lists

我正在尝试学习python并且在这个问题上已经解决了问题。

我有一个像这样的嵌套列表,其中包含数字中的分数和整数的混合:

<div class="top">
    </div>
    <div class="bottom">
    </div>

我想遍历&#34;结果&#34;中的每个条目。将分数转换为隐含百分比,然后使用与更新值相同的格式创建新列表。

我在python中创建了以下函数来转换结果中的一个项目:

results = [
{
    "date": "2016-06-30",
    "item1": [
        "9/2",
        "4",
    ],
    "item2": [
        "13/10",
        "6/5"
    ]},

{
    "date": "2016-06-29",
    "item1": "5/4",
    "item2": [
        "9/10",
        "1"
    ],
    ]}}

但是我如何为&#34;结果&#34;?

中的每条记录调用它

我的想法是,我需要为每个项目调用item = results[0]["item1"] emptyList = [] def convertOdds(): for i in item1: if "/" in i: winnings = int(i.rsplit("/", 1)[0]) stake = int(i.rsplit("/", 1)[1]) total = winnings + stake i = float((stake/total)*100) emptyList.append(round(i,2)) else: winnings = int(i) stake = 1 total = winnings + stake i = float((stake/total)*100) emptyList.append(round(i,2)) ,因此创建另一个函数来遍历列表,为每个项目调用convertOdds(),然后在该函数中创建列表?

但我对如何做到这一点感到茫然。任何帮助非常感谢。

4 个答案:

答案 0 :(得分:0)

首先,有两个'}',你错误地放置了它们。它实际上不是一个列表。顺便问一下,什么是rsplit?我认为你应该只使用拆分。

答案 1 :(得分:0)

假设只有item2list_ = [] for dicts in results: item_1 = dicts["item1"] // pass item_1 in the method, append to list_ item_2 = dicts["item2"] // pass item_2 in the method, list_ is updated print list_ 个键包含浮点数或整数列表,您可以试试这个:

{{1}}

答案 2 :(得分:0)

最后一部分是解决方案的关键。您需要访问dict键中的键和值:值对才能测试“item”的键,并在值匹配时对值执行操作。希望这可以解决问题,或者至少指出你正确的方向。

results = [
    {
        "date": "2016-06-30",
        "item1": [
            "9/2",
            "4",
        ],
        "item2": [
            "13/10",
            "6/5"
        ]},

    {
        "date": "2016-06-29",
        "item1": "5/4",
        "item2": [
            "9/10",
            "1"
        ]}
    ]


item = results[0]

emptyList = []


def convertOdds(i):
    for x in i:
        if "/" in x:

            winnings = int(x.rsplit("/", 1)[0])
            stake = int(x.rsplit("/", 1)[1])
            total = winnings + stake
            i = float((stake/total)*100)
            emptyList.append(round(i,2))

        else:

            winnings = int(x)
            stake = 1
            total = winnings + stake
            i = float((stake/total)*100)
            emptyList.append(round(i,2))


for k, v in item.items():
    if 'item' in k:
        convertOdds(v)
    else:
        pass

print(emptyList)

输出:

[43.48, 45.45, 18.18, 20.0]

答案 3 :(得分:0)

鉴于对op的这些修订:

results = [
  {
    "date": "2016-06-30",
    "item1": [
        "9/2",
        "4",
    ],
    "item2": [
        "13/10",
        "6/5"
    ]
  },
  {
    "date": "2016-06-29",
    "item1": "5/4",
    "item2": [
        "9/10",
        "1"
    ]
  }
]
# cleaned up original results syntax only,
# note that item1 of the second record is a string.

def convert_odds(item_list):
  """ now takes the item_list as a variable
      returns the transform as a new_list
      *typecast total as a float
  """
  new_list = list()

  for i in item_list:

    if "/" in i:
      winnings = int(i.rsplit("/", 1)[0])
      stake = int(i.rsplit("/", 1)[1])
      total = float(winnings + stake) #*
      i = float((stake/total)*100)
      new_list.append(round(i,2))

    else:
      winnings = int(i)
      stake = 1
      total = float(winnings + stake) #*
      i = float((stake/total)*100)
      new_list.append(round(i,2))

  return new_list

以下内容将迭代结果并转换赔率......

def transform(original_list):
  new_list = list()

  for record in original_list:
    for key in record.keys():

      if 'item' in key:
        item = record.get(key)
        # normalize all items as list objects
        item_list = [item] if not isinstance(item, list) else item

        # call for odds conversion and update the record
        record.update({key:convert_odds(item_list)})

    new_list.append(record)
  return new_list

调用转换将产生以下结果......

from pprint import pprint
pprint(transform(results))

[{'date': '2016-06-30', 'item1': [18.18, 20.0], 'item2': [43.48, 45.45]},
{'date': '2016-06-29', 'item1': [44.44], 'item2': [52.63, 50.0]}]