Skip to content

Instantly share code, notes, and snippets.

@acrymble
Created July 6, 2011 09:24
Show Gist options
  • Select an option

  • Save acrymble/1066901 to your computer and use it in GitHub Desktop.

Select an option

Save acrymble/1066901 to your computer and use it in GitHub Desktop.
Python slice
# calculate the length of the n-gram
kwic = 'amongst them a black there was one'.split()
n = len(kwic)
print n
-> 7
# calculate the index position of the keyword
keyindex = n // 2
print keyindex
-> 3
# display the items before the keyword
print kwic[:keyindex]
-> ['amongst', 'them', 'a']
# display the keyword only
print kwic[keyindex]
-> black
# display the items after the keyword
print kwic[(keyindex+1):]
-> ['there', 'was', 'one']
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment