如果我有任何字符串,例如:
“最早的alpha gma beta”或“ alpha”
我要:
$data = [
'name' => (string)$row['name'],
'address' => (string)$row['address'],
'suite' => (string)$row['suite'],
'city' => (string)$row['city'],
'zipcode' => (string)$row['zipcode'],
'country_code' => (string)$row['country_code'],
'province_code' => (string)$row['province_code'],
'region_id' => $region_id,
'phone' => (string)$row['phone'],
'fax' => (string)$row['fax'],
'website' => (string)$row['website'],
'linkedin' => (string)$row['linkedin'],
'facebook' => (string)$row['facebook'],
'twitter' => (string)$row['twitter'],
'number_employees' => (string)$row['number_employees'],
'year_creation' => (string)$row['year_creation'],
'turnover_id' => $turnover_id,
'status' => 1,
'hidden_directory' => 0,
'hidden_directory_byadmin' => 0,
'is_member' => (bool)$row['is_member'],
'created_by' => 1,
];
$company = Company::create($data);
替换空格between
不,我正在使用:
||
问题在于,如果仅是一个关键字,则不会添加*。
我知道我可以写一个term_str = " ".join(keyword.strip().split()).replace(' ', '* ||')
和一个for loop
,但是我正在寻找更多的python单行表达式。
答案 0 :(得分:2)
我们可以拆分字符串,在每个单词之后添加*
,然后将经过修饰的单词与||
结合在一起:
def func(keywords):
return '||'.join(map('{}*'.format, keywords.split()))
print(func('arest alpha gma beta')) # arest*||alpha*||gma*||beta*
print(func('alpha ')) # alpha*