我尝试了很多次却无法做到。
以下是一个例子:
print( concat_corresponding( "12345", "6789XYZ" ) )
期望的输出:
162738495XYZ
答案 0 :(得分:3)
这是itertools
的一种方式:
from itertools import chain, zip_longest
a = "12345"
b = "6789XYZ"
res = ''.join(list(chain.from_iterable(zip_longest(a, b, fillvalue=''))))
# '162738495XYZ'
请注意,列表转换不是必需的,而是improves performance。
答案 1 :(得分:2)
你可以采用列表推导,加入和压缩:
te1 = "12345"
te2 = "6789XYZ"
print (''.join( ''.join(x) for x in zip(te1,te2)) + (te1[len(te2):] if len(te1)>len(te2) else te2[len(te1):]))
# ^^^^ this part adds the remainer of longer list to result
输出:
162738495XYZ
https://docs.python.org/3/library/functions.html#zip
https://docs.python.org/3/library/stdtypes.html#str.join
说明:
zip按位置对字符进行配对,其余按钮将对列表重新组合成字符串。
Zip仅适用于较短长度的字符串 - 您可以切换到itertools.zip_longest(see JimDennis answer)或附加更长的列表部分(就像我在这里做的那样)
答案 2 :(得分:2)
from itertools import izip_longest
''.join(['%s%s' % (x ,y)\
for x,y in izip_longest("12345","6789XYZ", fillvalue='')])
## Was: ''.join(['%s%s' % (x if x else '',y if y else '') \
## for x,y in izip_longest("12345","6789XYZ")])
稍微打破一下:
答案 3 :(得分:1)
我认为此解决方案更加清晰,并利用itertools.zip_longest
的mydict.get(key)
关键字参数。
@Service("intentExportImportService")
public class IntentExportImportServiceImpl implements IntentExportImportService {
@Resource(name = "intentExportImportService")
private IntentExportImportService intentExportImportService;
public Map<String, Integer> importIntents(ExportImportData exportImportData,boolean overwrite) throws DataAccessLayerException {
Map<String, Integer> statiscisMap= createOrUpdateIntent(exportImportData,overwrite);
return statiscisMap;
}
private Map<String, Integer> createOrUpdateIntent(ExportImportData exportImportData,boolean overwrite)throws DataAccessLayerException {
List<Intent> intentsList = exportImportData.getIntents();
Map<String,Entity> entityMap = getEntityMap(exportImportData.getEntityList());
Map<String,Api> apiMap = getApiMap(exportImportData.getApiList());
Map<String,Integer> statisticsMap = new HashMap<>();
Long domainId = ExcelUtil.getDomainId(exportImportData.getDomainName());
for(Intent intent : intentsList) {
Intent existingIntent = intentExists(intent.getIntentNm());
if(existingIntent != null){
startUpdateIntent(intent,existingIntent,entityMap,apiMap,overwrite,statisticsMap,domainId);
}else{
startCreateIntent(intent,entityMap,apiMap,overwrite,statisticsMap,domainId);
}
}
return statisticsMap;
}
@Transactional
public void startUpdateIntent(Intent intent, Intent existingIntent, Map<String, Entity> entityMap, Map<String, Api> apiMap, boolean overwrite, Map<String, Integer> statisticsMap, Long domainId) {
try {
intentExportImportService.updateIntent(intent, existingIntent, entityMap, apiMap, overwrite, statisticsMap,domainId);
}catch (Exception e)
{
updateStatisticsMap(FAILED,statisticsMap);
LOGGER.error("Error Importing Intents to update and hence rolling back intent: "+intent.getIntentNm());
}
}
@Transactional(value = "dataTxManager", propagation = Propagation.REQUIRES_NEW, isolation = Isolation.READ_COMMITTED, rollbackFor = {
DuplicateException.class, DataAccessException.class,DataAccessLayerException.class, SQLTimeoutException.class, SQLException.class,Exception.class})
public void updateIntent(Intent intent, Intent existingIntent, Map<String, Entity> entityMap, Map<String, Api> apiMap, boolean overwrite, Map<String, Integer> statisticsMap,Long domainId) throws DataAccessLayerException {
if(!overwrite){
LOGGER.info("Not overwriting the Intent: "+intent.getIntentNm()+" as it already exist and overwrite is false");
throw new DataAccessLayerException(CommonConstants.IMPORT_FAILURE_ERROR_CODE,"rolling back intent importing: "+intent.getIntentNm());
}
manageEntitiesAndApis(intent, entityMap, apiMap, overwrite,domainId);
Long intentId = updateImportedIntent(intent,existingIntent);
if(intentId != null) {
updateStatisticsMap(UPDATED, statisticsMap);
}
}
答案 4 :(得分:0)
这是基本但有用的
str1="12345"
str2="6789XYZ"
str3=""
i=0
for i, ch in enumerate(zip(str1, str2)):
str3 += ch[0] + ch[1]
if len(str1) < len(str2):
str3 += str2[i+1:]
else:
str3 += str1[i+1:]
print(str3)
答案 5 :(得分:0)
另一种方法是:
$(document).ready(function () {
$(document).on('click', '#get-my-data-icon', function (e) {
e.preventDefault();
var uid = $(this).data('id'); //here your get the id
$('.modal-body').html(''); //empty the html of the modal body
$.ajax({
url: "../imCallingThisFunctionViaAjaxToGetMyDataInTheModal",
type: 'POST',
data: {id: uid}
})
.done(function (data) {
console.log(data); //if u want...
$('.modal-body').html("<div>"+data+"</div>");
//print the result in the modal-body
})
.fail(function () {
//in case it fails
$('.modal-body').html('<i class="fa fa-warning"></i> Try again!');
});
});
});
输出:
def concat_corresponding(string1, string2):
minimum, maximum = sorted([string1, string2], key=len)
return "".join(string1[i]+string2[i] for i in range(len(minimum))) + maximum[len(minimum):]
print(concat_corresponding( "12345", "6789XYZ" ))