我有一个包含字符串列表的输入文件.
我从第二行开始迭代每四行.
从这些行中的每一行开始,我从第一个和最后6个字符创建一个新字符串,并且仅当新字符串是唯一的时才将其放在输出文件中.
我写的代码可以实现这一点,但是我正在使用非常大的深度排序文件,并且已经运行了一天并且没有取得多大进展.所以我正在寻找任何建议,如果可能的话,这样做会更快.谢谢.
def method():
target = open(output_file, 'w')
with open(input_file, 'r') as f:
lineCharsList = []
for line in f:
#Make string from first and last 6 characters of a line
lineChars = line[0:6]+line[145:151]
if not (lineChars in lineCharsList):
lineCharsList.append(lineChars)
target.write(lineChars + '\n') #If string is unique, write to output file
for skip in range(3): #Used to step through four lines at a time
try:
check = line #Check for additional lines in file
next(f)
except StopIteration:
break …
Run Code Online (Sandbox Code Playgroud) 我正在使用 matplotlib 和 seaborn 生成一些图,x 轴和 y 轴的长度不同,如下面的示例所示。
有没有办法确保每个轴的比例相同?
我的示例代码没什么特别的,但这是一个示例:
# plot data
plt.scatter(df['x'], df['y'], color = 'gray', s=5) # s controls point size
plt.xlim(-0.0002,0.002)
plt.ylim(-0.0002,0.002)
# add y=x line
plt.plot([0,1],[0,1], lw=2, color='#414242', linestyle='dashed')
sns.set_context("paper", font_scale=1.5)
plt.xlabel(xlabel, {'size':'20'})
plt.ylabel(ylabel, {'size':'20'})
plt.title(title)
sns.set_style('ticks')
sns.despine(offset=10, trim=True)
plt.xticks(rotation=45)
plt.tight_layout()
plt.show()
Run Code Online (Sandbox Code Playgroud)
使用plt.axis('equal')
:
有没有一种好方法可以使用 Levenstein 距离将一个特定字符串与第二个较长字符串中的任何区域进行匹配?
例子:
str1='aaaaa'
str2='bbbbbbaabaabbbb'
if str1 in str2 with a distance < 2:
return True
Run Code Online (Sandbox Code Playgroud)
因此,在上面的示例中,字符串 2 的一部分是aabaa
,distance(str1,str2) < 2
因此语句应该返回True
。
我能想到的唯一方法是一次从 str2 中取出 5 个字符,与 str1 进行比较,然后在 str2 中重复此操作。不幸的是,这看起来效率很低,我需要以这种方式处理大量数据。
当使用类似的东西时,sns.barplot(myrange, means, pallette='deep')
我会得到一些颜色顺序。我看到的默认顺序是蓝色、绿色、红色、紫色、棕色、蓝绿色。如果我有 6 个条形,一切都很好,但是如果我想要 12 个条形,并且我希望前两个条形为蓝色,接下来的两个为绿色,接下来的两个为红色,等等,这样组的颜色相似呢? ?
我可以只传递一个列表,color=myColors
但这似乎不接受 html 颜色,#4C72B0
所以我不知道如何获得我正在寻找的确切色调。
最初,我一直在打开并同时阅读两个文件,如下所示:
with open(file1, 'r') as R1:
with open(file2, 'r') as R2:
### my code
Run Code Online (Sandbox Code Playgroud)
但现在输入文件名有时可能被gzip压缩.所以,我想拆分with
语句并使用if
语句来处理这两个场景,如下所示:
if zipped:
R1 = gzip.open(file1, 'r')
R2 = gzip.open(file2, 'r')
else:
R1 = open(file1, 'r')
R2 = open(file2, 'r')
with R1:
with R2:
### my code
Run Code Online (Sandbox Code Playgroud)
第二个代码的功能是否与第一个类似?或者甚至有更好的方法来做到这一点?
我正在使用多重处理,并为每个进程生成一个 pandas DataFrame。我想将它们合并在一起并输出数据。以下策略似乎几乎可行,但是当尝试用它读入数据时,df.read_csv()
仅使用第一个作为name
列标题。
from multiprocessing import Process, Lock
def foo(name, lock):
d = {f'{name}': [1, 2]}
df = pd.DataFrame(data=d)
lock.acquire()
try:
df.to_csv('output.txt', mode='a')
finally:
lock.release()
if __name__ == '__main__':
lock = Lock()
for name in ['bob','steve']
p = Process(target=foo, args=(name, lock))
p.start()
p.join()
Run Code Online (Sandbox Code Playgroud) 我试图在两个文件之间连接片段特定的行.这样我想从file2中的第2行添加一些东西到file1的第2行.然后从第6行从file2到文件1的第6行,依此类推.有没有办法同时迭代这两个文件来做到这一点?(知道输入文件大约为15GB可能会有所帮助).
这是一个简化的例子:
档案1:
Ignore
This is a
Ignore
Ignore
Ignore
This is also a
Ignore
Ignore
Run Code Online (Sandbox Code Playgroud)
文件2:
Ignore
sentence
Ignore
Ignore
Ignore
sentence
Ignore
Ignore
Run Code Online (Sandbox Code Playgroud)
输出文件:
Ignore
This is a sentence
Ignore
Ignore
Ignore
This is also a sentence
Ignore
Ignore
Run Code Online (Sandbox Code Playgroud) 我将一些 python 函数导入 Jupyter Lab 笔记本,然后在笔记本中使用它们。但我会在对函数进行更改和在 Jupyter Lab 笔记本中重新运行它们之间来回切换。我发现让 Jupyter Lab 使用更新后的代码的唯一方法是重新启动内核,然后重新运行所有内容。虽然这工作正常,但有点麻烦,因为我需要再次运行笔记本中的所有内容。
有没有更好的方法可以让 Jupyter Lab 看到导入函数中的新变化,同时仍然保留所有先前设置的变量?
我正在使用 jupyter 实验室笔记本并尝试修改代码,在 jupyter 笔记本中重新加载它并使用修改后的代码而不重新加载内核。我正在使用 python 3.5.5 并且正在运行这样的代码:
(在文件 test.py 中)
def myTest():
print('hello')
Run Code Online (Sandbox Code Playgroud)
(在 jupyter 中)
from test import myTest
import importlib
importlib.reload(test)
myTest()
Run Code Online (Sandbox Code Playgroud)
当我在我的 jupyter 实验室笔记本中运行代码时,我收到一个 NameError name 'test' is not defined
。通过在 stackoverflow 上搜索,我发现此错误的唯一参考是使用旧版本的 python 的问题。但我使用的方式importlib.reload()
似乎是正确的。
我想转换一个看起来像这样的表:
Blue Green Red
Thing 1 No Yes No
Thing 2 Yes No No
Thing 3 Yes No No
Run Code Online (Sandbox Code Playgroud)
进入这种风格:
Color
Thing 1 Green
Thing 2 Blue
Thing 3 Blue
Run Code Online (Sandbox Code Playgroud)
在 python 或 Pandas 中有没有一个很好的方法来做到这一点?这些表格样式有名称吗?