- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
您好,我目前正在研究机器学习,这对我来说有些复杂。这是一个字典,其中包含'data'和'name'数组,子数组的数量相同,这意味着它们通过相同级别和相同位置的数组相互链接。 for example , data [-12.746249259898983, -40.618078412505398, -53.765980530841738, 5.2261728305123398, 0.12162578441442624, -5.7948531230422988, -449155.5052490317, 0.91963697398417421]'s total name is 100, and data -12.746249259898983 which inside that array 's name is '100.0 '.
但是,有些名称可以表示单级数组或嵌套数组。
{'data': [[ [-12.746249259898983, -40.618078412505398, -53.765980530841738, 5.2261728305123398, 0.12162578441442624, -5.7948531230422988, -449155.5052490317, 0.91963697398417421], [-98.037517854387147, -221.057540054155, -159.4518899022695, 2.8761226353857214, -5.3555144738112865, -8.4617336886131636, -1078807.4268864163, -13.13441127151575], [20.903581634463947, 25.329222910065528, 5.412150882014295, 9.8334972052776468, 5.0344964646349721, -4.9321628192214595, 465839.20729012048, 13.101100255062818], [6906900.092440652, 41574768.75710336, 10927415.550909607, 11310.66577299293, 8798.704300502737, 8799.682832152064, 183845205120901.88, 112667.8001209582], [23668.070022021395, 50044.215525581945, 31776.017910004855, 1187.8286828919734, 1004.3222306303834, 1097.6394464618995, 144191707.36233276, 3204.3354707366584]], [[-12.746249259898983, -40.618078412505398, -53.765980530841738, 5.2261728305123398, 0.12162578441442624, -5.7948531230422988, -449155.5052490317, 0.91963697398417421], [-98.037517854387147, -221.057540054155, -159.4518899022695, 2.8761226353857214, -5.3555144738112865, -8.4617336886131636, -1078807.4268864163, -13.13441127151575], [20.903581634463947, 25.329222910065528, 5.412150882014295, 9.8334972052776468, 5.0344964646349721, -4.9321628192214595, 465839.20729012048, 13.101100255062818], [6906900.092440652, 41574768.75710336, 10927415.550909607, 11310.66577299293, 8798.704300502737, 8799.682832152064, 183845205120901.88, 112667.8001209582], [23668.070022021395, 50044.215525581945, 31776.017910004855, 1187.8286828919734, 1004.3222306303834, 1097.6394464618995, 144191707.36233276, 3204.3354707366584]], [[105488.30345760827], [3289.7903599842557], [1084894.676814588], [14144.972809638024], [-0.28007907692942713, 0.22442925752465423, 0.53764244860950272], [-0.17191558990475442, -0.79049974125170352, 0.50351854155808451], [0.82458612961411526, 0.76972732632638607, 0.85167351947413183], [1.0, 1.0, 1.0]], [[523.30228588437444, 45.819830845333598, 28.816263055785519, -13.399823220658771, -4.3270838526008539, 6.3733684200628371, 8595795.3597348519, 8.384858849273007], [475.31250457055427, -228.32291772427124, -176.40773800372068, -18.093080976490384, -10.369967809128632, 4.2210565912106244, 7098802.0557304611, -7.1771997695309269], [588.25628456928746, 294.97718389914189, 262.91102656456047, -10.427939493412241, 1.4416568032097778, 8.5693307123828362, 10382020.11504475, 25.39147629554094], [48743197.97266989, 30241091.100284778, 18077525.93554477, 34815.577041886056, 13488.38255077284, 9285.35861896049, 1.33034499502354e+16, 137928.77372005512], [88438.08631445921, 56149.17419161105, 43172.45645293931, 2264.570124291333, 1314.3600405678308, 1119.5488398712207, 1452689415.79519, 3947.582767320521]], [[523.30228588437444, 45.819830845333598, 28.816263055785519, -13.399823220658771, -4.3270838526008539, 6.3733684200628371, 8595795.3597348519, 8.384858849273007], [475.31250457055427, -228.32291772427124, -176.40773800372068, -18.093080976490384, -10.369967809128632, 4.2210565912106244, 7098802.0557304611, -7.1771997695309269], [588.25628456928746, 294.97718389914189, 262.91102656456047, -10.427939493412241, 1.4416568032097778, 8.5693307123828362, 10382020.11504475, 25.39147629554094], [48743197.97266989, 30241091.100284778, 18077525.93554477, 34815.577041886056, 13488.38255077284, 9285.35861896049, 1.33034499502354e+16, 137928.77372005512], [88438.08631445921, 56149.17419161105, 43172.45645293931, 2264.570124291333, 1314.3600405678308, 1119.5488398712207, 1452689415.79519, 3947.582767320521]], [[187759.71695900976], [4698.479004730381], [1027854.7076158928], [23680.601277936279], [-0.15439507445075032, -0.075920544072209092, 0.66503179584326411], [0.46923791478750121, -0.69581655384848085, -0.72748336248359702], [0.21092970277607359, 0.14987815855097059, 0.81570800991816339], [1.0, 1.0, 1.0]], [[13.396472405737706, -18.359344127602043, 36.826432688017405, -5.4414333610698273, -10.694878175101108, 7.9448139442889429, 10816437.307411144, 0.19533722684887966], [-15.189934136155252, -200.13563785186551, -109.36104765476506, -11.7199280963678, -17.858474582674631, 6.8660269597012578, 1044415.9081568928, -1.3455015074983672], [72.972793023164726, 181.79005098185144, 199.11746464797852, -4.9326473220315465, -5.0927085243861612, 14.603766227834551, 19440694.34613679, 1.7339256547371409], [1384309.4521168934, 8347935.341004645, 7917333.307240587, 29378.49283018787, 32011.489849631027, 36975.920226450995, 3.3465760911327116e+16, 911.4959262418305], [11296.673361701862, 32414.28539427253, 29892.827418169887, 1857.859505569651, 2026.9222424051377, 2193.920509546758, 1961706423.8618736, 309.5250420877879]], [[13.396472405737706, -18.359344127602043, 36.826432688017405, -5.4414333610698273, -10.694878175101108, 7.9448139442889429, 10816437.307411144, 0.19533722684887966], [-15.189934136155252, -200.13563785186551, -109.36104765476506, -11.7199280963678, -17.858474582674631, 6.8660269597012578, 1044415.9081568928, -1.3455015074983672], [72.972793023164726, 181.79005098185144, 199.11746464797852, -4.9326473220315465, -5.0927085243861612, 14.603766227834551, 19440694.34613679, 1.7339256547371409], [1384309.4521168934, 8347935.341004645, 7917333.307240587, 29378.49283018787, 32011.489849631027, 36975.920226450995, 3.3465760911327116e+16, 911.4959262418305], [11296.673361701862, 32414.28539427253, 29892.827418169887, 1857.859505569651, 2026.9222424051377, 2193.920509546758, 1961706423.8618736, 309.5250420877879]], [[73603.78617414428], [6078.7022575215515], [625733.61247557076], [45672.991571875093], [0.043448760802826739, 0.23278517949941815, -0.4412484230587122], [0.73571039835259899, -0.97150579562327632, -0.85102560336644162], [0.43273760788012128, 0.59834831245116515, 0.76048753082083709], [1.0, 1.0, 1.0]], [[-4312.4566991228266, 111.37588860973005, -369.27441630396561, 1.0221741887428542, 10.264001072191407, -0.64794873494824146, -26535.315429097467, 3733.3894352435532], [-4391.6993203248185, 2.1930377207860374, -556.18155858830767, -2.8465168776531042, 6.8275893393989735, -2.992456229023948, -134301.2497497506, 3085.6901898537008], [-4265.6954381966243, 490.57527638444799, -106.53884094262287, 2.6903234802233951, 14.899879413776139, 1.2542987455939745, 124282.28583445404, 4312.3584935051322], [3154842424.3400664, 66787778.51899314, 37678234.97132428, 1886.122427403741, 27432.6986991385, 1552.4163983149103, 9721961586366.314, 2438338620.274526], [728805.1821517571, 88155.59598567848, 68098.54374026474, 500.1103528606394, 1980.9257474491533, 442.23338752473614, 30550976.541662402, 630942.8145561604]], [[-4312.4566991228266, 111.37588860973005, -369.27441630396561, 1.0221741887428542, 10.264001072191407, -0.64794873494824146, -26535.315429097467, 3733.3894352435532], [-4391.6993203248185, 2.1930377207860374, -556.18155858830767, -2.8465168776531042, 6.8275893393989735, -2.992456229023948, -134301.2497497506, 3085.6901898537008], [-4265.6954381966243, 490.57527638444799, -106.53884094262287, 2.6903234802233951, 14.899879413776139, 1.2542987455939745, 124282.28583445404, 4312.3584935051322], [3154842424.3400664, 66787778.51899314, 37678234.97132428, 1886.122427403741, 27432.6986991385, 1552.4163983149103, 9721961586366.314, 2438338620.274526], [728805.1821517571, 88155.59598567848, 68098.54374026474, 500.1103528606394, 1980.9257474491533, 442.23338752473614, 30550976.541662402, 630942.8145561604]], [[885059.3218777011], [2923.2694878345296], [2236604.1192022543], [8808.54792655048], [-0.67448622916323409, -0.50158101433414648, 0.59882229555795874], [-0.78104865531713752, -0.76429278912704701, 0.63516453144318363], [0.20462205303333128, 0.84578383059323625, 0.59233056719450861], [1.0, 1.0, 1.0]], [[-259.72665899990471, -83.479321394167755, -121.15460261636761, -9.5566399167895906, -10.751374026150252, 6.9216241967193373, 14885579.317280933, -21.445028515600601], [-443.23038559240399, -180.2214836399863, -177.88859043152414, -17.157662401019614, -16.084610849457864, 2.4976391930266151, 10238968.298370993, -44.428320700622876], [-303.57098842656001, 48.893029546726382, -69.225922326687694, -3.4513266189454357, -7.9243380822281777, 11.288531677853044, 19912765.253164865, 2.6916854500962191], [41735450.1518125, 24390799.584619, 9017310.78529916, 28630.03604412623, 27367.43896473854, 17107.884579392492, 4.242364584898001e+16, 253955.56763607], [75924.07800808054, 43290.42097424058, 29303.03439665969, 1778.5410136020398, 1988.7244606604343, 1372.5600617945775, 2515662904.620479, 4776.4285037481795]], [[-259.72665899990471, -83.479321394167755, -121.15460261636761, -9.5566399167895906, -10.751374026150252, 6.9216241967193373, 14885579.317280933, -21.445028515600601], [-443.23038559240399, -180.2214836399863, -177.88859043152414, -17.157662401019614, -16.084610849457864, 2.4976391930266151, 10238968.298370993, -44.428320700622876], [-303.57098842656001, 48.893029546726382, -69.225922326687694, -3.4513266189454357, -7.9243380822281777, 11.288531677853044, 19912765.253164865, 2.6916854500962191], [41735450.1518125, 24390799.584619, 9017310.78529916, 28630.03604412623, 27367.43896473854, 17107.884579392492, 4.242364584898001e+16, 253955.56763607], [75924.07800808054, 43290.42097424058, 29303.03439665969, 1778.5410136020398, 1988.7244606604343, 1372.5600617945775, 2515662904.620479, 4776.4285037481795]], [[148517.53337898097], [5139.82553605705], [1242960.9144121602], [27678.070645658128], [-0.072211399963483205, -0.01330100771527422, 0.50872724028242133], [0.12899184324668519, -0.83705081189441499, -0.30244329509440016], [0.8127044117719453, 0.76986342841470423, 0.67385354549078191], [1.0, 1.0, 1.0]]],
'name': [[100, 101, 102, 103, 104, 105], [106, 107, 108], [109, 110, 111, 112, 113, 114, 115, 116], [200, 201, 202, 203, 204, 205], [206, 207, 208], [209, 210, 211, 212, 213, 214, 215, 216], [300, 301, 302, 303, 304, 305], [306, 307, 308], [309, 310, 311, 312, 313, 314, 315, 316], [400, 401, 402, 403, 404, 405], [406, 407, 408], [409, 410, 411, 412, 413, 414, 415, 416], [500, 501, 502, 503, 504, 505], [506, 507, 508], [509, 510, 511, 512, 513, 514, 515, 516]]}
现在我想将数据展平并将它们全部放在底层,但是,一旦我这样做了,名称就会不匹配。
更普遍的(提醒它自己的数据可能会得到更多的嵌套级别,但地址不会),如果我得到:{'数据':[[[1],[2,3,[4]]],[[5,6],[7,8,9]]],'名称':[[100,101] ,[202,203]]}
其中 100 是 [1] 的名称,101 是 [2,3,[4]] 的名称,202 是 [5,6] 的名称,203 是[7,8,9] 的名称
更清楚:
`{'data':[[ [1],[2,3,[4]] ],[ [5,6],[7,8,9]]]
| | | |
'name': [[ 100, 101 ],[ 202, 203 ]]
,一旦我将数据展平并变为:[1,2,3,4,5,6,7,8,9]
相应的名称数组仍然是 [100,101,202,203],这意味着不匹配,我希望它们匹配,所以当我展平嵌套数组时,我可以获得内部元素的相同名称,例如 get [1,2,3,4,5,6,7,8,9]
和 [100.0,101.0,101.1,101.2,101.2,202.0,202.1,203.0,203.1,203.2]
因为 [2,3,[4]]
都是named 101
,100.0 表示数组中的第一个元素,2,101.2 表示数组中的第三个元素,(不关心进一步嵌套表示 [4] 仍然是 101.2 但不是 101.20,并且 [ [4]] 也是 101.2 但不是 101.200,当然,如果你是魔术师,我希望我能有这种很酷的名字风格,但我认为它对于一个问题来说已经足够复杂了),因此,名字数组也会正确压平。
这是一种头脑 Storm ..感谢您的帮助..
### 我已经重写了这个问题,最后一天在@schlezzz15 的帮助下我在我的原始数据中发现了一些错误,非常感谢,可惜数据太大而且很难找到里面的错误,真的很抱歉最佳答案
有趣的问题。
zip 的解决方案, itertools.chain , itertools.cycle和 compiler.ast.flatten
from compiler.ast import flatten
from itertools import chain,cycle
d = {'data':[[ [1],[2,3,[4]] ],[ [5,6],[7,8,9]]],
'name': [[ 100, 101 ],[ 202, 203 ]]}
data,name = chain(*d["data"]),list(chain(*d["name"]))
data = [flatten(sublist)for sublist in data ]
print name
# >>> [100, 101, 202, 203]
print data
# >>> [[1], [2, 3, 4], [5, 6], [7, 8, 9]]
aligned = zip(name,data)
print aligned
# >>> [(100, [1]), (101, [2, 3, 4]), (202, [5, 6]), (203, [7, 8, 9])]
result = [zip(cycle([n]),da) for n,da in aligned]
print result
# >>> [[(100, 1)], [(101, 2), (101, 3), (101, 4)], [(202, 5), (202, 6)], [(203, 7), (203, 8), (203, 9)]]
flattened_result = list(chain(*result))
print flattened_result
# >>> [(100, 1), (101, 2), (101, 3), (101, 4), (202, 5), (202, 6), (203, 7), (203, 8), (203, 9)]
string_result = [str(item[0])+"."+str(item[1]-1) for item in flattened_result]
print string_result
# >>> ['100.0', '101.1', '101.2', '101.3', '202.4', '202.5', '203.6', '203.7', '203.8']
# I do not recommend the below version, only on OP's request.
d = {'data':[[ [1],[2,3,[4]] ],[ [5,6],[7,8,9]]],
'name': [[ 100, 101 ],[ 202, 203 ]]}
all_in_one =[str(item[0])+"."+str(item[1]-1) for item in chain(*[zip(cycle([n]),da) for n,da in zip((chain(*d["name"])),(flatten(sublist)for sublist in chain(*d["data"])))])]
print all_in_one == string_result
# >>> True
我不确定您想要哪种输出格式,因此我包含了所有中间结果。你应该很容易得到你想要的。如果您指定所需的输出,我将修改我的解决方案。
关于python - 展平嵌套数组,但也在 python 中附加来自另一个数组的信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30447603/
我正在处理一组标记为 160 个组的 173k 点。我想通过合并最接近的(到 9 或 10 个组)来减少组/集群的数量。我搜索过 sklearn 或类似的库,但没有成功。 我猜它只是通过 knn 聚类
我有一个扁平数字列表,这些数字逻辑上以 3 为一组,其中每个三元组是 (number, __ignored, flag[0 or 1]),例如: [7,56,1, 8,0,0, 2,0,0, 6,1,
我正在使用 pipenv 来管理我的包。我想编写一个 python 脚本来调用另一个使用不同虚拟环境(VE)的 python 脚本。 如何运行使用 VE1 的 python 脚本 1 并调用另一个 p
假设我有一个文件 script.py 位于 path = "foo/bar/script.py"。我正在寻找一种在 Python 中通过函数 execute_script() 从我的主要 Python
这听起来像是谜语或笑话,但实际上我还没有找到这个问题的答案。 问题到底是什么? 我想运行 2 个脚本。在第一个脚本中,我调用另一个脚本,但我希望它们继续并行,而不是在两个单独的线程中。主要是我不希望第
我有一个带有 python 2.5.5 的软件。我想发送一个命令,该命令将在 python 2.7.5 中启动一个脚本,然后继续执行该脚本。 我试过用 #!python2.7.5 和http://re
我在 python 命令行(使用 python 2.7)中,并尝试运行 Python 脚本。我的操作系统是 Windows 7。我已将我的目录设置为包含我所有脚本的文件夹,使用: os.chdir("
剧透:部分解决(见最后)。 以下是使用 Python 嵌入的代码示例: #include int main(int argc, char** argv) { Py_SetPythonHome
假设我有以下列表,对应于及时的股票价格: prices = [1, 3, 7, 10, 9, 8, 5, 3, 6, 8, 12, 9, 6, 10, 13, 8, 4, 11] 我想确定以下总体上最
所以我试图在选择某个单选按钮时更改此框架的背景。 我的框架位于一个类中,并且单选按钮的功能位于该类之外。 (这样我就可以在所有其他框架上调用它们。) 问题是每当我选择单选按钮时都会出现以下错误: co
我正在尝试将字符串与 python 中的正则表达式进行比较,如下所示, #!/usr/bin/env python3 import re str1 = "Expecting property name
考虑以下原型(prototype) Boost.Python 模块,该模块从单独的 C++ 头文件中引入类“D”。 /* file: a/b.cpp */ BOOST_PYTHON_MODULE(c)
如何编写一个程序来“识别函数调用的行号?” python 检查模块提供了定位行号的选项,但是, def di(): return inspect.currentframe().f_back.f_l
我已经使用 macports 安装了 Python 2.7,并且由于我的 $PATH 变量,这就是我输入 $ python 时得到的变量。然而,virtualenv 默认使用 Python 2.6,除
我只想问如何加快 python 上的 re.search 速度。 我有一个很长的字符串行,长度为 176861(即带有一些符号的字母数字字符),我使用此函数测试了该行以进行研究: def getExe
list1= [u'%app%%General%%Council%', u'%people%', u'%people%%Regional%%Council%%Mandate%', u'%ppp%%Ge
这个问题在这里已经有了答案: Is it Pythonic to use list comprehensions for just side effects? (7 个答案) 关闭 4 个月前。 告
我想用 Python 将两个列表组合成一个列表,方法如下: a = [1,1,1,2,2,2,3,3,3,3] b= ["Sun", "is", "bright", "June","and" ,"Ju
我正在运行带有最新 Boost 发行版 (1.55.0) 的 Mac OS X 10.8.4 (Darwin 12.4.0)。我正在按照说明 here构建包含在我的发行版中的教程 Boost-Pyth
学习 Python,我正在尝试制作一个没有任何第 3 方库的网络抓取工具,这样过程对我来说并没有简化,而且我知道我在做什么。我浏览了一些在线资源,但所有这些都让我对某些事情感到困惑。 html 看起来
我是一名优秀的程序员,十分优秀!