我有一个非常大的csv文件,我在熊猫中打开如下....
import pandas
df = pandas.read_csv('large_txt_file.txt')
Run Code Online (Sandbox Code Playgroud)
一旦我这样做,我的内存使用量增加2GB,这是预期的,因为该文件包含数百万行.当我需要释放这个内存时,我的问题出现了.我跑了....
del df
Run Code Online (Sandbox Code Playgroud)
但是,我的内存使用率没有下降.这是释放熊猫数据帧所使用的内存的错误方法吗?如果是,那么正确的方法是什么?
我有一个带有一些自定义标签的文件,我想写一个正则表达式来提取标签之间的字符串.例如,如果我的标签是:
[customtag]String I want to extract[/customtag]
Run Code Online (Sandbox Code Playgroud)
如何编写正则表达式以仅提取标记之间的字符串.这段代码似乎是朝着正确方向迈出的一步:
Pattern p = Pattern.compile("[customtag](.+?)[/customtag]");
Matcher m = p.matcher("[customtag]String I want to extract[/customtag]");
Run Code Online (Sandbox Code Playgroud)
不知道下一步该怎么做.有任何想法吗?谢谢.
我无法使用自动换行来处理这个例子......
<html>
<head></head>
<body>
<table style="table-layout:fixed;">
<tr>
<td style="word-wrap: break-word; width:100px;">ThisStringWillNotWrapThisStringWillNotWrapThisStringWillNotWrapThisStringWillNotWrapThisStringWillNotWrapThisStringWillNotWrapThisStringWillNotWrapThisStringWillNotWrapThisStringWillNotWrap</td>
</tr>
</table>
</body></html>
Run Code Online (Sandbox Code Playgroud)
我记得读过必须指定布局(我做过),但除此之外我不知道要做什么才能让它工作.我真的希望这能在Firefox中运行.谢谢.
编辑:Chrome 19和Firefox 12失败,它适用于IE8.我试过doctype strict和transitional,都没有用.
我有一个很长的小数列表,我必须根据某些条件调整因子10,100,1000,..... 1000000.当我乘以它们时,有时候我想摆脱无用的尾随零(虽然并不总是).例如...
from decimal import Decimal
# outputs 25.0, PROBLEM! I would like it to output 25
print Decimal('2.5') * 10
# outputs 2567.8000, PROBLEM! I would like it to output 2567.8
print Decimal('2.5678') * 1000
Run Code Online (Sandbox Code Playgroud)
是否有一个函数告诉小数对象丢弃这些无关紧要的零?我能想到这样做的唯一方法是转换为字符串并使用正则表达式替换它们.
应该提一下我使用的是python 2.6.5
编辑发送者的好答案让我意识到我偶尔会得到一个像250.0这样的数字,当标准化时产生2.5E + 2.我想在这些情况下我可以尝试将它们排序并转换为int
我想得到一个匹配一行的二维Numpy数组的索引.例如,我的数组是这样的:
vals = np.array([[0, 0],
[1, 0],
[2, 0],
[0, 1],
[1, 1],
[2, 1],
[0, 2],
[1, 2],
[2, 2],
[0, 3],
[1, 3],
[2, 3],
[0, 0],
[1, 0],
[2, 0],
[0, 1],
[1, 1],
[2, 1],
[0, 2],
[1, 2],
[2, 2],
[0, 3],
[1, 3],
[2, 3]])
Run Code Online (Sandbox Code Playgroud)
我想得到与行[0,1]匹配的索引,它是索引3和15.当我做的事情就像numpy.where(vals == [0 ,1])我得到的......
(array([ 0, 3, 3, 4, 5, 6, 9, 12, 15, 15, 16, 17, 18, 21]), array([0, 0, 1, 1, 1, 0, 0, 0, …Run Code Online (Sandbox Code Playgroud) 我正在编写一个python CGI脚本来查询MySQL数据库.我正在使用MySQLdb模块.由于数据库将被重复查询,我写了这个函数....
def getDatabaseResult(sqlQuery,connectioninfohere):
# connect to the database
vDatabase = MySQLdb.connect(connectioninfohere)
# create a cursor, execute and SQL statement and get the result as a tuple
cursor = vDatabase.cursor()
try:
cursor.execute(sqlQuery)
except:
cursor.close()
return None
result = cursor.fetchall()
cursor.close()
return result
Run Code Online (Sandbox Code Playgroud)
我的问题是......这是最好的做法吗?我应该在我的函数中重用我的光标吗?例如.哪个更好...
def callsANewCursorAndConnectionEachTime():
result1 = getDatabaseResult(someQuery1)
result2 = getDatabaseResult(someQuery2)
result3 = getDatabaseResult(someQuery3)
result4 = getDatabaseResult(someQuery4)
Run Code Online (Sandbox Code Playgroud)
或者完全取消getDatabaseeResult函数并执行类似的操作.
def reusesTheSameCursor():
vDatabase = MySQLdb.connect(connectionInfohere)
cursor = vDatabase.cursor()
cursor.execute(someQuery1)
result1 = cursor.fetchall()
cursor.execute(someQuery2)
result2 = cursor.fetchall()
cursor.execute(someQuery3)
result3 = cursor.fetchall()
cursor.execute(someQuery4)
result4 …Run Code Online (Sandbox Code Playgroud) 我在/res/values/colors.xml下的Android应用程序中创建了一个colors.xml文件.内容是......
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="Green">#00ff00</color>
</resources>
Run Code Online (Sandbox Code Playgroud)
我尝试使用...更新我的TableRow的背景
TableRow test = (TableRow)findViewById(R.id.tableRow2);
test.setBackgroundColor(R.color.Green);
Run Code Online (Sandbox Code Playgroud)
这不会将其设置为绿色,而是灰色.无论我添加到colors.xml文件的值是什么,它总是相同的灰色.不过这确实有用......
TableRow test = (TableRow)findViewById(R.id.tableRow2);
test.setBackgroundColor(android.graphics.Color.GREEN);
Run Code Online (Sandbox Code Playgroud)
我的colors.xml有问题吗?
我有一大堆数据,我需要将这个数组中一组样本的距离与数组的所有其他元素进行比较.下面是我的数据集的一个非常简单的例子.
import numpy as np
import scipy.spatial.distance as sd
data = np.array(
[[ 0.93825827, 0.26701143],
[ 0.99121108, 0.35582816],
[ 0.90154837, 0.86254049],
[ 0.83149103, 0.42222948],
[ 0.27309625, 0.38925281],
[ 0.06510739, 0.58445673],
[ 0.61469637, 0.05420098],
[ 0.92685408, 0.62715114],
[ 0.22587817, 0.56819403],
[ 0.28400409, 0.21112043]]
)
sample_indexes = [1,2,3]
# I'd rather not make this
other_indexes = list(set(range(len(data))) - set(sample_indexes))
sample_data = data[sample_indexes]
other_data = data[other_indexes]
# compare them
dists = sd.cdist(sample_data, other_data)
Run Code Online (Sandbox Code Playgroud)
有没有办法为不是样本索引的索引索引numpy数组?在上面的例子中,我创建了一个名为other_indexes的列表.我宁愿不必出于各种原因这样做(大数据集,线程,在运行等等的系统上非常少量的内存等).有没有办法做...
other_data = data[ indexes not in sample_indexes]
Run Code Online (Sandbox Code Playgroud)
我读到numpy面具可以做到这一点,但我试过......
other_data …Run Code Online (Sandbox Code Playgroud) 我有一个整数列表......
[1,2,3,4,5,8,9,10,11,200,201,202]
Run Code Online (Sandbox Code Playgroud)
我想将它们分组到一个列表列表中,其中每个子列表包含其序列尚未被破坏的整数.像这样...
[[1,5],[8,11],[200,202]]
Run Code Online (Sandbox Code Playgroud)
我有一个相当笨重的工作......
lSequenceOfNum = [1,2,3,4,5,8,9,10,11,200,201,202]
lGrouped = []
start = 0
for x in range(0,len(lSequenceOfNum)):
if x != len(lSequenceOfNum)-1:
if(lSequenceOfNum[x+1] - lSequenceOfNum[x]) > 1:
lGrouped.append([lSequenceOfNum[start],lSequenceOfNum[x]])
start = x+1
else:
lGrouped.append([lSequenceOfNum[start],lSequenceOfNum[x]])
print lGrouped
Run Code Online (Sandbox Code Playgroud)
这是我能做的最好的事情.是否有更"pythonic"的方式来做到这一点?谢谢..
我有一个唯一行列表和另一个更大的数据数组(在示例中称为test_rows).我想知道是否有更快的方法来获取数据中每个唯一行的位置.我能想到的最快的方法是......
import numpy
uniq_rows = numpy.array([[0, 1, 0],
[1, 1, 0],
[1, 1, 1],
[0, 1, 1]])
test_rows = numpy.array([[0, 1, 1],
[0, 1, 0],
[0, 0, 0],
[1, 1, 0],
[0, 1, 0],
[0, 1, 1],
[0, 1, 1],
[1, 1, 1],
[1, 1, 0],
[1, 1, 1],
[0, 1, 0],
[0, 0, 0],
[1, 1, 0]])
# this gives me the indexes of each group of unique rows
for row in uniq_rows.tolist():
print row, numpy.where((test_rows == row).all(axis=1))[0]
Run Code Online (Sandbox Code Playgroud)
这打印...... …