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

 
(사용자 3명의 중간 판 24개는 보이지 않습니다)
1번째 줄: 1번째 줄:
{{DISPLAYTITLE:함수 array_count_values()}}
;uniq -c
;uniq -c
;array_count_values
;array_count_values
4번째 줄: 5번째 줄:
==Bash==
==Bash==
[[category: Bash]]
[[category: Bash]]
<source lang='Bash'>
<syntaxhighlight lang='Bash' run>
ARR=(1 "hello" 1 "world" "hello")
ARR=(1 "hello" 1 "world" "hello")
for VALUE in "${ARR[@]}"; do
for VALUE in "${ARR[@]}"; do
echo $VALUE
echo $VALUE
done | sort | uniq -c
done | sort | uniq -c
#      2 1
</syntaxhighlight>
#      2 hello
 
#      1 world
==POSIX Shell==
</source>
<syntaxhighlight lang='Bash' run>
ARR="1 hello 1 world hello"
for VALUE in $ARR
do
        echo $VALUE
done | sort | uniq -c
</syntaxhighlight>
 
==C==
[[category: C]]
{{참고|C언어 array_count_values()}}


==PHP==
==PHP==
[[category: PHP]]
[[category: PHP]]
<source lang='PHP'>
{{참고|PHP array_count_values() }}
$array = array(1, "hello", 1, "world", "hello");
<syntaxhighlight lang='PHP' run>
print_r(array_count_values($array));
$arr = [1, 'hello', 1, 'world', 'hello'];
// Array
print_r(array_count_values($arr));
// (
</syntaxhighlight>
//    [1] => 2
 
//    [hello] => 2
==Python==
//    [world] => 1
[[category: Python]]
// )
<syntaxhighlight lang='Python' run>
</source>
import collections
lst = [1, 'hello', 1, 'world', 'hello']
print( collections.Counter(lst) )
</syntaxhighlight>
 
==Perl==
[[category:Perl]]
<syntaxhighlight lang='Perl' run>
my %cnt;
my @lst = (1, 'hello', 1, 'world', 'hello');
$cnt{$_}++ for @lst;
print %cnt;
</syntaxhighlight>
 
==R==
[[분류: R]]
{{참고|R table()}}
<syntaxhighlight lang='r' run>
v <- c(1, 'hello', 1, 'world', 'hello')
table(v)
</syntaxhighlight>
 
==SQL==
[[category: SQL]]
===MySQL===
[[category: MySQL]]
<syntaxhighlight lang='console'>
mysql> SELECT * FROM array;
+-------+
| value |
+-------+
| 1     |
| hello |
| 1    |
| world |
| hello |
+-------+
5 rows in set (0.00 sec)
</syntaxhighlight>
<syntaxhighlight lang='console'>
mysql> SELECT value, COUNT(*) FROM array GROUP BY value;
+-------+----------+
| value | COUNT(*) |
+-------+----------+
| 1    |        2 |
| hello |        2 |
| world |        1 |
+-------+----------+
3 rows in set (0.00 sec)
</syntaxhighlight>
 
==같이 보기==
==같이 보기==
*[[array_unique]]
*[[함수 array_unique()]]
*[[함수 array_count_value()]]
*[[함수 majority()]]

2021년 4월 14일 (수) 01:21 기준 최신판

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 POSIX Shell[ | ]

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

3 C[ | ]

4 PHP[ | ]

$arr = [1, 'hello', 1, 'world', 'hello'];
print_r(array_count_values($arr));

5 Python[ | ]

import collections
lst = [1, 'hello', 1, 'world', 'hello']
print( collections.Counter(lst) )

6 Perl[ | ]

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

7 R[ | ]

v <- c(1, 'hello', 1, 'world', 'hello')
table(v)

8 SQL[ | ]

8.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)

9 같이 보기[ | ]

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