小编alp*_*o20的帖子

zip(*) 是如何生成 n-gram 的?

我正在回顾一些关于 n 元语法的笔记,并且遇到了一些有趣的函数。首先是生成二元组:

def bigrams(word):
    return sorted(list(set(''.join(bigram)
                           for bigram in zip(word,word[1:]))))

def bigram_print(word):
    print("The bigrams of", word, "are:")
    print(bigrams(word))

bigram_print("ababa")
bigram_print("babab")
Run Code Online (Sandbox Code Playgroud)

在自己阅读并使用 Python 后,我明白了为什么这是有效的。然而,当我看到这个函数时,我对这里的使用感到非常困惑zip(*word[i:])。我知道这*是一个解包运算符(如此处所解释的,但我真的被它如何与列表理解结合使用而绊倒了。谁能解释一下吗?

def ngrams(word, n):
    return sorted(list(set(''.join(ngram)
                           for ngram in zip(*[word[i:]
                                              for i in range(n)]))))

def ngram_print(word, n):
    print("The {}-grams of {} are:".format(n, word))
    print(ngrams(word, n))

for n in [2, 3, 4]:
    ngram_print("ababa", n)
    ngram_print("babab", n)
    print()
Run Code Online (Sandbox Code Playgroud)

python zip n-gram

2
推荐指数
2
解决办法
1139
查看次数

为什么我在 Git 上有这么多未跟踪的文件?

我在 YouTube 上观看了一个试图学习 Git 的教程,当我输入“git status”时,我得到了一堆我什至不知道自己拥有的未跟踪文件。它看起来像这样:(

use "git add/rm <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

    deleted:    HW5-master/.DS_Store
    deleted:    HW5-master/README.md
    deleted:    HW5-master/github_fork.png
    deleted:    HW5-master/solution/vltrees.py
    deleted:    HW5-master/variably_leafed_trees/instructions.mdown
    deleted:    HW5-master/variably_leafed_trees/vltrees.py

Untracked files:
  (use "git add <file>..." to include in what will be committed)

    .CFUserTextEncoding
    .DS_Store
    .Rapp.history
    .Rhistory
    .anaconda/
    .bash_history
    .bash_profile
    .bash_profile-anaconda3.bak
    .bash_profile.pysave
    .bash_sessions/
    .conda/
    .condarc
    .config/
    .cups/
    .ghc/
    .gitconfig
    .idlerc/
    .ipynb_checkpoints/
    .ipython/
    .jupyter/
    .matplotlib/
    .oracle_jre_usage/
    .python_history
    .rstudio-desktop/
    .spyder-py3/
    .subversion/
    .viminfo
    .wing101-7 …
Run Code Online (Sandbox Code Playgroud)

git terminal command-line

-1
推荐指数
1
解决办法
691
查看次数

标签 统计

command-line ×1

git ×1

n-gram ×1

python ×1

terminal ×1

zip ×1