当前位置:网站首页>Dimension and format of data

Dimension and format of data

2022-07-04 06:16:00 Sweet cake

         We live in a multidimensional world , The same goes for data . The movie 《 The matrix 》 in , Human beings live in a world of data , Everything you see , Including taste, smell and so on , Are made up of data . Machines connect humans through matrices , The basic elements that make up the world , It's data .

         The dimension of data is everywhere . generally , The dimension of data is the organization form of data , It can be divided into One-dimensional data , Two dimensional data , Multidimensional data and high-dimensional data

  • One dimensional data is composed of ordered or disordered data with equal relationship in a linear manner , Such as lists and collections , Corresponding to the array in Mathematics .
  • Two dimensional data , It consists of multiple one-dimensional data , It's a combination of one-dimensional data , Also known as tabular data , As listing , Corresponding to matrix in Mathematics .
  • Multidimensional data is the expansion of one-dimensional or two-dimensional data on new dimensions , such as Time dimension .
  • High dimensional data shows the complex structure between data in a simple binary relationship , It can be nested in multiple layers , Such as a dictionary ,JSON,XML etc. .

1、 One-dimensional data

         One dimensional data is the simplest type of data organization . Its expression has two forms , If the data is in order , Use list type []; If the data is out of order , Then use the collection type {}.

        1.1 One dimensional data storage

         There are mainly the following ways :

Separator Example remarks
Space Apple Banana Plum Mango. durian There must be no spaces in the element
comma Apple , Banana , Plum , Mango. , durian There cannot be a comma in the element
A newline Apple \n Banana \n Plum \n Mango. \n durian
Special symbols Apple @ Banana @ Plum @ Mango. @ durian

         Be careful , The above punctuation marks are English punctuation marks , That is, half angle symbol .

         Regardless of any way of segmentation and representation , One dimensional data are linear ,for Loops can traverse data , And then each data is processed .

        1.2 Reading of one-dimensional data

txt=open('file').read()    #file Is the path and name of the file to be read 
ls=txt.split()    # Divide according to the corresponding separator 
txt.close()

        1.3 One dimensional data writing

ls=[...]    # To write to the list 
f=open('file','w')    # To write a file 
f.write(' ',join(ls))    # To write the separator 
f.close()

