加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

Python:如何在行和列的值之间创建对应矩阵?

发布时间:2020-12-20 13:49:19 所属栏目:Python 来源:网络整理
导读:我需要在excel中编写,我需要根据组和名称之间的对应关系来知道在哪里写’x’,所以我需要一个结构来保存组名和corespondent列和行 n1 – 在第1行 n2 – 在第2行 A(组名) – 在第2列 B(组名) – 在第3栏 c-在第4栏 D – 在第5栏 我必须在矩阵中设置用户和组之
我需要在excel中编写,我需要根据组和名称之间的对应关系来知道在哪里写’x’,所以我需要一个结构来保存组名和corespondent列和行

n1 – 在第1行
n2 – 在第2行

A(组名) – 在第2列
B(组名) – 在第3栏
c-在第4栏
D – 在第5栏

我必须在矩阵中设置用户和组之间的对应关系,并写入excel.工作数据:

Groups = A,B,C,D,E,F ...
Name  =  N1,N2,N3,N4 .....

N1 => A,F
N2= B,D
N3= A,F

预期的结果是:

A  B  C   D  E  F
N1  x     x   x     x

N2     x  x   x

N3  x            x  x


N1,N3 - are on rows

A,c,D  - are on columns

我需要在excel中编写,所以我需要一个结构来保存组名和corespondent列和行

N1 - is on row 1
N2 - is on row 2

A(group name) - is on column 2
B(group name)  - is on column 3
C is on column 4
D  is on column 5

例:

for N1 I need to write on (row1,col2),(row1,column4),column5),column7)
for N2 I need to write on (row2,col3),(row2,column5)
for N3 I need to write on (row3,column3),column6),column7)

我从db / csv文件中逐个接收每个用户的数据.我一开始并不知道所有的组,我只是在我检查之后在列上添加了唯一的组,而不是为之前的用户添加的.

如果一个组是新的,尚未出现,我将添加一个包含组名的新列.如果存在,我检查列上的组的位置,并在行中写入x.

我尝试使用2个列表或2个词典,但第一行的对应关系无法正确设置.

for a in b
.....
 if group_name not in groups_pos.items():
        groups_pos[col_pos] = group_name
        name_group[row_pos] = group_name
        col_pos += 1   
row_pos += 1
......

for col,value in groups_pos.items():
    sheet.cell(row=1,column=2+col).value = value
    for row,value_p in groups_pos.items():
      sheet.cell(row=row,column=2+col).value = 'x'`

解决方法

由于未使用库,上游/下游代码或所需的实现,因此这里是纯python实现

groups = ['A','C','D','F','B','A','E','F'] # ...
names = ['N1','N1','N2','N3','N3'] # ...
row_names,col_names,data_table = [],[],[[]]

# for loop used to simulate getting data one at a time
for name,group in zip(names,groups):

    # see if that name exists in table
    if name in row_names:
        row = row_names.index(name)
    else:
        row_names.append(name)
        row = len(row_names)-1
        # add new row of empty strings
        data_table.append(['' for _ in xrange(len(data_table[0]))])

    # see if that group exists in table
    if group in col_names:
        col = col_names.index(group)
    else:
        col_names.append(group)
        col = len(col_names)-1
        # add empty string to every column
        for i,r in enumerate(data_table):
            data_table[i].append('')

    # add to table
    data_table[row][col] = 'x'

# display
print 'Table:'
print '  ','    '.join(col_names)
for rowname,row in zip(row_names,data_table):
    print rowname,'    '.join([' ' if x == '' else x for x in row])

产量

table:
   A    C    D    F    B    E
N1 x    x    x    x          
N2      x    x         x     
N3 x              x         x

一旦所有数据都在data_table中,请执行excel写入,idk导入的库是什么…所以此部分未经测试:

# add col headings
for c,col_name in enumerate(col_names):
    sheet.cell(row=1,column=c+2).value = col_name
for r,row in enumerate(data_tables):
    # add row heading on first column
    sheet.cell(row=r+2,column=1).value = row_names[r]
    # add each column to row
    for c,col in enumerate(row):
        sheet.cell(row=r+2,column=c+2).value = data_table[r][c]

或者您可以使用像pandas这样的库!如果您已安装或可以获得它,2D数据更改将更容易.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读