我正在使用此代码将一个字典合并到另一个字典中 - 如果存在键,它应该合并两个字典中的值:
source_dict = defaultdict(set)
target_dict = defaultdict(set)
def merge_dict(source_dict, target_dict):
for source_key in source_dict:
if source_key in target_dict:
target_dict[source_key].update(source_dict[source_key])
else:
target_dict[source_key] = source_dict[source_key]
Run Code Online (Sandbox Code Playgroud)
有没有办法优化merge_dict
上面的功能?例如,使其更简洁或更高效?
如何每隔一个数字加 1?
例子:
2323 -> 2424
1112 -> 1213
3912 -> 31013
Run Code Online (Sandbox Code Playgroud)
这就是我现在所拥有的:
def plus_to_every_second(integer):
integer = str(integer)
integer_new = ''
for i in integer:
if integer.find(i) % 2 != 0:
integer_new += str(int(i) + 1)
else:
integer_new += i
return integer_new
Run Code Online (Sandbox Code Playgroud)
由于某种原因,它不起作用。但实际的解决方案是什么?
我有一个路径列表,我希望它们根据它们来自的文件夹名称动态地分成它们应该属于的列表。前两个来自“tent1”文件夹,我希望它们一起放在一个列表中,依此类推。我不想对这些文件夹的名称进行硬编码,然后将路径附加到它们。例如:
paths = [
'/var/lib/cons/states/tent1/tops-ok_2022_11_28',
'/var/lib/cons/states/tent1/tops-ok_2022_11_27',
'/var/lib/cons/states/tent2/tops-ok_2022_11_28',
'/var/lib/cons/states/tent2/tops-ok_2022_11_27',
'/var/lib/cons/states/tent3/tops-ok_2022_11_28',
'/var/lib/cons/states/tent3/tops-ok_2022_11_27',
'/var/lib/cons/states/tent4/tops-ok_2022_11_28',
'/var/lib/cons/states/tent4/tops-ok_2022_11_27',
]
Run Code Online (Sandbox Code Playgroud)
我希望它们是这样的:
[['/var/lib/cons/states/tent1/tops-ok_2022_11_28',
'/var/lib/cons/states/tent1/tops-ok_2022_11_27'],
['/var/lib/cons/states/tent2/tops-ok_2022_11_28',
'/var/lib/cons/states/tent2/tops-ok_2022_11_27'],
['/var/lib/cons/states/tent3/tops-ok_2022_11_28',
'/var/lib/cons/states/tent3/tops-ok_2022_11_27'],
['/var/lib/cons/states/tent4/tops-ok_2022_11_28',
'/var/lib/cons/states/tent4/tops-ok_2022_11_27']]
Run Code Online (Sandbox Code Playgroud) 我想做的是下面的事情。我有一个网址,比如http://www.google.com/one/two/three
我需要提取主域名“www.google.com”,将其提供给 nslookup(因为 nslookup/dig 似乎不适用于完整的 URL),然后将 URL 替换为已解析的 IP 地址。eg
$ echo "http://www.google.com/one/two/three" | sed "s/<pattern>//g"
$ www.google.com
Run Code Online (Sandbox Code Playgroud)
问题是“http://”可能并不总是存在。进而
$ echo "http://www.google.com/one/two/three" | sed "s/<pattern>//g"
$ http://11.22.33.44/one/two/three
Run Code Online (Sandbox Code Playgroud)
任何人都可以提供任何相关链接或相关示例吗?
在 Ruby 中你可能会说:
n = 10_000
Run Code Online (Sandbox Code Playgroud)
或者
n = 10000
Run Code Online (Sandbox Code Playgroud)
它们是相同的。我认为你也可以在 Perl 中做类似的事情。这是一件非常漂亮的事情,可以让大量的数字更容易被人类阅读。
我的问题实际上分为两部分:
有等效的 JavaScript 吗?因为我肯定会用它。
从语法上来说,你如何称呼这个下划线字符?我敢打赌,经验丰富的程序员可以很容易地解决这个问题,但我在尝试使用互联网作为反向字典时遇到了很大的困难。我认为这些信息对我学习未来的语言很有用。这绝对让我发疯,我不知道如何描述它。
我希望我的程序检查我的二维列表是否充满 0
usedGameboardPosition = [[0 for x in range(16)] for x in range(16)]
Run Code Online (Sandbox Code Playgroud)
这是我稍后在程序中填写的声明1。
我试着用这个来检查:
if all(v == 0 for v in usedGameboardPosition):
start = 0
Run Code Online (Sandbox Code Playgroud)
但这似乎不起作用。尽管列表已满 0,但它不会进入条件。
我在列表中有一个列表,并且想要将所有值更改为整数。我已经做到了,但是假设我让用户指定了列表中他们将拥有多少个列表。如何将所有这些值都转换为整数?
我有以下代码,该代码当前接受任何列表或列表中的任何列表,并将所有值转换为整数。我可以看到如何将其扩展到列表中的更多列表,但这可能是不必要的,或者可能还不够。当然,这似乎很慢并且需要很多代码。
for i in l:
index = l.index(i)
l[index] = list(map(int, l[index]))
Run Code Online (Sandbox Code Playgroud)
我的清单是l
,其中的每个清单l
都是i
。我获取我所处位置的索引,并将其中的所有值转换为整数。我如何创建一个函数/循环,以接收列表中有多少个列表,并将它们全部转换为整数?
例如在此输入上:
l = [['6', '5'], '7', ['88', '99', '1']]
Run Code Online (Sandbox Code Playgroud)
上面的代码将返回:
[[6, 5], 7, [88, 99, 1]]
Run Code Online (Sandbox Code Playgroud)
但是,如果我有:
l = [['6', '7'], ['6', ['7', '8']]]
Run Code Online (Sandbox Code Playgroud)
我收到错误:
"TypeError: int() argument must be a string, a bytes-like object or a number, not 'list'"
Run Code Online (Sandbox Code Playgroud)
我知道这是因为我仅转换嵌套在列表中的列表,而不转换嵌套在列表中的列表中的列表,因此它认为我正在尝试将整个列表转换为整数。
长话短说,我如何构建一个可以将任意数量的嵌套列表转换为整数的函数?
我正在使用 gensim (在 jupyter 笔记本中)进行主题建模。我成功创建了一个模型并将其可视化。下面是代码:
import time
start_time = time.time()
import re
import spacy
import nltk
import pyLDAvis
import pyLDAvis.gensim
import gensim
import gensim.corpora as corpora
from gensim.utils import simple_preprocess
from gensim.models import CoherenceModel
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
%matplotlib inline
import logging
logging.basicConfig(format='%(asctime)s : %(levelname)s : %(message)s', level=logging.ERROR)
import warnings
warnings.filterwarnings("ignore",category=DeprecationWarning)
# nlp = spacy.load('en')
stop_word_list = nltk.corpus.stopwords.words('english')
stop_word_list.extend(['from', 'subject', 're', 'edu', 'use'])
df = pd.read_csv('Topic_modeling.csv')
data = df.Articles.values.tolist()
# Remove Emails …
Run Code Online (Sandbox Code Playgroud) 我只想获取父目录的名称。意思是,只有它的最后一个组件,而不是完整路径。
例如,对于a/b/c/d/e
我想要获取的路径d
,而不是a/b/c/d
.
我目前的代码:
import os
path = "C:/example/folder/file1.jpg"
directoryName = os.path.dirname(os.path.normpath(path))
print(directoryName)
Run Code Online (Sandbox Code Playgroud)
这会打印出来C:/example/folder
,我只想得到folder
.
我正在从事 cs50/pset6/dna 项目。我正在努力寻找一种方法来分析字符串序列,并收集特定字符序列连续重复的最大次数。这是一个例子:
细绳:JOKHCNHBVDBVDBVDJHGSBVDBVD
我应该寻找的字符序列:BVD
结果:我的函数应该能够返回3
,因为在某个时刻,字符BVD
连续重复三次,即使它再次重复两次,我也应该查找它重复次数最多的时间。
python ×8
path ×2
python-3.x ×2
defaultdict ×1
dictionary ×1
gensim ×1
int ×1
integer ×1
javascript ×1
linux ×1
list ×1
python-3.7 ×1
sed ×1
string ×1
syntax ×1
typeerror ×1