Skip to main content
Commonmark migration
Source Link

##Python

Python

###Option 1

Option 1

#!/usr/bin/env python
# get_unique_words.py

import sys

l = []
for w in sys.argv[1].split(','):
  if w not in l:
    l += [ w ]
print ','.join(l)

Make executable, then call from Bash:

$ ./get_unique_words.py "aaa,aaa,aaa,bbb,bbb,ccc,bbb,ccc"
aaa,bbb,ccc

Or you could implement it as a Bash function, but the syntax is messy.

get_unique_words(){
  python -c "
l = []
for w in '$1'.split(','):
  if w not in l:
    l += [ w ]
print ','.join(l)"
}

Option 2

This option can become a one-liner if needed:

#!/usr/bin/env python
# get_unique_words.py

import sys

s_in = sys.argv[1]
l_in = s_in.split(',') # Turn string into a list.
set_out = set(l_in) # Turning a list into a set removes duplicates items.
s_out = ','.join(set_out) 
print s_out

In Bash:

get_unique_words(){
  python -c "print ','.join(set('$1'.split(',')))"
}

##Python

###Option 1

#!/usr/bin/env python
# get_unique_words.py

import sys

l = []
for w in sys.argv[1].split(','):
  if w not in l:
    l += [ w ]
print ','.join(l)

Make executable, then call from Bash:

$ ./get_unique_words.py "aaa,aaa,aaa,bbb,bbb,ccc,bbb,ccc"
aaa,bbb,ccc

Or you could implement it as a Bash function, but the syntax is messy.

get_unique_words(){
  python -c "
l = []
for w in '$1'.split(','):
  if w not in l:
    l += [ w ]
print ','.join(l)"
}

Option 2

This option can become a one-liner if needed:

#!/usr/bin/env python
# get_unique_words.py

import sys

s_in = sys.argv[1]
l_in = s_in.split(',') # Turn string into a list.
set_out = set(l_in) # Turning a list into a set removes duplicates items.
s_out = ','.join(set_out) 
print s_out

In Bash:

get_unique_words(){
  python -c "print ','.join(set('$1'.split(',')))"
}

Python

Option 1

#!/usr/bin/env python
# get_unique_words.py

import sys

l = []
for w in sys.argv[1].split(','):
  if w not in l:
    l += [ w ]
print ','.join(l)

Make executable, then call from Bash:

$ ./get_unique_words.py "aaa,aaa,aaa,bbb,bbb,ccc,bbb,ccc"
aaa,bbb,ccc

Or you could implement it as a Bash function, but the syntax is messy.

get_unique_words(){
  python -c "
l = []
for w in '$1'.split(','):
  if w not in l:
    l += [ w ]
print ','.join(l)"
}

Option 2

This option can become a one-liner if needed:

#!/usr/bin/env python
# get_unique_words.py

import sys

s_in = sys.argv[1]
l_in = s_in.split(',') # Turn string into a list.
set_out = set(l_in) # Turning a list into a set removes duplicates items.
s_out = ','.join(set_out) 
print s_out

In Bash:

get_unique_words(){
  python -c "print ','.join(set('$1'.split(',')))"
}
Set is easier than dict. Also doesn't need to be sorted.
Source Link
wjandrea
  • 722
  • 9
  • 22

##Python

###Option 1

#!/usr/bin/env python
# get_unique_words.py

import sys

l = []
for w in sys.argv[1].split(','):
  if w not in l:
    l += [ w ]
print ','.join(l)

Make executable, then call from Bash:

$ ./get_unique_words.py "aaa,aaa,aaa,bbb,bbb,ccc,bbb,ccc"
aaa,bbb,ccc

Or you could implement it as a Bash function, but the syntax is messy.

get_unique_words(){
  python -c "
l = []
for w in '$1'.split(','):
  if w not in l:
    l += [ w ]
print ','.join(l)"
}

Option 2

This option can become a one-liner if needed:

#!/usr/bin/env python
# get_unique_words.py

import sys

s_in = sys.argv[1]
l_in = s_in.split(',') # Turn string into a list.
l_outset_out = sorted(dict.fromkeysset(l_in)) # Turning a list into a set removes duplicates items.
s_out = ','.join(l_outset_out) 
print s_out

In Bash:

get_unique_words(){
  python -c "print ','.join(sorted(dict.fromkeysset('$1'.split(','))))"
}

##Python

###Option 1

#!/usr/bin/env python
# get_unique_words.py

import sys

l = []
for w in sys.argv[1].split(','):
  if w not in l:
    l += [ w ]
print ','.join(l)

Make executable, then call from Bash:

$ ./get_unique_words.py "aaa,aaa,aaa,bbb,bbb,ccc,bbb,ccc"
aaa,bbb,ccc

Or you could implement it as a Bash function, but the syntax is messy.

get_unique_words(){
  python -c "
l = []
for w in '$1'.split(','):
  if w not in l:
    l += [ w ]
print ','.join(l)"
}

