我正在使用commmand:
在Ubuntu,python 2中测试一些map reduce代码 #!/usr/bin/python
import sys
def reducer():
max = 0
min = 1
old_tuple = ('foo', 'bar')
i = 0
for line in sys.stdin:
data = line.strip().split("\t")
if len(data) != 3:
continue
city, year, value = data
new_tuple = (city, year)
if old_tuple != new_tuple:
if i != 0:
print "{0}\t{1}\t{2}\t{3}".format(old_tuple[0], old_tuple[1], max, min)
max = 0
min = 1
i += 1
old_tuple = new_tuple
if min > value:
min = value
if max < value:
max = value
if old_tuple != ('foo', 'bar'):
print "{0}\t{1}\t{2}\t{3}".format(old_tuple[0], old_tuple[1], max, min)
if __name__ == '__main__':
reducer()
我得到最大但不是min的正确输出,我得到min的值为1,好像它没有从它的原始值改变。每个单独的值'value'都小于1,因此在第一次迭代到for循环min时应该更改为第一个值,然后在进一步迭代时更新min。我是在放松自己的想法还是代码中有一个愚蠢的错误?请帮忙!
Alert 2009 0.215236752 1
Winnipeg 2017 0.032557214 1
我得到的输出看起来像
import java.util.Scanner;
public class Coursework {
public static void main(String[] args) {
final int linesOfMatrix; //number of lines in the matrix
System.out.println("Enter number of lines: ");
Scanner sc = new Scanner(System.in);
linesOfMatrix = sc.nextInt();
Scanner sc2 = new Scanner(System.in);
String [][] matrix = new String [linesOfMatrix][]; // declare the Matrix
for(int i=0; i < matrix.length; i++) {
System.out.println("Enter a value for the string " + (i+1) + "
through a space");
matrix[i] = sc2.nextLine().split(" ");
}
sc.close();
sc2.close();
//below must be unique sort, but he dosen't work rigth
for(int i=0; i < matrix.length; i++){
for(int j=0; j < matrix[i].length-1; j++){
if(matrix[i][j].equals(matrix[i][j+1])){
matrix[i][j+1] = matrix[i][j+1];
}
}
}
System.out.println("Matrix");
for(int i=0; i < matrix.length; i++){
for(int j=0; j < matrix[i].length-1; j++){
System.out.println("[" +(i) + "][" + (j) + "]= " + matrix[i]
[j] + " [" + (i) + "][" + (j+1) + "]= " + matrix[i][j+1] );
}
}
}
}
答案 0 :(得分:0)
首先,我使用min和max作为关键字的变量名。更改后
min => minimum
max => maximum
输出仍然不正确。实际上是同样的问题。只是在尝试以更加Pythonic的方式获得最小值和最大值之后,我才开始工作。我是Python的新手,所以这可能仍然不是最好的Pythonic方式,但下面的代码至少可以根据需要获得最小值和最大值。
#!/usr/bin/python
import sys
import math
def reducer():
list_ = []
old_tuple = ('foo', 'bar')
i = 0
for line in sys.stdin:
data = line.strip().split("\t")
if len(data) != 3:
continue
city, year, value = data
new_tuple = (city, year)
if old_tuple != new_tuple:
if i != 0:
print "{0}\t{1}\t{2}\t{3}".format(old_tuple[0], old_tuple[1], max(list_), min(list_))
list_ = []
i += 1
list_.append(value)
old_tuple = new_tuple
if old_tuple != ('foo', 'bar'):
print "{0}\t{1}\t{2}\t{3}".format(old_tuple[0], old_tuple[1], max(list_), min(list_))
if __name__ == '__main__':
reducer()