"함수 array count values()"의 두 판 사이의 차이

51번째 줄: 51번째 줄:
print( collections.Counter(lst) )
print( collections.Counter(lst) )
# Counter({1: 2, 'hello': 2, 'world': 1})
# Counter({1: 2, 'hello': 2, 'world': 1})
</source>
==Perl==
[[category:Perl]]
<source lang='Perl'>
my %cnt;
my @lst = (1, 'hello', 1, 'world', 'hello');
$cnt{$_}++ for @lst;
print %cnt;
# 12hello2world1
</source>
</source>



2018년 1월 17일 (수) 11:53 판

uniq -c
array_count_values

1 Bash

ARR=(1 "hello" 1 "world" "hello")
for VALUE in "${ARR[@]}"; do
	echo $VALUE
done | sort | uniq -c
#      2 1
#      2 hello
#      1 world

2 POSIX Shell

ARR="1 hello 1 world hello"
for VALUE in $ARR
do
        echo $VALUE
done | sort | uniq -c
#      2 1
#      2 hello
#      1 world

3 C

4 PHP

$array = array(1, 'hello', 1, 'world', 'hello');
print_r(array_count_values($array));
// Array
// (
//     [1] => 2
//     [hello] => 2
//     [world] => 1
// )

5 Python

import collections
lst = [1, 'hello', 1, 'world', 'hello']
print( collections.Counter(lst) )
# Counter({1: 2, 'hello': 2, 'world': 1})

6 Perl

my %cnt;
my @lst = (1, 'hello', 1, 'world', 'hello');
$cnt{$_}++ for @lst;
print %cnt;
# 12hello2world1

7 SQL

7.1 MySQL

mysql> SELECT * FROM array;
+-------+
| value |
+-------+
| 1     |
| hello |
| 1     |
| world |
| hello |
+-------+
5 rows in set (0.00 sec)
mysql> SELECT value, COUNT(*) FROM array GROUP BY value;
+-------+----------+
| value | COUNT(*) |
+-------+----------+
| 1     |        2 |
| hello |        2 |
| world |        1 |
+-------+----------+
3 rows in set (0.00 sec)

8 같이 보기

문서 댓글 ({{ doc_comments.length }})
{{ comment.name }} {{ comment.created | snstime }}