def minimum_index(xs):
minimum_index=xs[0]
for i in range(len(xs)):
if xs[i]<xs[i+1]:
min_i=i
elif xs[i]>xs[i+1]:
min_i=i+1
continue
return minimum_index
这看起来对我来说是正确的,但由于某种原因,我一直试图改变一些事情,我得到一个不正确的返回值或没有返回值。
答案 0 :(得分:1)
简化功能
public class RoundsListAdapter extends CursorAdapter {
private String mOptionOne = "option_1";
private String mOptionTwo = "option_2";
private String mOptionThree = "option_3";
private String mOptionFour = "option_4";
private LayoutInflater mLayoutInflator;
private Context mContext;
public RoundsListAdapter(Context context, Cursor c, boolean autoQuery,int windowHeight, int windowWidth) {
super(context, c,autoQuery);
mLayoutInflator = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
final View customView = mLayoutInflator.inflate(R.layout.talk_row_list,null);
return customView;
}
@Override
public void bindView(View view, Context context, final Cursor cursor) {
final CustomButtonFlat option_1 = (CustomButtonFlat)view.findViewById(R.id.talks_answerA);
View option_1SepartorView = view.findViewById(R.id.talks_answerA_seprator);
final CustomButtonFlat option_2 = (CustomButtonFlat)view.findViewById(R.id.talks_answerB);
View option_2SepartorView = view.findViewById(R.id.talks_answerB_seprator);
final CustomButtonFlat option_3 = (CustomButtonFlat)view.findViewById(R.id.talks_answerC);
View option_3SepartorView = view.findViewById(R.id.talks_answerC_seprator);
final CustomButtonFlat option_4 = (CustomButtonFlat)view.findViewById(R.id.talks_answerD);
final ImageView talksThankImage = (ImageView)view.findViewById(R.id.talks_thank_image);
final TextView talksNoOfThanks = (TextView)view.findViewById(R.id.talks_no_of_thanks);
String optionA = cursor.getString(cursor.getColumnIndexOrThrow(TalkTable.TALK_OPTION_1));
String optionB = cursor.getString(cursor.getColumnIndexOrThrow(TalkTable.TALK_OPTION_2));
String optionC = cursor.getString(cursor.getColumnIndexOrThrow(TalkTable.TALK_OPTION_3));
String optionD = cursor.getString(cursor.getColumnIndexOrThrow(TalkTable.TALK_OPTION_4));
if(optionA == null || optionA.length()<=0){
option_1.setVisibility(View.GONE);
option_1SepartorView.setVisibility(View.GONE);
}else{
option_1.setVisibility(View.VISIBLE);
option_1SepartorView.setVisibility(View.VISIBLE);
option_1.setText(cursor.getString(cursor.getColumnIndexOrThrow(TalkTable.TALK_OPTION_1)));
}
if(optionB == null || optionB.length()<=0){
option_2.setVisibility(View.GONE);
option_2SepartorView.setVisibility(View.GONE);
}else{
option_2.setVisibility(View.VISIBLE);
option_2SepartorView.setVisibility(View.VISIBLE);
option_2.setText(cursor.getString(cursor.getColumnIndexOrThrow(TalkTable.TALK_OPTION_1)));
}
if(optionC == null || optionC.length()<=0){
option_3.setVisibility(View.GONE);
option_3SepartorView.setVisibility(View.GONE);
}else{
option_3.setVisibility(View.VISIBLE);
option_3SepartorView.setVisibility(View.VISIBLE);
option_3.setText(cursor.getString(cursor.getColumnIndexOrThrow(TalkTable.TALK_OPTION_1)));
}
if(optionD == null || optionD.length()<=0){
option_4.setVisibility(View.GONE);
}else{
option_4.setVisibility(View.VISIBLE);
option_4.setText(cursor.getString(cursor.getColumnIndexOrThrow(TalkTable.TALK_OPTION_1)));
}
// only set these listeners if MCQ in the talks are available
final String mcqAnswer = cursor.getString(cursor.getColumnIndexOrThrow(TalkTable.TALK_ANSWER));
Log.d(TAG," Talk mcq answer is = " + mcqAnswer);
option_1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(mcqAnswer.equalsIgnoreCase(mOptionOne)){
option_1.setBackgroundResource(R.color.correctMCQAnswer);
}else if(mcqAnswer.equalsIgnoreCase(mOptionTwo)){
option_1.setBackgroundColor(Color.RED); // settings wrong color
option_2.setBackgroundResource(R.color.correctMCQAnswer);
// setting correct color for correct answer
Log.d(TAG, " Setting button color answer 2");
}else if(mcqAnswer.equalsIgnoreCase(mOptionThree)){
option_1.setBackgroundColor(Color.RED); // settings wrong color
option_3.setBackgroundResource(R.color.correctMCQAnswer);
Log.d(TAG, " Setting button color answer 3");
}else if(mcqAnswer.equalsIgnoreCase(mOptionFour)){
option_1.setBackgroundColor(Color.RED); // settings wrong color
option_4.setBackgroundResource(R.color.correctMCQAnswer);
Log.d(TAG, " Setting button color answer 4");
}
}
});
option_2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (mcqAnswer.equalsIgnoreCase(mOptionOne)) {
option_2.setBackgroundColor(Color.RED);
option_1.setBackgroundResource(R.color.correctMCQAnswer);
} else if (mcqAnswer.equalsIgnoreCase(mOptionTwo)) {
option_2.setBackgroundResource(R.color.correctMCQAnswer);
} else if (mcqAnswer.equalsIgnoreCase(mOptionThree)) {
option_2.setBackgroundColor(Color.RED); // settings wrong color
option_3.setBackgroundResource(R.color.correctMCQAnswer);
} else if (mcqAnswer.equalsIgnoreCase(mOptionFour)) {
option_2.setBackgroundColor(Color.RED); // settings wrong color
option_4.setBackgroundResource(R.color.correctMCQAnswer);
}
}
});
option_3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (mcqAnswer.equalsIgnoreCase(mOptionOne)) {
option_3.setBackgroundColor(Color.RED);
option_1.setBackgroundResource(R.color.correctMCQAnswer);
} else if (mcqAnswer.equalsIgnoreCase(mOptionTwo)) {
option_3.setBackgroundColor(Color.RED); // settings wrong color
option_2.setBackgroundResource(R.color.correctMCQAnswer);
} else if (mcqAnswer.equalsIgnoreCase(mOptionThree)) {
option_3.setBackgroundResource(R.color.correctMCQAnswer);
} else if (mcqAnswer.equalsIgnoreCase(mOptionFour)) {
option_3.setBackgroundColor(Color.RED); // settings wrong color
option_4.setBackgroundResource(R.color.correctMCQAnswer);
}
}
});
option_4.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (mcqAnswer.equalsIgnoreCase(mOptionOne)) {
//mQuestion_1.setBackgroundColor(Color.GREEN); // setting correct color
option_4.setBackgroundColor(Color.RED);
option_1.setBackgroundResource(R.color.correctMCQAnswer);
} else if (mcqAnswer.equalsIgnoreCase(mOptionTwo)) {
option_4.setBackgroundColor(Color.RED); // settings wrong color
option_2.setBackgroundResource(R.color.correctMCQAnswer);
} else if (mcqAnswer.equalsIgnoreCase(mOptionThree)) {
option_4.setBackgroundColor(Color.RED); // settings wrong color
option_3.setBackgroundResource(R.color.correctMCQAnswer);
} else if (mcqAnswer.equalsIgnoreCase(mOptionFour)) {
option_4.setBackgroundResource(R.color.correctMCQAnswer);
}
}
});
}
}
或以更加pythonic的方式
def minimum_index(xs):
ans = 0
for i in range(1, len(xs)):
if xs[i] < xs[ans]:
ans = i
return ans
答案 1 :(得分:0)
您的代码至少有两个问题:您似乎有两个代表最小索引的变量,并且您将它们混合起来。此外,仅比较后续元素是不够的,您必须与最小值进行比较。试试这个:
def minimum_index(xs):
minx = xs[0]
mini = 0
for i in range(1,len(xs)):
if xs[i]<minx:
mini = i
minx = xs[i]
return mini
如果您使用的是numpy
,那么您只需使用他们的numpy.argmin(xs)
。