longest = 0
for value in values:
longest = max(longest, len(value))
我主要是想知道最Python化的方式来实现这一目标。
答案 0 :(得分:6)
这将返回列表public class MainActivity extends AppCompatActivity {
static final int REQUEST_IMAGE_CAPTURE = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getSupportActionBar().hide();
setContentView(R.layout.activity_main);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
galleryAddPic();
ImageView img = findViewById(R.id.img);
Bitmap bitm = BitmapFactory.decodeFile(mCurrentPhotoPath);
img.setImageBitmap(bitm);
}
private void dispatchTakePictureIntent() {
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
File photoFile = null;
try {
photoFile = createImageFile();
} catch (IOException ex) {
ex.printStackTrace();
}
if (photoFile != null) {
Uri photoURI = FileProvider.getUriForFile(this,
"com.example.android.fileprovider", photoFile);
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
}
}
}
String mCurrentPhotoPath;
private File createImageFile() throws IOException {
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
String imageFileName = "JPEG_" + timeStamp + "_";
File storageDir = getExternalFilesDir(Environment.DIRECTORY_PICTURES);
File image = File.createTempFile(imageFileName, ".jpg", storageDir);
mCurrentPhotoPath = image.getAbsolutePath();
return image;
}
private void galleryAddPic() {
Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
File f = new File(mCurrentPhotoPath);
Uri contentUri = Uri.fromFile(f);
mediaScanIntent.setData(contentUri);
this.sendBroadcast(mediaScanIntent);
}
public void cameraClick(View v){
dispatchTakePictureIntent();
}
}
中最长的列表:
values
答案 1 :(得分:1)
values = [['a','a'], ['a','b','b'], ['a','b','b','a'], ['a','b','c','a']]
max(values, key=len)
[out]:
['a', 'b', 'b', 'a']
pandas
解决方案不是竞争对手,因为它在返回第一个最长的列表方面的速度得到了公认的答案。pandas
进行分析,因此从这个角度来看,这是一个有效的问题。df.len.max()
可以用int
代替,以返回指定长度的列表。'len'
列已创建max(df.lists, key=len)
可用于pandas.Series
来查找第一个最长的列表。import pandas as pd
# convert the list of lists to a dataframe
df = pd.DataFrame({'lists': values})
# display(df)
lists
0 [a, a]
1 [a, b, b]
2 [a, b, b, a]
3 [a, b, c, a]
# create column for the length of each list
df['len'] = df.lists.map(len)
lists len
0 [a, a] 2
1 [a, b, b] 3
2 [a, b, b, a] 4
3 [a, b, c, a] 4
# select lists with max(len)
max_len = df[df.len == df.len.max()] # or [df.len == some int] for a specific length
# display(max_len)
lists len
2 [a, b, b, a] 4
3 [a, b, c, a] 4
%timeit
import pandas as pd
import random
import string
# 1M sub-list of 1-15 characters
l = [random.sample(string.ascii_letters, random.randint(1, 15)) for _ in range(10**6)]
%timeit max(l, key=len)
29.6 ms ± 1.74 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)
# function to do all the pandas stuff for testing
def get_max_len(l):
df = pd.DataFrame({'lists': l})
df['len'] = df.lists.map(len)
return df[df.len == df.len.max()]
%timeit get_max_len(l)
682 ms ± 14.9 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)
答案 2 :(得分:0)
尝试一下:
max(map(len,values))