将列表中具有相同模式的元素组合为字符串

时间:2018-07-30 20:46:43

标签: python

我有一个字符串列表,这些字符串可能会有所不同。

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>


<div class="container">
  <div class="elem elem1">
   Elem 1
  </div>
   <div class="elem elem2">
   Elem 2
  </div>
  <div class="elem elem3">
   Elem 3
  </div>
</div>

转换后,它应该变成I18n.t("user.releated_links.url", id: xx)

仅当可以合并时,才应合并下一个元素。 (仅当它与模式lst = ['ban-eur.kd', 'ban-eur.sd', 'ban-eur.td' ] 相匹配时)

ban-eur<kd,sd,td>

这应该导致ban-eur

如果没有任何可以组合的元素,则应将其全部与lst = ['ban-eur.kd', 'kd', 'ban-eur.sd', 'ban-eur.td' ]

连接

如何做到这一点,而又不丢失数组中的第一个元素(与字符串重复)。

感谢您的时间。

3 个答案:

答案 0 :(得分:0)

您可以使用# calculations.py from models import Pres, Future def present_calc(): try: inputs = Pres.query.all() for item in inputs: cash = item.cash rate = item.rate periods = item.periods pv = cash / (1 + rate)**periods return dict(inputs=inputs, cash=cash, pv=pv) except: return dict() def future_calc(): inputs = Future.query.all() for item in inputs: cash = item.cash rate = item.rate periods = item.periods fv = cash * (1 + rate)**periods return dict(inputs=inputs, cash=cash, fv=fv) # views.py from flask import render_template, redirect, url_for, request from app import app, db from .calculations import present_calc, future_calc from app import db @app.route('/present', methods=['GET', 'POST']) def present(): inputs = Pres( cash=request.form['cash'], rate=request.form['rate'], periods=request.form['rate']) db.session.add(inputs) db.session.commit() calcs = present_calc() return render_template('present.html', **calcs) @app.route('/future', methods=['GET', 'POST']) def future(): inputs = Pres( cash=request.form['cash'], rate=request.form['rate'], periods=request.form['rate']) db.session.add(inputs) db.session.commit() calcs = future_calc() return render_template('future.html', **calcs)

itertools.groupby

输出:

import itertools, re
lst = ['ban-eur.kd', 'ban-eur.sd', 'ban-eur.td' ]
def group_result(d:list) -> list:
   if len(d) == 1:
     return d[0]
   new_result = [[a, list(b)] for a, b in itertools.groupby(sorted(d, key=lambda x:x.split('.')[0]), key=lambda x:x.split('.')[0])]
   return '_'.join('{}<{}>'.format(a, ','.join(i.split('.')[-1] for i in b)) for a, b in new_result)

new_data = '_'.join(group_result(list(b)) if a else '_'.join(list(b)) for a, b in itertools.groupby(lst, key=lambda x:'.' in x))

'ban-eur<kd,sd,td>' 上运行时:

['ban-eur.kd', 'kd', 'ban-eur.sd', 'ban-eur.td']

答案 1 :(得分:0)

这应该有效

pre = ''
result = []
endings = []
for item in lst:
    if item.split('.')[0] == pre:
        endings.append(item.split('.')[1])
    else:
        if endings:
            result.append(pre+'<'+','.join(endings)+'>')
        else:
            result.append(item)
        pre = item.split('.')[0]
        endings = []
print('_'.join(result))

答案 2 :(得分:0)

 matches = []
 resulting_string
 for item in lst:
     if item.startsWith('ban-eur'):
         matches.append(item)
     elif not item.startsWith('ban-eur') and len(matches) >= 1:
         if len(matches) == 1:
            resulting_string += item
         else:
            resulting_string += 'ban-eur.<'
            for s in matches:
                resulting_string += s + ', '
            resulting_string += '>'
            matches = []
     resulting_string += '_' + item + '_'