有没有更多的“ pythonic”方式从具有自定义分隔符的列表生成字符串

时间:2019-12-30 22:39:15

标签: python

我想知道是否有比下面的方法更有效的从列表生成字符串的方法。

免责声明:我是一名生物物理学家,对python没有很深的了解,并且确实进行了搜索并测试了诸如resid {}.format(a) / ([a]) / (*a) / (x for x in a)之类的组合,但是可能我不知道要寻找什么。 ..我知道下面的代码有效,但是我不清楚为什么列出的任何代码都不起作用。

输入:

a=[23,33,105,400]

代码:

c=""
for x in a[0:-1]: 
     c = c + "resid {} or ".format(x)
c=c+"resid {}".format(a[-1])
print(c)

输出:

resid 23 or resid 33 or resid 105 or resid 400

3 个答案:

答案 0 :(得分:7)

使用字符串连接

" or ".join("resid {}".format(x) for x in [23,33,105,400])
# 'resid 23 or resid 33 or resid 105 or resid 400'

您也可以使用f字符串

" or ".join(f'resid {x}' for x in [23,33,105,400])

答案 1 :(得分:1)

您可以按如下方式创建格式字符串:

format_string = (len(a)-1) * 'resid {} or ' + 'resid {} '

然后通过以下方法对其应用a

print(format_string.format(*a))

答案 2 :(得分:-1)

您不必使用for循环从列表中生成字符串。

看这里

 protected function getColumns()
    {
        return [

            [ 'data' => 'phyto_number', 'name' => 'phytos.phyto_number', 'title' => 'Phyto Number' ],
            [ 'data' => 'product_name', 'name' => 'products.product_name', 'title' => 'Product Name' ],
            [ 'data' => 'weight', 'name' => 'phyto_product.weight', 'title' => 'Weight' ],
            [ 'data' => 'charge', 'name' => 'phyto_product.charge', 'title' => 'charge' ],

        ];
    }

输出

    <?php

    namespace App\Models;

    use Eloquent as Model;
    use Illuminate\Database\Eloquent\SoftDeletes;

    /**
     * Class Phyto
     * @package App\Models
     * @version December 27, 2019, 1:08 am UTC
     *
     * @property string phyto_number
     * @property integer destination_id
     * @property string indate
     */
    class Phyto extends Model
    {
        use SoftDeletes;

        public $table = 'phytos';

        const CREATED_AT = 'created_at';
        const UPDATED_AT = 'updated_at';


        protected $dates = ['deleted_at'];



        public $fillable = [
            'phyto_number',
            'destination_id',
           'indate'
        ];

        /**
         * The attributes that should be casted to native types.
         *
         * @var array
         */
        protected $casts = [
            'id' => 'integer',
            'phyto_number' => 'string',
            'destination_id' => 'integer',
            'indate' => 'date:d/m/y',
        ];

        /**
         * Validation rules
         *
         * @var array
         */
        public static $rules = [
            'phyto_number' => 'required',
            'indate' => 'required'
        ];


        public function products()
        {
            return $this->belongsToMany(Product::class)->withPivot(['weight','charge']);
        }
        public function destinations()
        {
            return $this->hasMany(Destination::class);
        }

    }

PhytoProduct Model

    <?php

    namespace App\Models;

    use Eloquent as Model;
    use Illuminate\Database\Eloquent\SoftDeletes;

    /**
     * Class PhytoProduct
     * @package App\Models
     * @version December 27, 2019, 2:20 am UTC
     *
     * @property integer phyto_id
     * @property integer product_id
     * @property number weight
     * @property number charge
     */
    class PhytoProduct extends Model
    {
        use SoftDeletes;

        public $table = 'phyto_product';

        const CREATED_AT = 'created_at';
        const UPDATED_AT = 'updated_at';


        protected $dates = ['deleted_at'];



        public $fillable = [
            'phyto_id',
            'product_id',
            'weight',
            'charge'
        ];

        /**
         * The attributes that should be casted to native types.
         *
         * @var array
         */
        protected $casts = [
            'id' => 'integer',
            'phyto_id' => 'integer',
            'product_id' => 'integer',
            'weight' => 'float',
            'charge' => 'float'
        ];

        /**
         * Validation rules
         *
         * @var array
         */
        public static $rules = [
            'phyto_id' => 'required',
            'product_id' => 'required'
        ];


    }

Product Model

    <?php

    namespace App\Models;

    use Eloquent as Model;
    use Illuminate\Database\Eloquent\SoftDeletes;

    /**
     * Class Product
     * @package App\Models
     * @version December 27, 2019, 1:07 am UTC
     *
     * @property string product_name
     */
    class Product extends Model
    {
        use SoftDeletes;

        public $table = 'products';

        const CREATED_AT = 'created_at';
        const UPDATED_AT = 'updated_at';


        protected $dates = ['deleted_at'];



        public $fillable = [
            'product_name'
        ];

        /**
         * The attributes that should be casted to native types.
         *
         * @var array
         */
        protected $casts = [
            'id' => 'integer',
            'product_name' => 'string'
        ];

        /**
         * Validation rules
         *
         * @var array
         */
        public static $rules = [
            'product_name' => 'required'
        ];

        public function phytos()
        {
            return $this->belongsToMany(Product::class)->withPivot(['weight','charge']);
        }
    }