【问题标题】:Applying functions/methods within each sublist, not to the list of sublists as a whole in python在每个子列表中应用函数/方法,而不是在 python 中作为一个整体的子列表列表
【发布时间】:2017-10-12 02:22:59
【问题描述】:

我已经尝试了一些方法来正确实现此功能,但我尝试的任何方法似乎都不起作用。我想我对要做什么有了一些想法,但是我想怎么做却没有成功。所以我已经有了一个名为 string_avg(L) 的函数,我想我可以将它用于这个函数。 string_avg(L) 由以下代码定义:

      '''(list of str) -> list of list
    Given a list of strings where each string has the format:
    'name, grade, grade, grade, ...' return a new list of 
    lists where each inner list has the format :
[name (str), average grade (float)]
Requirement: This function should be 1 line long.
>>> string_avg(['Jo, 92, 80', 'Bill, 60, 70', 'Cal, 98.5, 100, 95.5, 98'])
[['Jo', 86.0], ['Bill', 65.0], ['Cal', 98.0]]
'''

但现在我需要为以下内容编写代码:

def string_avg_update(L):
'''(list of str) -> NoneType
Given a list of strings where each string has the format:
'name, grade, grade, grade, ...' update  the given 
list of strs to be a list of floats where each item 
is the average of the corresponding numbers in the 
string. Note this function does NOT RETURN the list.
>>> L = ['Jo, 50, 92, 80', 'Bill, 60, 70', 'Cal, 98.5, 100, 95.5, 98']
>>> string_avg_update(L)
>>> L 
[89.0, 65.0, 98.0]
'''

所以要获得 string_avg_update(L),我可能会使用 string_avg(L) 这会给我 [['Jo', 86.0], ['Bill', 65.0], [' Cal', 98.0]] ,然后从每个子列表中删除名称并将每个列表中剩余的 int 连接到一个列表中。有没有办法做到这一点?我不知道如何删除列表中的第一个字符,但知道 list.pop() 删除列表中的最后一个字符,所以如果我使用 list.reverse() 将数字放在每个子列表中的第一位,我可以删除名称,然后尽可能将子列表连接在一起。但是,使用 list.reverse 会颠倒子列表的顺序(即 Cal 排在第一位),而不是颠倒每个子列表中的项目?有没有办法让每个列表都反转,所以我可以使用 list.pop() (如果我能弄清楚如何在每个子列表上使用它)。

或者有没有办法从列表中只删除字符串,这样我就可以加入所有子列表,然后删除名称。 或者最好的想法是使用 del 从每个子列表中删除名称(即使用 del list[0]),但我不知道如何让 del 遍历每个子列表如果我将它用作 del list[0] 它将删除第一个子列表而不是每个子列表中的第一个字符。

我想这里的一个关键问题是如何将函数/方法应用于每个单独的子列表,而不是作为列表应用于子列表。

谢谢!!

【问题讨论】:

  • 这似乎是使用字典的最佳场所?
  • 您应该真正考虑一下这里的列表理解。考虑一下如何从任何 one 列表中选择第二个元素(等级):L[1],对吗?那么我怎么能从 every 子列表中选择那个元素呢?
  • 我想这是我的问题.. 如果我使用“for sublist in L:”然后在它下面写一些关于只选择 L[1] 或删除 L[0] 的内容,它是否有效?
  • @Rir8 是的,这可行。你知道list.pop()index 作为参数,对吧?
  • 当我这样做时似乎不起作用,但我会继续尝试类似的不同事情 - 谢谢!

标签: python python-3.x


【解决方案1】:

对于您想要实现的目标,字典可能是一种更好的数据结构。 然而,这是一个适用于列表和单行限制的解决方案:

首先得到名字的平均值:

def av_grade(lst):
    """(list of str)->list of list
    Given a list of strings where each string has the format:
    'name, grade, grade, grade, ...' return a new list of 
    lists where each inner list has the format :
    [name (str), average grade (float)]
    Requirement: This function should be 1 line long.

    >>> avg_grade(['Jo, 92, 80', 'Bill, 60, 70', 'Cal, 98.5, 100, 95.5, 98'])
    [['Jo', 86.0], ['Bill', 65.0], ['Cal', 98.0]]
    """
    return [[x.split()[0], sum(map(float, x.split()[1:]))/(len(x.split()) - 1)] for x in ";".join(lst).replace(",", "").split(";")]

现在获取一个仅包含平均值的平面列表:

def av_grade_update(lst):
    """(list of str)->[float, float, ...]
    Given a list of strings where each string has the format:
    'name, grade, grade, grade, ...' return a new with the format :
    [average grade (float), average grade (float)]
    Requirement: This function should be 1 line long.

    >>> avg_grade_update(['Jo, 92, 80', 'Bill, 60, 70', 'Cal, 98.5, 100, 95.5, 98'])
    [86.0, 65.0, 98.0]
    """
    return [sum(map(float, x.split()[1:]))/(len(x.split()) - 1) for x in ";".join(lst).replace(",", "").split(";")]

请注意,这两个函数都在原始列表上运行。这是为了避免改变原始列表,因为一旦按照您最初想要的方式进行了变异,原始信息就会不可挽回地丢失(至少在程序中)。

【讨论】:

    猜你喜欢
    • 2022-07-04
    • 1970-01-01
    • 1970-01-01
    • 2010-10-04
    • 1970-01-01
    • 2023-02-25
    • 1970-01-01
    • 2017-11-05
    • 1970-01-01
    相关资源
    最近更新 更多