  C RUBY-ON-RAILS MYSQL ASP.NET DEVELOPMENT RUBY .NET LINUX SQL-SERVER REGEX WINDOWS ALGORITHM ECLIPSE VISUAL-STUDIO STRING SVN PERFORMANCE APACHE-FLEX UNIT-TESTING SECURITY LINQ UNIX MATH EMAIL OOP LANGUAGE-AGNOSTIC VB6 MSBUILD # R - Combinations of a list WITH duplicates?  » r » R - Combinations of a list WITH duplicates?

By : pengyu.li
Date : November 21 2020, 04:03 PM
Does that help How can I get all the combinations of a list with duplicates. By duplicates I mean an element with itself. I am building a symmetric matrix. , It sounds like you're looking for expand.grid instead of combn: code :
``````expand.grid(names.list, names.list)
#   Var1 Var2
# 1    A    A
# 2    B    A
# 3    C    A
# 4    A    B
# 5    B    B
# 6    C    B
# 7    A    C
# 8    B    C
# 9    C    C
``````
``````library(gtools)
combinations(3, 2, names.list, repeats = TRUE)
#     [,1] [,2]
# [1,] "A"  "A"
# [2,] "A"  "B"
# [3,] "A"  "C"
# [4,] "B"  "B"
# [5,] "B"  "C"
# [6,] "C"  "C"
`````` ## two element combinations of the elements of a list inside lisp (without duplicates)

By : Thinh Nguyen
Date : March 29 2020, 07:55 AM
hope this fix your issue From any given list in lisp, I want to get the two element combinations of the elements of that list without having duplicate combinations ( meaning (a b) = (b a) and one should be removed) , Assuming I'm understanding you correctly, I'd use mapcar and friends.
code :
``````(defun pair-with (elem lst)
(mapcar (lambda (a) (list elem a)) lst))

(defun unique-pairs (lst)
(mapcon (lambda (rest) (pair-with (car rest) (cdr rest)))
(remove-duplicates lst)))
``````
``````CL-USER> (unique-pairs (list 1 2 3 4 5))
((1 2) (1 3) (1 4) (1 5) (2 3) (2 4) (2 5) (3 4) (3 5) (4 5))
CL-USER> (unique-pairs (list :a :b :c :a :b :d))
((:C :A) (:C :B) (:C :D) (:A :B) (:A :D) (:B :D))
``````
``````(defun unique-pairs (lst)
(loop for (a . rest) on (remove-duplicates lst)
append (pair-with a rest)))
`````` ## FAST unique combinations (from list with duplicates) WITHOUT LOOKUPS

By : Eric Noh
Date : March 29 2020, 07:55 AM
around this issue Instead of post-processing/filtering your output, you can pre-process your input list. This way, you can avoid generating duplicates in the first place. Pre-processing involves either sorting (or using a collections.Counter on) the input. One possible recursive realization is:
code :
``````def subbags(bag, k):
a = sorted(bag)
n = len(a)
sub = []

def index_of_next_unique_item(i):
j = i + 1

while j < n and a[j] == a[i]:
j += 1

return j

def combinate(i):
if len(sub) == k:
yield tuple(sub)
elif n - i >= k - len(sub):
sub.append(a[i])
yield from combinate(i + 1)
sub.pop()
yield from combinate(index_of_next_unique_item(i))

yield from combinate(0)

bag = [1, 2, 3, 1, 2, 1]
k = 3
i = -1

print(sorted(bag), k)
print('---')

for i, subbag in enumerate(subbags(bag, k)):
print(subbag)

print('---')
print(i + 1)
``````
``````[1, 1, 1, 2, 2, 3] 3
---
(1, 1, 1)
(1, 1, 2)
(1, 1, 3)
(1, 2, 2)
(1, 2, 3)
(2, 2, 3)
---
6
`````` ## Generating a list of combinations from a list of strings with a common separator without reverse duplicates

By : alluniquepainting
Date : March 29 2020, 07:55 AM
I hope this helps . Make sure currPosition is always smaller than nextPosition: Maybe by changing
code :
``````if currPosition != nextPosition:
``````
``````if currPosition < nextPosition:
`````` ## All 3 value combinations of a list without duplicates

By : one.zhao
Date : March 29 2020, 07:55 AM
hope this fix your issue Is there a way to get all possible 3 value combinations out of something like: , Yes you can try this:-
code :
``````import itertools
a = ['1','2','3']
for i in itertools.permutations(a):
print(''.join(i))
`````` ## How to find all possible combinations from a list with only two elements and no duplicates with Python?

By : Bortolettot
Date : March 29 2020, 07:55 AM
I wish did fix the issue. You can try this, as it makes use of combinations so import it from itertools like this:
code :
``````from itertools import combinations

#also it's good practice not to use list, so for sake of it call it something else
listPlayers = ["player1", "player2", "player3"]

getPairPlayers = sorted(map(sorted, combinations(set(listPlayers), 2)))

print(getPairPlayers)
``````
``````[['player1', 'player2'], ['player1', 'player3'], ['player2', 'player3']]
`````` 