2、 Two dimensional data

         The common representation of two-dimensional data is two-dimensional list , Similar to plane rectangular coordinate system , By line (row) And column (column) To determine an element . Each element in the outer list can correspond to one line , It can also correspond to a column , You need to use two layers for Loop through each element .

        2.1 Two dimensional data storage

         Two dimensional data are often used CSV(Comma-Separated Values) Format to store .CSV Each line of the file is one-dimensional data , Whole CSV The file is a two-dimensional data .

        CSV It is an internationally used 、 Two dimensional data storage format , Is the standard format for data conversion , General with .csv Extension name , for Excel Wait for the editing software to read 、 Edit and save , The specifications are as follows :

  • Leave the beginning blank , Behavior unit .
  • With or without header , The header is on the first line of the file , It can be stored separately .
  • One line of data does not cross lines , There is no vacancy .
  • Comma with half angle (,) As separator , Missing elements should also be preserved .
  • If there are half quotes in the column contents ("), Replace with half angle double quotes ("") escape .
  • File read 、 Writing quotation mark , Comma reverse rule interoperability .
  • Internal code format is not limited , for ASCII、Unicode Or other .
  • Special characters are not supported

        CSV Data is stored in rows or columns depending on the program , General indexing habits ls[row][column] , First, then .

         With 2020 Comprehensive ranking of national hospitals For example

ranking Hospital name The reputation of the specialist Scientific research and academic research Comprehensive score
1 Peking Union Hospital, Chinese Academy of Medical Sciences 8015.39695.396
2 West China Hospital of Sichuan University 69.572089.57
3 PLA General Hospital 58.65812.73471.392
4 Zhongshan Hospital Affiliated to Fudan University 32.25412.03244.286
5 Ruijin Hospital Affiliated to medical school of Shanghai Jiaotong University 32.80711.43644.243
6 Tongji Hospital Affiliated to Tongji Medical College of Huazhong University of science and technology 23.67614.1537.826
7 Huashan Hospital Affiliated to Fudan University 25.2329.41534.647
8 The First Affiliated Hospital of Sun Yat sen University 23.34510.9334.275
9 Union Hospital Affiliated to Tongji Medical College of Huazhong University of science and Technology 19.48514.133.585
10 The First Affiliated Hospital of Medical College of Zhejiang University 17.00214.0631.062

         The file is stored as Hospital ranking .csv, The format is :

 ranking , Hospital name , The reputation of the specialist , Scientific research and academic research , Comprehensive score 
1, Peking Union Hospital, Chinese Academy of Medical Sciences ,80,15.396,95.396
2, West China Hospital of Sichuan University ,69.57,20,89.57
3, PLA General Hospital ,58.658,12.734,71.392
4, Zhongshan Hospital Affiliated to Fudan University ,32.254,12.032,44.286
5, Ruijin Hospital Affiliated to medical school of Shanghai Jiaotong University ,32.807,11.436,44.243
6, Tongji Hospital Affiliated to Tongji Medical College of Huazhong University of science and technology ,23.676,14.15,37.826
7, Huashan Hospital Affiliated to Fudan University ,25.232,9.415,34.647
8, The First Affiliated Hospital of Sun Yat sen University ,23.345,10.93,34.275
9, Union Hospital Affiliated to Tongji Medical College of Huazhong University of science and Technology ,19.485,14.1,33.585
10, The First Affiliated Hospital of Medical College of Zhejiang University ,17.002,14.06,31.062

2.2 Reading of two-dimensional data         

f=open(' Hospital ranking .csv')
ls=[]
for line in f:
    line=line.replace('\n','')    # Or to .strip('\n') Function delete carriage return 
    ls.append(line.split(','))
for line in ls[:4]:    # Print the top three 
    line=','.joint(line)
    print(line)
f.close()
 ranking , Hospital name , The reputation of the specialist , Scientific research and academic research , Comprehensive score 
1, Peking Union Hospital, Chinese Academy of Medical Sciences ,80,15.396,95.396
2, West China Hospital of Sichuan University ,69.57,20,89.57
3, PLA General Hospital ,58.658,12.734,71.392
>>> 

2.3 Writing of two-dimensional data

        Overwrite writing and append writing are different

wls=[[' ranking ', ' Hospital name ', ' The reputation of the specialist ', ' Scientific research and academic research ', ' Comprehensive score '],\
     ['1', ' Peking Union Hospital, Chinese Academy of Medical Sciences ', '80', '15.396', '95.396'],\
     ['2', ' West China Hospital of Sichuan University ', '69.57', '20', '89.57'],\
     ['3', ' PLA General Hospital ', '58.658', '12.734', '71.392']]
file=open('new.csv','w')
for item in wls:
    file.write(','.join(item)+'\n')
file.close()

         The result is to generate files in the current directory new.csv, Its storage format is :

 ranking , Hospital name , The reputation of the specialist , Scientific research and academic research , Comprehensive score 
1, Peking Union Hospital, Chinese Academy of Medical Sciences ,80,15.396,95.396
2, West China Hospital of Sichuan University ,69.57,20,89.57
3, PLA General Hospital ,58.658,12.734,71.392

         Append write , The original document has been rewritten :

als=[['11',' Xijing Hospital of Air Force Military Medical University ','22.974','7.771','30.745'],\
    ['12',' The first hospital of Peking University ','20.523','9.398','29.921']]
f=open(' Hospital ranking .csv','a')
f.seek(2)
for item in als:
    f.write('\n'+','.join(item))
f.close()
 ranking , Hospital name , The reputation of the specialist , Scientific research and academic research , Comprehensive score 
1, Peking Union Hospital, Chinese Academy of Medical Sciences ,80,15.396,95.396
2, West China Hospital of Sichuan University ,69.57,20,89.57
3, PLA General Hospital ,58.658,12.734,71.392
4, Zhongshan Hospital Affiliated to Fudan University ,32.254,12.032,44.286
5, Ruijin Hospital Affiliated to medical school of Shanghai Jiaotong University ,32.807,11.436,44.243
6, Tongji Hospital Affiliated to Tongji Medical College of Huazhong University of science and technology ,23.676,14.15,37.826
7, Huashan Hospital Affiliated to Fudan University ,25.232,9.415,34.647
8, The First Affiliated Hospital of Sun Yat sen University ,23.345,10.93,34.275
9, Union Hospital Affiliated to Tongji Medical College of Huazhong University of science and Technology ,19.485,14.1,33.585
10, The First Affiliated Hospital of Medical College of Zhejiang University ,17.002,14.06,31.062
11, Xijing Hospital of Air Force Military Medical University ,22.974,7.771,30.745
12, The first hospital of Peking University ,20.523,9.398,29.921

2.4 One by one processing of two-dimensional data

         Using a two-layer cycle

als=[['11',' Xijing Hospital of Air Force Military Medical University ','22.974','7.771','30.745'],\
    ['12',' The first hospital of Peking University ','20.523','9.398','29.921']]
for row in als:
    for column in row:
        print(column)
11
 Xijing Hospital of Air Force Military Medical University 
22.974
7.771
30.745
12
 The first hospital of Peking University 
20.523
9.398
29.921
>>>

3、 High dimensional data

         High dimensional data consists of key value pair data , Organize... In an object-oriented manner , Compared with one-dimensional and two-dimensional data, it can express more flexible and complex data relationships . This leads to HTML,XML、JSON And so on , It's today Internet The main form of organizing content .

原网站

版权声明
本文为[Sweet cake]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202141614039112.html