打印矢量名称的索引(printing indexes of vector to names)

系统教程 行业动态 更新时间:2024-06-14 16:58:30
打印矢量名称的索引(printing indexes of vector to names)

我有这个载体:

labels: [0 5 8 6 1 3 3 2 2 5 5 6 1 1 3 3 1 8 8 3 3 1 1 1 1 5 2 5 1 1 7 3 6 4 3 3 8 1 3 3 5 1 8 8 1 8 7 1 1 8 6]

该向量有51个索引,每个索引代表一个美国州。 我有这个代码及其输出:

for j in range(9): print(" cluster no %i:%s"%(j,[i for i,x in enumerate(labels) if x == j])) output: cluster no 0:[0] cluster no 1:[3, 11, 32] cluster no 2:[2, 17, 24, 36, 43, 49] cluster no 3:[1, 9, 10, 18, 25, 27, 40, 42, 45] cluster no 4:[6, 8, 19, 20, 26, 31, 34, 38, 39] cluster no 5:[21, 30, 46] cluster no 6:[33] cluster no 7:[4, 5, 12, 13, 14, 15, 16, 22, 23, 28, 29, 35, 37, 41, 44, 47, 48] cluster no 8:[7, 50]

基本上,输出是带有索引的值,我想打印而不是那些索引,状态,

举些例子,,

cluster no 0:[AK] .......

反之亦然。

所有51的索引应按以下顺序排列:

'AK'=0 'AL'=1 'AR'=2 'AZ'=3 'CT';... 'DC'; 'DE'; 'FL'; 'GA'; 'HI'; 'IA'; 'ID'; 'IL';... 'IN'; 'KS'; 'KY'; 'LA'; 'MA'; 'MD'= 'ME'; 'MI'; 'MN'; 'MO'; 'MS'; 'MT'; 'NC'; 'ND'; 'NE'; 'NH'; 'NJ'; 'NM'; 'NV'; 'NY'; 'OH'; 'OK'; 'OR'; 'PA'; 'RI'; 'SC'; 'SD'; 'TN'; 'TX'; 'UT'; 'VA'; 'VT'; 'WA'; 'WI'; 'WV'; 'WY'= 50 ...

I have this vector:

labels: [0 5 8 6 1 3 3 2 2 5 5 6 1 1 3 3 1 8 8 3 3 1 1 1 1 5 2 5 1 1 7 3 6 4 3 3 8 1 3 3 5 1 8 8 1 8 7 1 1 8 6]

This vector has 51 indexes, each represent one state of US. And I have this code and its output:

for j in range(9): print(" cluster no %i:%s"%(j,[i for i,x in enumerate(labels) if x == j])) output: cluster no 0:[0] cluster no 1:[3, 11, 32] cluster no 2:[2, 17, 24, 36, 43, 49] cluster no 3:[1, 9, 10, 18, 25, 27, 40, 42, 45] cluster no 4:[6, 8, 19, 20, 26, 31, 34, 38, 39] cluster no 5:[21, 30, 46] cluster no 6:[33] cluster no 7:[4, 5, 12, 13, 14, 15, 16, 22, 23, 28, 29, 35, 37, 41, 44, 47, 48] cluster no 8:[7, 50]

Basically,the output is the values with their indexes, I would like to print instead of those indexes, the states,

for examples,,

cluster no 0:[AK] .......

and visa versa.

The indexes of all 51 should be the following states in order:

'AK'=0 'AL'=1 'AR'=2 'AZ'=3 'CT';... 'DC'; 'DE'; 'FL'; 'GA'; 'HI'; 'IA'; 'ID'; 'IL';... 'IN'; 'KS'; 'KY'; 'LA'; 'MA'; 'MD'= 'ME'; 'MI'; 'MN'; 'MO'; 'MS'; 'MT'; 'NC'; 'ND'; 'NE'; 'NH'; 'NJ'; 'NM'; 'NV'; 'NY'; 'OH'; 'OK'; 'OR'; 'PA'; 'RI'; 'SC'; 'SD'; 'TN'; 'TX'; 'UT'; 'VA'; 'VT'; 'WA'; 'WI'; 'WV'; 'WY'= 50 ...

