Python:包含范围列表的合并列表

时间:2016-02-19 17:15:29

标签: python list python-2.7 range list-comprehension

我有list

L = ['a', 'b']

我需要通过连接原始list来创建新的list,其范围从1k。例如:

k = 4
L1 = ['a1','b1', 'a2','b2','a3','b3','a4','b4']

我试试:

l1 = L * k
print l1
#['a', 'b', 'a', 'b', 'a', 'b', 'a', 'b']

l = [ [x] * 2  for x in range(1, k + 1) ]
print l
#[[1, 1], [2, 2], [3, 3], [4, 4]]

l2 = [item for sublist in l for item in sublist]
print l2
#[1, 1, 2, 2, 3, 3, 4, 4]

print zip(l1,l2)
#[('a', 1), ('b', 1), ('a', 2), ('b', 2), ('a', 3), ('b', 3), ('a', 4), ('b', 4)]

print [x+ str(y) for x,y in zip(l1,l2)] 
#['a1', 'b1', 'a2', 'b2', 'a3', 'b3', 'a4', 'b4']

但我觉得这很复杂。 什么是最快,最通用的解决方案?

2 个答案:

答案 0 :(得分:17)

您可以使用列表理解:

L = ['a', 'b']
k = 4
L1 = ['{}{}'.format(x, y) for y in range(1, k+1) for x in L]
print(L1)

<强>输出

['a1', 'b1', 'a2', 'b2', 'a3', 'b3', 'a4', 'b4']

答案 1 :(得分:-1)

我的解决方案几乎相同,但输出顺序不同......

订单有关系吗?

这是我的解决方案

public static String camPicturePath;

public static void captureImage(Activity activity) {
        File photoFile = null;
        Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        // Ensure that there's a camera activity to handle the intent
        if (takePictureIntent.resolveActivity(activity.getPackageManager()) != null) {
            // Create the File where the photo should go
            try {
                photoFile = createImageFile();
            } catch (IOException ex) {
                // Error occurred while creating the File
                ex.printStackTrace();
            }
            // Continue only if the File was successfully created
            if (photoFile != null) {
                takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT,
                        Uri.fromFile(photoFile));
                activity.startActivityForResult(takePictureIntent, 100);
            }
        }
        camPicturePath = (photoFile != null) ? photoFile.getAbsolutePath() : null;
    }


private LruCache<String, Bitmap> mMemoryCache;

public void setSelectedPic(CustomNetworkImageView view, String url) {
    Context context = view.getContext();
    if (!TextUtils.isEmpty(url)) {
        if (url.startsWith("http")) {
            view.setImageUrl(url, CustomVolleyRequest.getInstance(context).getImageLoader());
        } else {
            try {
                Uri uri = Uri.parse(url);
                Bitmap bitmap = mMemoryCache.get(url);
                if (bitmap == null) {
                    InputStream is = context.getContentResolver().openInputStream(uri);
                    bitmap = BitmapFactory.decodeStream(is);
                    Bitmap scaled = ImageUtil.getInstance().scaleBitmap(bitmap);
                    mMemoryCache.put(url, scaled);
                    if (is!=null) {
                        is.close();
                    }
                }
                view.setLocalImageBitmap(bitmap);
            } catch (Exception ex) {
                Log.e("Image", ex.getMessage(), ex);
            }
        }
    } else {
        view.setImageUrl("", CustomVolleyRequest.getInstance(view.getContext()).getImageLoader());
    }
}
public Bitmap scaleBitmap(Bitmap bitmap) {
    int width = WIDTH;
    int height = (WIDTH * bitmap.getHeight()) / bitmap.getWidth();
    return Bitmap.createScaledBitmap(bitmap, width, height, false);
}

输出:

from itertools import product
L = ['a', 'b']
k = 4
L2 = range(1, k+1)
print [x+ str(y) for x,y in list(product(L,L2))]