최신판 |
당신의 편집 |
1번째 줄: |
1번째 줄: |
| {{DISPLAYTITLE:함수 array_count_values()}}
| |
| ;uniq -c | | ;uniq -c |
| ;array_count_values | | ;array_count_values |
5번째 줄: |
4번째 줄: |
| ==Bash== | | ==Bash== |
| [[category: Bash]] | | [[category: Bash]] |
| <syntaxhighlight lang='Bash' run> | | <source lang='Bash'> |
| 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 |
| </syntaxhighlight>
| | # 2 1 |
| | | # 2 hello |
| ==POSIX Shell==
| | # 1 world |
| <syntaxhighlight lang='Bash' run>
| | </source> |
| 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]] |
| {{참고|PHP array_count_values() }}
| | <source lang='PHP'> |
| <syntaxhighlight lang='PHP' run> | | $array = array(1, 'hello', 1, 'world', 'hello'); |
| $arr = [1, 'hello', 1, 'world', 'hello']; | | print_r(array_count_values($array)); |
| print_r(array_count_values($arr)); | | // Array |
| </syntaxhighlight> | | // ( |
| | // [1] => 2 |
| | // [hello] => 2 |
| | // [world] => 1 |
| | // ) |
| | </source> |
|
| |
|
| ==Python== | | ==Python== |
| [[category: Python]] | | [[category: Python]] |
| <syntaxhighlight lang='Python' run> | | <source lang='Python'> |
| import collections | | import collections |
| lst = [1, 'hello', 1, 'world', 'hello'] | | lst = [1, 'hello', 1, 'world', 'hello'] |
| print( collections.Counter(lst) ) | | print( collections.Counter(lst) ) |
| </syntaxhighlight>
| | # Counter({1: 2, 'hello': 2, 'world': 1}) |
| | | </source> |
| ==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()]] | | *[[함수 array_count_value()]] |
| *[[함수 majority()]]
| |