【问题标题】:Iterate permutation per row per item每个项目的每行迭代排列
【发布时间】:2018-01-29 02:31:27
【问题描述】:

我想使用 ggnet 操纵数据进行网络分析。

数据集为 csv 格式,如下所示:

offers
{9425, 5801, 18451, 17958, 16023, 7166}
{20003, 17737, 4031, 5554}
{19764, 5553, 5554}

我想打破数组,并迭代以将每行的所有项目排列为一对 2。所以最终输出应该如下所示:

print list(itertools.permutations([1,2,3,4], 2)) per row to create: 

(9425, 5801)
(9425, 18451)
(9425, 17958)
(9425, 16023)
(9425, 7166)
(5801, 18451)
(5801, 17958)
(5801, 16023)
(5801, 7166)
...

我可以使用 R 或 Python 来执行此操作。 有什么解决这个问题的建议吗?

【问题讨论】:

  • 您已经找到itertools.permutations。你还在寻找什么?
  • 这是关于导入数据和/或排列的问题吗?

标签: python r iteration permutation


【解决方案1】:

另一个 R 解决方案,假设您的文件中有更多行。

# read in csv file as list of integers (each row in csv = 1 list element)
offers <- readLines("offers.csv") %>% strsplit(",") %>% lapply(as.integer)

# create permutation pairs for each element in the list
permutation.list <- lapply(seq_along(offers), function(i) {t(combn(offers[[i]], m = 2))})

# combine all permutation pairs into 1 data frame
permutation.data.frame <- plyr::ldply(permutation.list, data.frame)

以下是根据提供的样本数据得出的结果:

> permutation.list
[[1]]
       [,1]  [,2]
 [1,]  9425  5801
 [2,]  9425 18451
 [3,]  9425 17958
 [4,]  9425 16023
 [5,]  9425  7166
 [6,]  5801 18451
 [7,]  5801 17958
 [8,]  5801 16023
 [9,]  5801  7166
[10,] 18451 17958
[11,] 18451 16023
[12,] 18451  7166
[13,] 17958 16023
[14,] 17958  7166
[15,] 16023  7166

[[2]]
      [,1]  [,2]
[1,] 20003 17737
[2,] 20003  4031
[3,] 20003  5554
[4,] 17737  4031
[5,] 17737  5554
[6,]  4031  5554

[[3]]
      [,1] [,2]
[1,] 19764 5553
[2,] 19764 5554
[3,]  5553 5554

> permutation.data.frame
      X1    X2
1   9425  5801
2   9425 18451
3   9425 17958
4   9425 16023
5   9425  7166
6   5801 18451
7   5801 17958
8   5801 16023
9   5801  7166
10 18451 17958
11 18451 16023
12 18451  7166
13 17958 16023
14 17958  7166
15 16023  7166
16 20003 17737
17 20003  4031
18 20003  5554
19 17737  4031
20 17737  5554
21  4031  5554
22 19764  5553
23 19764  5554
24  5553  5554

【讨论】:

    【解决方案2】:

    你可以在R试试这个:

    a <- c(9425, 5801, 18451, 17958, 16023, 7166)
    b <- c(20003, 17737, 4031, 5554)
    c <- c(19764, 5553, 5554)
    
    rbind(t(combn(a,2)),
    t(combn(b,2)),
    t(combn(c,2)))
    

    【讨论】:

      【解决方案3】:
      t(do.call(cbind,mapply(combn,list(a,b,c),2)))
             [,1]  [,2]
       [1,]  9425  5801
       [2,]  9425 18451
       [3,]  9425 17958
       [4,]  9425 16023
       [5,]  9425  7166
       [6,]  5801 18451
       [7,]  5801 17958
       [8,]  5801 16023
       [9,]  5801  7166
      [10,] 18451 17958
      [11,] 18451 16023
      [12,] 18451  7166
        :     :      :
        :     :      :
      

      【讨论】:

        【解决方案4】:

        您已经有了排列的解决方案。要破坏数组并合并它,请逐行打开 csv 读取并附加到列表。

        from itertools import chain
        import itertools
        #Create Empty Dictionary
        list= []
        for i, eline in enumerate(CSVfile.readlines()):
            list.append(eline.strip())
        MergedArray= {i for j in (list) for i in j}
        #Use your permutations code below
        print list(itertools.permutations(MergedArray, 2))
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2017-12-15
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多