【问题标题】:Python: What is the fastest way to split and join these strings?Python:拆分和连接这些字符串的最快方法是什么?
【发布时间】:2014-07-19 23:21:55
【问题描述】:

我有一个脚本,其中一个全局常量 ZETA 由用户输入

ZETA = 阿尔法。*

用户将提供如下输入:

alpha.aaa_xxx
alpha.bbb_yyy
alpha.abc_xyz
etc...

alpha.* 参数代表整数,而且它们数量众多,构建一个类函数来将输出分配给每个已知整数值会很麻烦。

稍后在我的代码中,从提供给 ZETA 的输入开始,我需要如下所示的输出:

beta.aaa, beta.xxx
beta.bbb, beta.yyy
beta.abc, beta.xyz

我想使用列表、拆分和连接方法来获取这些输出,但我遇到了错误:

当我尝试列出(ZETA)时,我希望:

a, l, p, h, a, ., a, a, a, _, x, x, x

我明白了:

TypeError: 'int' object is not iterable

我明白为什么……但这肯定会影响我的计划。想法?

谢谢!

预计到达时间:

所以我想如果我需要输入引号,我可以到达我需要的地方:

'alpha.aaa_xxx'

而不是

alpha.aaa_xxx

有什么办法可以解决吗?

【问题讨论】:

  • 发布代码对于获得修复它的帮助几乎是必不可少的。
  • 查看raw_input 和input 之间的区别。再一次,我们真的需要查看代码来提供帮助。
  • 听起来你正在使用input() 来请求这个值,它会立即忘记用户输入的变量的name,只存储result 输入的表达式。相反,首先使用raw_input() 获取名称,然后调用eval() 提取值(实际上,input() 几乎是eval(raw_input())。)

标签: python split concatenation


【解决方案1】:

好的,假设您将数据放在一个列表中。

sample = ['alpha.111_222', 'alpha.222_444', 'alpha.433_cvx'] # for example

您可以将文件读入列表,但是,生成器的美,您不必这样做,您可以轻松地自己构建一个生成器来读取文件。

据我了解,您想先剪掉 alpha 部分(由 . 分割)。 我们可以通过生成器来做到这一点

[y.split('.') for y in sample] # list of [['alpha', 'xxx_yyy'], ...]

现在我们要获取每个列表的第二个成员并用“_”分割它。与生成器的想法相同。

[x.split('_') for x in [y.split('.')[1] for y in sample]]

现在,当我们有 ['xxx', 'yyy'] 对的列表时,我们所需要的就是形成这样的新行

result = ["beta.%s, beta.%s" % (x[0],x[1]) for x in [x.split('_') for x in [y.split('.')[1] for y in sample]]] # ['beta.xxx, beta.yyy', ...]

或者,如果您不喜欢嵌套生成器,您可以编写如下代码:

k = [x.split('.')[1] for x in sample]
l = [x.split('_') for x in k]
result = ["beta.%s, beta.%s" % (x[0],x[1]) for x in l]

虽然看起来更清晰,但变量更多。现在,我们需要的只是打印。或者输出到文件。

for item in result:
    print item

希望对你有帮助。

