Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Calculate the value of the second column according to the same item in the first example

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/03 Report--

File test:

The text is as follows:

A 2

B 3

C 6

A 8

D 9

The processing method is that if the first column is the same, the values of the second column are added (for example, a 10). After processing, the data of the second column with a value greater than 8 is printed.

Awk:

Awk'{a [$1] + = $2;} END {for (i in a) {if (a [I] > 8) {print I "" a [I];} 'test

Python:

#! / usr/bin/env pythonf = file ("test",'r') m = {} for x in f.readlines (): key,value = x.split () if m.has_key (key): M [key] = int (m [key]) + int (value) else: M [key] = value if int (m [key]) < 8: del m [key] print mfor KJV in m.items (): print KJV

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report