Option 2

This option can become a one-liner if needed:

#!/usr/bin/env python
# get_unique_words.py

import sys

s_in = sys.argv[1]
l_in = s_in.split(',')
l_out = sorted(dict.fromkeys(l_in))
s_out = ','.join(l_out)
print s_out

In Bash:

get_unique_words(){
  python -c "print ','.join(sorted(dict.fromkeys('$1'.split(','))))"
}

##Python

###Option 1

#!/usr/bin/env python
# get_unique_words.py

import sys

l = []
for w in sys.argv[1].split(','):
  if w not in l:
    l += [ w ]
print ','.join(l)

Make executable, then call from Bash:

$ ./get_unique_words.py "aaa,aaa,aaa,bbb,bbb,ccc,bbb,ccc"
aaa,bbb,ccc

Or you could implement it as a Bash function, but the syntax is messy.

get_unique_words(){
  python -c "
l = []
for w in '$1'.split(','):
  if w not in l:
    l += [ w ]
print ','.join(l)"
}

Option 2

This option can become a one-liner if needed:

#!/usr/bin/env python
# get_unique_words.py

import sys

s_in = sys.argv[1]
l_in = s_in.split(',') # Turn string into a list.
set_out = set(l_in) # Turning a list into a set removes duplicates items.
s_out = ','.join(set_out) 
print s_out

In Bash:

get_unique_words(){
  python -c "print ','.join(set('$1'.split(',')))"
}
expanded for clarity
Source Link
wjandrea
  • 722
  • 9
  • 22

##Python

###Option 1

#!/usr/bin/env python
# get_unique_words.py

import sys

l = []
for w in sys.argv[1].split(','):
  if w not in l:
    l += [ w ]
print ','.join(l)

Make executable, then call from Bash:

$ ./get_unique_words.py "aaa,aaa,aaa,bbb,bbb,ccc,bbb,ccc"
aaa,bbb,ccc

Or you could implement it as a Bash function, but the syntax is messy.

get_unique_words(){
  python -c "
l = []
for w in '$1'.split(','):
  if w not in l:
    l += [ w ]
print ','.join(l)"
}

Option 2

This option can become a one-liner if needed:

#!/usr/bin/env python
# get_unique_words.py

import sys

l_ins_in = sys.argv[1]
l_in = s_in.split(',')
l_out = sorted(dict.fromkeys(l_in))
prints_out = ','.join(l_out)
print s_out

In Bash:

get_unique_words(){
  python -c "print ','.join(sorted(dict.fromkeys('$1'.split(','))))"
}

##Python

###Option 1

#!/usr/bin/env python
# get_unique_words.py

import sys

l = []
for w in sys.argv[1].split(','):
  if w not in l:
    l += [ w ]
print ','.join(l)

Make executable, then call from Bash:

$ ./get_unique_words.py "aaa,aaa,aaa,bbb,bbb,ccc,bbb,ccc"
aaa,bbb,ccc

Or you could implement it as a Bash function, but the syntax is messy.

get_unique_words(){
  python -c "
l = []
for w in '$1'.split(','):
  if w not in l:
    l += [ w ]
print ','.join(l)"
}

Option 2

This option can become a one-liner if needed:

#!/usr/bin/env python
# get_unique_words.py

import sys

l_in = sys.argv[1].split(',')
l_out = sorted(dict.fromkeys(l_in))
print ','.join(l_out)

In Bash:

get_unique_words(){
  python -c "print ','.join(sorted(dict.fromkeys('$1'.split(','))))"
}

##Python

###Option 1

#!/usr/bin/env python
# get_unique_words.py

import sys

l = []
for w in sys.argv[1].split(','):
  if w not in l:
    l += [ w ]
print ','.join(l)

Make executable, then call from Bash:

$ ./get_unique_words.py "aaa,aaa,aaa,bbb,bbb,ccc,bbb,ccc"
aaa,bbb,ccc

Or you could implement it as a Bash function, but the syntax is messy.

get_unique_words(){
  python -c "
l = []
for w in '$1'.split(','):
  if w not in l:
    l += [ w ]
print ','.join(l)"
}

Option 2

This option can become a one-liner if needed:

#!/usr/bin/env python
# get_unique_words.py

import sys

s_in = sys.argv[1]
l_in = s_in.split(',')
l_out = sorted(dict.fromkeys(l_in))
s_out = ','.join(l_out)
print s_out

In Bash:

get_unique_words(){
  python -c "print ','.join(sorted(dict.fromkeys('$1'.split(','))))"
}
Updated for changed question (comma delimiter). Added second option. Formatting.
Source Link
wjandrea
  • 722
  • 9
  • 22
Loading
added second method
Source Link
wjandrea
  • 722
  • 9
  • 22
Loading
Source Link
wjandrea
  • 722
  • 9
  • 22
Loading