社区所有版块导航
Python
python开源   Django   Python   DjangoApp   pycharm  
DATA
docker   Elasticsearch  
aigc
aigc   chatgpt  
WEB开发
linux   MongoDB   Redis   DATABASE   NGINX   其他Web框架   web工具   zookeeper   tornado   NoSql   Bootstrap   js   peewee   Git   bottle   IE   MQ   Jquery  
机器学习
机器学习算法  
Python88.com
反馈   公告   社区推广  
产品
短视频  
印度
印度  
Py学习  »  Sunitha  »  全部回复
回复总数  3
7 年前
回复了 Sunitha 创建的主题 » 计算python中两个单词的所有组合的频率[duplicate]

你可以使用 zip 得到每两个单词的组,然后使用 Counter 得到频率

>>> from collections import Counter
>>> text = "I have 2 laptops, I have 2 chargers"
>>> words = text.split()

>>> d = {' '.join(words):n for words,n in Counter(zip(words, words[1:])).items() if not  words[0][-1]==(',')}
>>> print (d)
{'I have': 2, 'have 2': 2, '2 laptops,': 1, '2 chargers': 1}

>>> import json
>>> print (json.dumps(d, indent=4))
{
    "I have": 2,
    "have 2": 2,
    "2 I": 1,
    "2 chargers": 1
}
7 年前
回复了 Sunitha 创建的主题 » python:如何使用python打印ssh密钥

如果要将输出捕获到变量,请使用 subprocess . 如果不是,你可以使用 os.system 如用户803422所述

import os, subprocess
path = '~/.ssh/authorized_keys'
cmd = 'cat ' + os.path.expanduser(path)
output = subprocess.Popen(cmd.split(), stdout=subprocess.PIPE)
response = output.communicate()
print (response)
8 年前
回复了 Sunitha 创建的主题 » python regex以匹配可选的双引号字符串

您的regexp模式几乎是正确的。您只需确保在您的模式前后没有引号。所以使用这个模式 r'(?<!")("?)Assets\1(?!")

>>> words = ['"Assets"', 'Assets', '"Assets', 'Assets"']
>>> ptrn = re.compile(r'(?<!")("?)Assets\1(?!")')
>>> [bool(ptrn.match(word)) for word in words]
[True, True, False, False]