【讨论】:

    【解决方案2】:

    感谢你们的帮助!

    我想我会分享我在沙盒环境中实施的最终解决方案。所有这一切都允许更改货币交易脚本中的多行代码,而只需对用户输入进行一次更改。我能够访问有关新货币对的大量数据,而无需在数百行代码中更改对新货币对的每个引用、投资组合余额、起始余额、日志输出等。

    PAIR        = 'btc_usd'  # requires format 'xxx_yyy'
    
    def instruments(pair):
    
        currency_code   = PAIR.split('_')[1]
        asset_code      = PAIR.split('_')[0]  
        instrument_pair = asset_code + '_' + currency_code
        instrument      = pairs[instrument_pair]
        currency        = portfolio[currencies[currency_code]]
        assets          = portfolio[currencies[asset_code]]
        currency_CODE   = (currency_code).upper()
        asset_CODE      = (asset_code).upper()
        if info.tick == 0:
            storage.initial_currency = currency
            storage.initial_assets = assets
    
        return (instrument_pair, instrument, currency_CODE, currency_code, currency, 
            storage.initial_currency, asset_CODE, asset_code, assets, storage.initial_assets)
    
    def tick():
    
        (instrument_pair, instrument, currency_CODE, currency_code, currency, storage.initial_currency, 
            asset_CODE, asset_code, assets, storage.initial_assets) = instruments(PAIR)
    
        if info.tick == 0:
            buy(instrument, 1)
        if info.tick == 1:
            log('******************************')        
            log('User Input......: %s' % PAIR)
            log('instrument pair.: %s' % instrument_pair)
            log('instrument......: %s' % instrument)
            log('CURRENCY CODE...: %s' % currency_CODE)        
            log('currency code...: %s' % currency_code)
            log('currency........: %.2f' % currency)
            log('initial currency: %.2f' % storage.initial_currency)
            log('ASSET CODE......: %s' % asset_CODE)          
            log('asset code......: %s' % asset_code) 
            log('assets..........: %.2f' % assets)
            log('initial assets..: %.2f' % storage.initial_assets)
            log('******************************')
    

    在我们的沙箱中产生:

    [2014-06-09 02:00:00]  BUY: 1.00 BTC (at 648.00 USD)
    [2014-06-09 02:15:00] ******************************
    [2014-06-09 02:15:00] User Input......: btc_usd
    [2014-06-09 02:15:00] instrument pair.: btc_usd
    [2014-06-09 02:15:00] instrument......: 2000
    [2014-06-09 02:15:00] CURRENCY CODE...: USD
    [2014-06-09 02:15:00] currency code...: usd
    [2014-06-09 02:15:00] currency........: 352.00
    [2014-06-09 02:15:00] initial currency: 1000.00
    [2014-06-09 02:15:00] ASSET CODE......: BTC
    [2014-06-09 02:15:00] asset code......: btc
    [2014-06-09 02:15:00] assets..........: 1.00
    [2014-06-09 02:15:00] initial assets..: 0.00
    [2014-06-09 02:15:00] ******************************
    

    我还得出的另一种解决方案是:

    PAIR        = 'Btc#@#$^@% _uSd'  # will also work with input 'BTCUSD'
    
    def instruments(pair):
    
        p = filter(str.isalpha, pair).lower()
        currency_code   = ''.join(list((p)[3:6]))
        asset_code      = ''.join(list((p)[0:3]))      
        instrument_pair = asset_code + '_' + currency_code
        instrument      = pairs[instrument_pair]
        currency        = portfolio[currencies[currency_code]]
        assets          = portfolio[currencies[asset_code]]
        currency_CODE   = (currency_code).upper()
        asset_CODE      = (asset_code).upper()
        if info.tick == 0:
            storage.initial_currency = currency
            storage.initial_assets = assets
    
        return (instrument_pair, instrument, currency_CODE, currency_code, currency, 
            storage.initial_currency, asset_CODE, asset_code, assets, storage.initial_assets)
    
    def tick():
    
        (instrument_pair, instrument, currency_CODE, currency_code, currency, storage.initial_currency, 
            asset_CODE, asset_code, assets, storage.initial_assets) = instruments(PAIR)
    
        if info.tick == 0:
            buy(instrument, 1)
        if info.tick == 1:
            log('******************************')        
            log('User Input......: %s' % PAIR)
            log('instrument pair.: %s' % instrument_pair)
            log('instrument......: %s' % instrument)
            log('CURRENCY CODE...: %s' % currency_CODE)        
            log('currency code...: %s' % currency_code)
            log('currency........: %.2f' % currency)
            log('initial currency: %.2f' % storage.initial_currency)
            log('ASSET CODE......: %s' % asset_CODE)          
            log('asset code......: %s' % asset_code) 
            log('assets..........: %.2f' % assets)
            log('initial assets..: %.2f' % storage.initial_assets)
            log('******************************')
    

    产生:

    [2014-06-09 02:00:00]  BUY: 1.00 BTC (at 648.00 USD)
    [2014-06-09 02:15:00] ******************************
    [2014-06-09 02:15:00] User Input......: Btc#@#$^@% _uSd
    [2014-06-09 02:15:00] instrument pair.: btc_usd
    [2014-06-09 02:15:00] instrument......: 2000
    [2014-06-09 02:15:00] CURRENCY CODE...: USD
    [2014-06-09 02:15:00] currency code...: usd
    [2014-06-09 02:15:00] currency........: 352.00
    [2014-06-09 02:15:00] initial currency: 1000.00
    [2014-06-09 02:15:00] ASSET CODE......: BTC
    [2014-06-09 02:15:00] asset code......: btc
    [2014-06-09 02:15:00] assets..........: 1.00
    [2014-06-09 02:15:00] initial assets..: 0.00
    [2014-06-09 02:15:00] ******************************
    

    【讨论】:

      猜你喜欢
      • 2014-03-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-11-30
      • 2013-01-20
      • 2012-05-24
      • 2010-11-21
      • 1970-01-01
      相关资源
      最近更新 更多