php-Arrays 函数-array_unique-移除数组中重复的值

本文介绍了 PHP 中的 array_unique() 函数,该函数用于从数组中移除重复的值并返回新的数组。文章详细解释了函数的工作原理,包括如何处理重复的值,并通过示例展示了如何使用该函数。

array_unique() 移除数组中重复的值


【功能】

         该函数将返回以指定数组为基础的没有重复值的新数组,键名保持不变

         该函数先将值作为字符串排序,然后对每个值只保留第一个遇到的键名,

         接着忽略所有后面的键名

         这并不意味着在未排序的数组中同值的第一个出现的键名被保留

【使用范围】 

         php4>=4.0.1、php5.

【使用】

         mixed array_unique( array array  ) 

         array/必需/即将进行函数处理的原始数组

【示例】

<?php
$input=array('c'=>"green",'aed','b'=>'green','blue','bed');
var_dump(array_unique($input));
/*
array(4) {
  ["c"]=>
  string(5) "green"
  [0]=>
  string(3) "aed"
  [1]=>
  string(4) "blue"
  [2]=>
  string(3) "bed"
}
*/
--------------------------------------------------------------------------- ValueError Traceback (most recent call last) Cell In[162], line 5 2 from tensorflow.keras.utils import to_categorical 4 # 划分训练集和测试集 ----> 5 x_train, x_test, y_train, y_test = train_test_split(all_rms_data, labels, test_size=0.2, random_state=42) 7 # 将标签转换为 one-hot 编码形式 8 y_train_one_hot = to_categorical(y_train, num_classes) File ~\anaconda3\Lib\site-packages\sklearn\utils\_param_validation.py:213, in validate_params.<locals>.decorator.<locals>.wrapper(*args, **kwargs) 207 try: 208 with config_context( 209 skip_parameter_validation=( 210 prefer_skip_nested_validation or global_skip_validation 211 ) 212 ): --> 213 return func(*args, **kwargs) 214 except InvalidParameterError as e: 215 # When the function is just a wrapper around an estimator, we allow 216 # the function to delegate validation to the estimator, but we replace 217 # the name of the estimator by the name of the function in the error 218 # message to avoid confusion. 219 msg = re.sub( 220 r"parameter of \w+ must be", 221 f"parameter of {func.__qualname__} must be", 222 str(e), 223 ) File ~\anaconda3\Lib\site-packages\sklearn\model_selection\_split.py:2777, in train_test_split(test_size, train_size, random_state, shuffle, stratify, *arrays) 2774 if n_arrays == 0: 2775 raise ValueError("At least one array required as input") -> 2777 arrays = indexable(*arrays) 2779 n_samples = _num_samples(arrays[0]) 2780 n_train, n_test = _validate_shuffle_split( 2781 n_samples, test_size, train_size, default_test_size=0.25 2782 ) File ~\anaconda3\Lib\site-packages\sklearn\utils\validation.py:514, in indexable(*iterables) 484 """Make arrays indexable for cross-validation. 485 486 Checks consistent length, passes through None, and ensures that everything (...) 510 [[1, 2, 3], array([2, 3, 4]), None, <3x1 sparse matrix ...>] 511 """ 513 result = [_make_indexable(X) for X in iterables] --> 514 check_consistent_length(*result) 515 return result File ~\anaconda3\Lib\site-packages\sklearn\utils\validation.py:457, in check_consistent_length(*arrays) 455 uniques = np.unique(lengths) 456 if len(uniques) > 1: --> 457 raise ValueError( 458 "Found input variables with inconsistent numbers of samples: %r" 459 % [int(l) for l in lengths] 460 ) ValueError: Found input variables with inconsistent numbers of samples: [0, 300]
05-22
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值