将任意数量的列表的元素连接到一个字符串python列表中

时间:2018-05-16 09:04:46

标签: python list

我想将两个列表的元素连接到一个列表中并添加一些字符,如下所示:

list_1 = ['some1','some2','some3']
list_2 = ['thing1','thing2','thing3']

joined_list = ['some1_thing1', 'some2_thing2', 'some3_thing3']

但是,我事先并不知道有多少列表需要这样做,即我想为任意数量的列表执行此操作

另外,我目前收到以下表格的清单:

list_A = [('some1','thing1'),('some2','thing2'),('some3','thing3')]

所以我将它分成如下列表:

list_B = [i for i in zip(*list_A)]

我这样做是因为有时我有一个int而不是一个字符串

list_A = [('some1','thing1',32),('some1','thing1',42),('some2','thing3', 52)] 

所以我可以在

之后这样做
list_C = [list(map(str,list_B[i])) for i in range(0,len(list_B)]

基本上list_1list_2list_C的元素。

那么有更有效的方法来做这一切吗?

3 个答案:

答案 0 :(得分:3)

如果您使用的是python> = 3.6:

,请尝试此操作
[f'{i}_{j}' for i,j in zip(list_1, list_2)]

如果你使用python3.5,你可以这样做:

 ['{}_{}'.format(i,j) for i,j in zip(list_1, list_2)]

如果您不想使用格式化字符串,也可以使用它:

['_'.join([i,j]) for i,j in zip(list_1, list_2)]

答案 1 :(得分:2)

您可以joinlist_A基础上split这样运作,无需int可能的list_A = [('some1','thing1',32),('some1','thing1',42), ('some2','thing3', 52)] ["_".join(map(str, i)) for i in list_A] 值:

['some1_thing1_32', 'some1_thing1_42', 'some2_thing3_52']

<强>输出:

tuple

<强>更新

对于您的要求,您要忽略list_A中最后if-else的最后一个元素,需要在list-comprehension内添加["_".join(map(str, i)) if list_A.index(i) != len(list_A)-1 else "_".join(map(str, i[:-1])) for i in list_A ] 条件,如下所示:

['some1_thing1_32', 'some1_thing1_42', 'some2_thing3']

更新了输出:

\Stripe\Stripe::setApiKey("$stripe_secret_key_test");
// Retrieve the request's body and parse it as JSON:
$input = @file_get_contents('php://input');
$event_json = json_decode($input);

// Update database with new billing entry
if ($event_json->type == 'invoice.payment_succeeded') {
    $invoice_id = $event_json->data->object->id;
    $customer = $event_json->data->object->customer;
    $date = $event_json->data->object->date;
    $price = $event_json->data->object->amount_paid;
    $user = $event_json->data->object->lines->data[0]->metadata->userid;
    $stripe_sub_id = $event_json->data->object->lines->data[0]->id;
    $plan_id = $event_json->data->object->lines->data[0]->plan->id;
    $stamp_created = $event_json->data->object->lines->data[0]->plan->created;
    $comments = $event_json->data->object->lines->data[0]->plan->nickname;
    $period_start = $event_json->data->object->lines->data[0]->period->start;
    $period_end = $event_json->data->object->lines->data[0]->period->end;

    $content=array("invoice"=>$invoice_id,
        "user"=>$user,
        "stripe_cust_id"=>$customer,
        "stripe_sub_id"=>$stripe_sub_id,
        "idea_id"=>0,
        "billing_date"=>$date,
        "plan"=>$plan_id,
        "price"=>$price,
        "stamp_created"=>$stamp_created,
        "period_start"=>$period_start,
        "period_end"=>$period_end,
        "status"=>'paid',
        "printed"=>0);
    insertGWInfo('billing',$content);

    $content=array("user"=>$user,
        "trans_time"=>date("Y-m-d H:i:s"),
        "ip"=>$_SERVER['REMOTE_ADDR'],
        "trans_type"=>1,
        "cash_amount"=>$price,
        "comments"=>"Subscription for ".$comments."",
        "idea_id"=>0);
    insertGWInfo('user_accounting',$content);
}

答案 2 :(得分:1)

为了忽略list_A中每个元组的最后一个元素,我发现这是最快的方式:

["_".join(map(str, i)) for i in [x[:-1] for x in list_A] ]