说我在列表中有一个列表,该列表包含在列表中。例如:
foo = [[[2, 2, 2], [1, 1, 1], [3, 3, 3]], [[2, 2, 2], [1, 1, 1], [3, 3, 3]]]
我想按顺序对其进行排序:
foo = [[[1, 1, 1], [2, 2, 2], [3, 3, 3]], [[1, 1, 1], [2, 2, 2], [3, 3, 3]]]
我可以使用[sorted(i) for i in foo]
实现这一目标。但是,有什么方法可以对列表进行排序而无需理解列表(或创建新列表)?
列表本身中的值将更改,但无需排序。
我尝试过的所有方法都归结为与上述相同的方法。
答案 0 :(得分:6)
如果要避免创建新列表,则只需遍历列表并调用.sort()
>>> foo = [[[2, 2, 2], [1, 1, 1], [3, 3, 3]], [[2, 2, 2], [1, 1, 1], [3, 3, 3]]]
for i in foo:
i.sort()
>>> foo
[[[1, 1, 1], [2, 2, 2], [3, 3, 3]], [[1, 1, 1], [2, 2, 2], [3, 3, 3]]]
答案 1 :(得分:2)
如果您不介意创建新列表,该如何使用import android.os.AsyncTask;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
public class fetchData extends AsyncTask<Void,Void,Void> {
String data ="";
String dataParsed ="";
String singleParsed="";
@Override
protected Void doInBackground(Void... voids) {
try {
URL url = new URL("https://itunes.apple.com/search?term="+MainActivity.searchValue+"&entity="+MainActivity.entityValue);
HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
InputStream inputStream = httpURLConnection.getInputStream();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
StringBuilder sb = new StringBuilder();
String line = null;
while((line = bufferedReader.readLine()) != null){
sb.append(line + "\n");
data = data + line;
}
JSONArray JA = new JSONArray(data);
for (int i = 0; i < JA.length(); i++) {
JSONObject JO = (JSONObject) JA.get(i);
singleParsed = " " +JO.get("trackName");
dataParsed = dataParsed + singleParsed;
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
MainActivity.data.setText(this.dataParsed);
}
}
函数:
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
Button submit;
public static TextView data;
public static String searchValue ="";
public static String entityValue ="";
public Spinner spinner;
public static final String[]paths = {"Music","Movie","Software"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Setup Spinner
spinner = (Spinner) findViewById(R.id.spinner);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(MainActivity.this,android.R.layout.simple_spinner_dropdown_item,paths);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
//Setup Button, Textview, and EditText
submit = (Button) findViewById(R.id.button);
data = (TextView) findViewById(R.id.textView);
final EditText searchText = (EditText) findViewById(R.id.searchEditText);
submit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
searchValue = searchText.getText().toString();
if(spinner.getSelectedItem().equals("Music")){
entityValue = "musicTrack";
}else if(spinner.getSelectedItem().equals("Movie")){
entityValue="movie";
}else if(spinner.getSelectedItem().equals("Software")){
entityValue="software";
}
System.out.println(entityValue);
fetchData process = new fetchData();
process.execute();
}
});
}
}