标签: list-manipulation

将函数应用于R中的数据帧列表

我需要有关如何以迭代方式管理列表的帮助.

我有以下列表list,它由几个具有相同列但行数不同的数据帧组成.例:

[[1]]
  id InpatientDays ERVisits OfficeVisits Narcotics
1  a             0        0           18         1
2  b             1        1            6         1
3  c             0        0            5         3
4  d             0        1           19         0
5  e             8        2           19         3
6  f             2        0            9         2

[[2]]
    id InpatientDays ERVisits OfficeVisits Narcotics
7   a            16        1            8         1
8   b             2        0            8         0
9   c             2        1            4         3
10  d             4        2            0         2
11  e             6        5 …
Run Code Online (Sandbox Code Playgroud)

r list list-manipulation

3
推荐指数
1
解决办法
154
查看次数

如何将字典中的列表元素相乘

我有两个字典,如下所述,我需要将字典列表中的每个元素与其他字典列表中的相应元素相乘并打印结果。我已经成功地增加了一个列表,如何使其动态化?

dict1 = {0: [1, 1, 0, 1, 1, 0], 1: [1, 0, 1, 1, 1, 0]}

dict2 = { 0: [16, 0, 2, 0, 0, 0], 1: [15, 0, 0, 0, 1, 0]}

result = { 0: [16, 0, 0, 0, 0, 0], 1:[15, 0, 0, 0, 1, 0]}

from operator import mul
result = list( map(mul, dict1[0], dict2[0]) )
Run Code Online (Sandbox Code Playgroud)

python dictionary list list-manipulation

3
推荐指数
1
解决办法
1418
查看次数

Scala - 在保持相同类型的同时展开各种类型的列表

我有一个列表,有各种类型,如:

val data: List[(String, Int, List[String])] = List(("foo", 1, List("a", "b")), ("bar", 2, List("c", "d")), ("baz", 1, List("e", "f")))

如果Int值大于1,我试图为该范围内的每个Int复制那些项(从1到Int) - 理想情况下它看起来像(强调Int需要从1开始每次迭代递增)到Int):

val dataExpanded: List[(String, Int, List[String])] = List(("foo", 1, List("a", "b")), ("bar", 1, List("c", "d")), ("bar", 2, List("c", "d")), ("baz", 1, List("e", "f")))

在scala中使用类型通常会让我头脑发热,尤其是嵌套结构,因为每个操作似乎都会引入越来越多的类型陷阱.

所以当我尝试时:

val dataExpanded = data.map{ case (s, i, l) =>

    if (i > 1) {

        List.range(1, i+1).map { n =>
            (s, n, l)
        }

    } else {
        (s, i, l)
    }
} 
Run Code Online (Sandbox Code Playgroud)

我得到了结果: …

types scala list-manipulation

2
推荐指数
1
解决办法
471
查看次数

Python中列表列表的简洁快速的zip操作?

有没有更简洁,更有效的方法zip在Python中实现以下操作?我将列表列表组合在一起以创建新列表,如下所示:

>>> a
[[1, 2, 3], [4, 5, 6]]
>>> b
[[10, 11, 12], [13, 14, 15]]
>>> for x, y in zip(a, b):
...   print zip(*[x, y])
... 
[(1, 10), (2, 11), (3, 12)]
[(4, 13), (5, 14), (6, 15)]
Run Code Online (Sandbox Code Playgroud)

谢谢.

python algorithm list list-manipulation

1
推荐指数
1
解决办法
895
查看次数

是否有一个R等同于Mathematica的Sow和Reap功能用于构建列表?

Mathematica有一种有趣的方法,可以逐步构建一个列表(或多个列表),您可以在复杂计算的各个点计算结果.我想在R里做类似的事情.

在Mathematica中,您可以Sow通过在调用函数时包装整个计算来收集计算期间每个函数调用的每个参数的列表Reap.R是否具有这些功能的等价物?您是否可以使用环境和<<-运营商来模拟它,或者范围规则是否允许它?

编辑:这是一个人为的例子.假设我想生成一个多维数据集的总和,但我还想收集我用来制作多维数据集总和的数字的平方.我知道可能有更多惯用的方法来进行这种精确计算,但它代表了在收集沿途生产的各种物品时获得最终答案.

reap(sum(sapply(1:100, function(i) { sow(squares = i * i); i * i * i }))
Run Code Online (Sandbox Code Playgroud)

我希望这能返回具有多维数据集总和以及包含正方形列表的命名变量"squares"的内容.

wolfram-mathematica r list-manipulation

1
推荐指数
1
解决办法
346
查看次数

如何在python中从一个列表中创建多个列表

我想在条件库中从一个列表中创建多个列表.

实际数据:

numbers = [1, 2, 3,4,5,6,7,8,9, 1, 11, 12, 13, 1, 21, 22, 25, 6, 1, 34 ,5 ,6 ,7,78]
Run Code Online (Sandbox Code Playgroud)

预期结果:

[1, 2, 3,4,5,6,7,8,9]
[1, 11, 12, 13]
[1, 21, 22, 25, 6]
[1, 34 ,5 ,6 ,7,78]
Run Code Online (Sandbox Code Playgroud)

这是我的尝试:

list_number=[]
numbers = [1, 2, 3,4,5,6,7,8,9, 1, 11, 12, 13, 1, 21, 22, 25, 6, 1, 34 ,5 ,6 ,7,78]
for x in numbers:
    if x==1:
        list_number.append(numbers)

print list_number[0] 
Run Code Online (Sandbox Code Playgroud)

python list-manipulation python-2.7

1
推荐指数
1
解决办法
424
查看次数