当前位置:网站首页>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 | 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 |
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 .
边栏推荐
- Halcon image calibration enables subsequent image processing to become the same as the template image
- 2022.7.2-----leetcode. eight hundred and seventy-one
- ES6 modularization
- SQL injection SQL lab 11~22
- Learning multi-level structural information for small organ segmentation
- 746. Climb stairs with minimum cost
- 安装 Pytorch geometric
- A little understanding of GSLB (global server load balance) technology
- el-select如何实现懒加载(带搜索功能)
- Install pytoch geometric
猜你喜欢
Inputstream/outputstream (input and output of file)
[March 3, 2019] MAC starts redis
雲原生——上雲必讀之SSH篇(常用於遠程登錄雲服務器)
如何实现视频平台会员多账号登录
740. Delete and get points
HMS v1.0 appointment. PHP editid parameter SQL injection vulnerability (cve-2022-25491)
APScheduler如何设置任务不并发(即第一个任务执行完再执行下一个)?
QT 获取随机颜色值设置label背景色 代码
buuctf-pwn write-ups (8)
js arguments参数使用和详解
随机推荐
如何展开Collapse 的所有折叠面板
接地继电器DD-1/60
Configure cross compilation tool chain and environment variables
Detectron: train your own data set -- convert your own data format to coco format
QT QTableWidget 表格列置顶需求的思路和代码
AWT introduction
树形dp
Manually page the list (parameter list, current page, page size)
QT get random color value and set label background color code
4G wireless all network solar hydrological equipment power monitoring system bms110
buuctf-pwn write-ups (8)
746. Climb stairs with minimum cost
How to solve the component conflicts caused by scrollbars in GridView
QT qtablewidget table column top requirements ideas and codes
MySQL information_ Schema database
Native Cloud - SSH articles must be read on Cloud (used for Remote Login to Cloud Server)
C语言中的函数(详解)
After the festival, a large number of people change careers. Is it still time to be 30? Listen to the experience of the past people
2022.7.3-----leetcode. five hundred and fifty-six
Json Web token - jwt vs. Traditional session login Authentication