最满意答案

而不是[i for i,x in... ,使用[states[i] for i,x in... ,假设states是一个类似['AK', 'AL', 'AR',... 你的状态列表只有49个条目,所以我加了两个来获得指定的51。

>>> label = list(map(int, '0 5 8 6 1 3 3 2 2 5 5 6 1 1 3 3 1 8 8 3 3 1 1 1 1 5 2 5 1 1 7 3 6 4 3 3 8 1 3 3 5 1 8 8 1 8 7 1 1 8 6'.split())) >>> states = ['AK','AL','AR','AZ','CT','DC','DE','FL','GA','HI','IA','ID','IL','IN','KS','KY','LA','MA','MD','ME','MI','MN','MO','MS','MT','NC','ND','NE','NH','NJ','NM','NV','NY','OH','OK','OR','PA','RI','SC','SD','TN','TX','UT','VA','VT','WA','WI','WV','WY', 'PR', "ZZ"] >>> for j in range(9): ... print(" cluster no %i:%s"%(j,[states[i] for i,x in enumerate(label) if x == j])) ... cluster no 0:['AK'] cluster no 1:['CT', 'IL', 'IN', 'LA', 'MN', 'MO', 'MS', 'MT', 'NH', 'NJ', 'RI', 'TX', 'VT', 'WV', 'WY'] cluster no 2:['FL', 'GA', 'ND'] cluster no 3:['DC', 'DE', 'KS', 'KY', 'ME', 'MI', 'NV', 'OK', 'OR', 'SC', 'SD'] cluster no 4:['OH'] cluster no 5:['AL', 'HI', 'IA', 'NC', 'NE', 'TN'] cluster no 6:['AZ', 'ID', 'NY', 'AND'] cluster no 7:['NM', 'WI'] cluster no 8:['AR', 'MA', 'MD', 'PA', 'UT', 'VA', 'WA', 'PR']

Instead of [i for i,x in..., use [states[i] for i,x in..., assuming states is a list that goes something like ['AK', 'AL', 'AR',.... Your list of states only had 49 entries, so I added two to get the specified 51.

>>> label = list(map(int, '0 5 8 6 1 3 3 2 2 5 5 6 1 1 3 3 1 8 8 3 3 1 1 1 1 5 2 5 1 1 7 3 6 4 3 3 8 1 3 3 5 1 8 8 1 8 7 1 1 8 6'.split())) >>> states = ['AK','AL','AR','AZ','CT','DC','DE','FL','GA','HI','IA','ID','IL','IN','KS','KY','LA','MA','MD','ME','MI','MN','MO','MS','MT','NC','ND','NE','NH','NJ','NM','NV','NY','OH','OK','OR','PA','RI','SC','SD','TN','TX','UT','VA','VT','WA','WI','WV','WY', 'PR', "ZZ"] >>> for j in range(9): ... print(" cluster no %i:%s"%(j,[states[i] for i,x in enumerate(label) if x == j])) ... cluster no 0:['AK'] cluster no 1:['CT', 'IL', 'IN', 'LA', 'MN', 'MO', 'MS', 'MT', 'NH', 'NJ', 'RI', 'TX', 'VT', 'WV', 'WY'] cluster no 2:['FL', 'GA', 'ND'] cluster no 3:['DC', 'DE', 'KS', 'KY', 'ME', 'MI', 'NV', 'OK', 'OR', 'SC', 'SD'] cluster no 4:['OH'] cluster no 5:['AL', 'HI', 'IA', 'NC', 'NE', 'TN'] cluster no 6:['AZ', 'ID', 'NY', 'AND'] cluster no 7:['NM', 'WI'] cluster no 8:['AR', 'MA', 'MD', 'PA', 'UT', 'VA', 'WA', 'PR']

更多推荐

本文发布于:2023-04-15 03:44:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/dzcp/0ec34bce49917f1e7cc93093bfd2c7f3.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:矢量   索引   名称   printing   vector

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!