Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Arrays 在Python中,如何从具有多个字符串的数组中获取中值字符串值?_Arrays_String_Median - Fatal编程技术网

Arrays 在Python中,如何从具有多个字符串的数组中获取中值字符串值?

Arrays 在Python中,如何从具有多个字符串的数组中获取中值字符串值?,arrays,string,median,Arrays,String,Median,我有一个数组,它保存的值例如1和0。 像这样: ['00100','00101','00101','01100','01001','00101'] '00101' 我想得到每个数组字符串中每个字符的中值: 返回应该是这样的: ['00100','00101','00101','01100','01001','00101'] '00101' 如果你能给出Python的例子,那就太好了。Python对这类问题有着天然的习惯用法。这个想法是把东西分成很好的可消化的块,所有的第0个字符,所有的第

我有一个数组,它保存的值例如1和0。 像这样:

['00100','00101','00101','01100','01001','00101']
'00101'
我想得到每个数组字符串中每个字符的中值:

返回应该是这样的:

['00100','00101','00101','01100','01001','00101']
'00101'

如果你能给出Python的例子,那就太好了。

Python对这类问题有着天然的习惯用法。这个想法是把东西分成很好的可消化的块,所有的第0个字符,所有的第1个字符,所有的第2个字符。。。等(拉链(*a)部分);然后计算每个区块的中位数(排序的(d)[len(a)/2]部分);然后再将这些块重新连接在一起。这给了我们一条线索:

def findMedians(a):
    return ''.join(sorted(d)[len(a)/2] for d in zip(*a))

print findMedians(['00100','00101','00101','01100','01001','00101'])
# 00101
print findMedians(['1210', '2501', '1234'])
# 1211
如果您觉得这有点困惑,请尝试:

print zip('123','456','789')
a = ['123','456','789']
print zip(*a)
查看“*”运算符在这里执行的操作。

因此您有了2d数组

array = {['1210'], ['2501'], ['1234']} // [3][4] array
我将创建一个新的2d数组,该数组存储包含原始数组中每个字符串的第0个字符的字符串,第一个字符,第二个字符

tempArray[4][3]

i = 0;
for(i = 0; i < 4; i = i + 1) 
{
    for(int j = 0; j < 3; j = j + 1){
        tempArray[i][j] = array[j][i]
    }
    // when outer cycle finishes collecting nth chars
    // you need to sort it from smallest to largest digit
    sortAscending(tempArray[i])
}
最后,收集新字符串的中间字符

stringOfMedians[4];
for(i = 0; i < 4; i = i + 1){
    // Take middle element from each string and put it in final string.
    stringOfMedians[i] = tempArray[i][strlen(tempArray[i])/2]
}
还有一句话。你应该加上

stringOfMedians[i] = tempArray[i][strlen(tempArray[i])/2]
到我为优化显示的第一个代码块的外循环。它不必有自己的循环。 最终结果:

array = {['1210'], ['2501'], ['1234']} // [3][4] array

tempArray[4][3]; 

i = 0;
for(i = 0; i < 4; i = i + 1) 
{
    for(int j = 0; j < 3; j = j + 1){
        tempArray[i][j] = array[j][i]
    }
    // when outer cycle finishes collecting nth chars
    // you need to sort it from smallest to largest digit
    sortAscending(tempArray[i]);

    // Take middle element from string and put it in final string.
    stringOfMedians[i] = tempArray[i][strlen(tempArray[i])/2]
}
array={['1210'],['2501'],['1234']}/[3][4]数组
temparay[4][3];
i=0;
对于(i=0;i<4;i=i+1)
{
对于(int j=0;j<3;j=j+1){
tempArray[i][j]=数组[j][i]
}
//当外循环完成收集第n个字符时
//您需要从最小数字到最大数字进行排序
排序(临时数组[i]);
//从字符串中提取中间元素并将其放入最后一个字符串中。
stringOfMedians[i]=tempArray[i][strlen(tempArray[i])/2]
}

您的解决方案实际上与我的相同;但是Python有很酷的内置组件,比如zip,还有列表理解,这就不需要像“tempArray”这样的显式临时变量了。@ChasBrown这两个都是很好的答案。我只是想保持简短。约瑟夫确实对此做了更多的解释;我也想给他荣誉。