как соединить элемент с другим в том же списке на основе условия

На PHP7 вы можете использовать DivisionByZeroError

try {
    echo 1/0;
}
catch(DivisionByZeroError $e){
    echo "got $e";
}

-2
задан glibdud 16 January 2019 в 18:38
поделиться

2 ответа

Вы можете перебрать a, сохранить слово, если оно не начинается с '#', или заменить '#' сохраненным словом, если оно произойдет: :

['when', 'when i am here', 'when go and get it', 'when life is hell', 'who', 'who i am here', 'who go and get it']
0
ответ дан blhsing 16 January 2019 в 18:38
поделиться

Здесь вы идете:

def carry_concat(string_list):
    replacement = ""  # current replacement string ("when" or "who" or whatever)
    replaced_list = []  # the new list
    for value in string_list:
        if value[0] == "#":
            # add string with replacement
            replaced_list.append(replacement + " " + value[1:])
        else:
            # set this string as the future replacement value
            replacement = value
            # add string without replacement
            replaced_list.append(value)
    return replaced_list

a = ['when', '#i am here','#go and get it', '#life is hell', 'who', '#i am here','#go and get it',]

print(a)
print(carry_concat(a))

Это печатает:

['when', '#i am here', '#go and get it', '#life is hell', 'who', '#i am here', '#go and get it']
['when', 'when i am here', 'when go and get it', 'when life is hell', 'who', 'who i am here', 'who go and get it']
0
ответ дан karl 16 January 2019 в 18:38
поделиться
Другие вопросы по тегам:

Похожие вопросы: