当前位置:网站首页>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.0622.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.9212.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 .
边栏推荐
- win10清除快速访问-不留下痕迹
- C language - Blue Bridge Cup - Snake filling
- C实现贪吃蛇小游戏
- Cloud native - SSH article that must be read on the cloud (commonly used for remote login to ECS)
- APScheduler如何设置任务不并发(即第一个任务执行完再执行下一个)?
- C realize Snake games
- QT releases multilingual International Translation
- 云原生——上云必读之SSH篇(常用于远程登录云服务器)
- How to implement lazy loading in El select (with search function)
- Detectron: train your own data set -- convert your own data format to coco format
猜你喜欢

雲原生——上雲必讀之SSH篇(常用於遠程登錄雲服務器)

QT QTableWidget 表格列置顶需求的思路和代码

Layoutmanager layout manager: flowlayout, borderlayout, GridLayout, gridbaglayout, CardLayout, BoxLayout

【无标题】

JS execution mechanism

QT 获取随机颜色值设置label背景色 代码

Webrtc quickly set up video call and video conference

复合非线性反馈控制(二)

我的NVIDIA开发者之旅——优化显卡性能

How to solve the component conflicts caused by scrollbars in GridView
随机推荐
APScheduler如何设置任务不并发(即第一个任务执行完再执行下一个)?
Functions in C language (detailed explanation)
509. Fibonacci number, all paths of climbing stairs, minimum cost of climbing stairs
注释与注解
ES6 modularization
Compound nonlinear feedback control (2)
QT QTableWidget 表格列置顶需求的思路和代码
接地继电器DD-1/60
How to implement lazy loading in El select (with search function)
JS扁平化数形结构的数组
Detectron: train your own data set -- convert your own data format to coco format
Experience weekly report no. 102 (July 4, 2022)
微信小程序使用rich-text中图片宽度超出问题
Layoutmanager layout manager: flowlayout, borderlayout, GridLayout, gridbaglayout, CardLayout, BoxLayout
C實現貪吃蛇小遊戲
746. Climb stairs with minimum cost
How to determine whether an array contains an element
Invalid revision: 3.18.1-g262b901-dirty
Tutle clock improved version
"In simple language programming competition (basic)" part 1 Introduction to language Chapter 3 branch structure programming