python random.shuffle如何更改变量

时间:2019-06-04 15:34:04

标签: python python-3.x

Python的random.shuffle()函数如何在不设置变量的情况下更改变量?

例如,如果我这样做了

def shuffle(array):
    shuffled = ... # Some code to shuffle array
    return shuffled

我做到了

array = shuffle(array)

这是有道理的,但是随机库如何不需要此步骤?没有办法在python中指向变量,所以它如何设置变量

shuffle(array)

没有将array显式设置为返回值shuffle(array)

2 个答案:

答案 0 :(得分:3)

来自documentation

  

export class CarList extends React.Component { componentDidMount() { // Schedule the updates and forcer re-render if we had cars. this.timer = setInterval(() => { // this.props should be different each time. const { carList } = this.props; if (carList !== null && carList.length) { this.forceUpdate() }; }, 1000) } componentWillUnmount() { clearInterval(this.timer); } getExirationTime(expireIn) { // modified code from your code var right Now = Math.floor((Date.now()/1000)+3600); var expires = parseInt(timer.dataset.expires) // in seconds if (rightNow > expireIn) { return 'expired'; } else { var seconds = expireIn - rightNow var minutes = Math.floor(seconds/60) var hours = Math.floor(minutes/60) var days = Math.floor(hours/24) seconds = ('0' + String(seconds%60)).slice(-2) minutes = ('0' + String(minutes%60)).slice(-2) hours = ('0' + String(hours%24)).slice(-2) return days + 'd ' + hours + 'h ' + minutes + 'm ' + seconds + 's' } } render() { const {carList}=this.props; console.log(carList); ......   将序列x随机播放

它们的键在这里是“就地” ,这意味着它会改变原始数组。因此,它不需要显式返回它。

答案 1 :(得分:1)

在函数参数中传递的数组是引用,这就是为什么您不需要获取返回值的原因。

def change_arr(some_array):
    some_array[1] = "World"

arr = ['Hello', 'Hello']
print(arr)
change_arr(arr)
print(arr)

输出:

['Hello', 'Hello']
['Hello', 'World']