python - Iterate permutation per row per item -


i manipulate data network analysis using ggnet.

the dataset in csv form , looks this:

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

what break array, , iterate permute items each row pair of 2. ultimate output should like:

print list(itertools.permutations([1,2,3,4], 2)) per row create:   (9425, 5801) (9425, 18451) (9425, 17958) (9425, 16023) (9425, 7166) (5801, 18451) (5801, 17958) (5801, 16023) (5801, 7166) ... 

i use either r or python this. suggestions solve problem?

another r solution, assuming there more rows in file.

# read in csv file list of integers (each row in csv = 1 list element) offers <- readlines("offers.csv") %>% strsplit(",") %>% lapply(as.integer)  # create permutation pairs each element in list permutation.list <- lapply(seq_along(offers), function(i) {t(combn(offers[[i]], m = 2))})  # combine permutation pairs 1 data frame permutation.data.frame <- plyr::ldply(permutation.list, data.frame) 

below results based on sample data provided:

> 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 

Comments

Popular posts from this blog

ubuntu - PHP script to find files of certain extensions in a directory, returns populated array when run in browser, but empty array when run from terminal -

php - How can i create a user dashboard -

javascript - How to detect toggling of the fullscreen-toolbar in jQuery Mobile? -