Python:拆分和连接这些字符串的最快方法是什么?

时间:2014-07-19 23:21:55

标签: python split concatenation

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

ZETA = alpha。*

用户将提供如下输入:

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

alpha。*参数代表整数,其中有很多构建一个类函数来将输出分配给每个已知的整数值会很混乱。

稍后在我提供给ZETA的输入代码中,我需要看起来像这样的输出:

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

我想使用list,split和join方法来获取这些输出,但我遇到了错误:

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

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

我明白了:

TypeError: 'int' object is not iterable

我理解为什么......但它确实在我的计划中引起了纠结。想法?

谢谢!

ETA:

所以如果我要求输入在引号中,我想我可以到达我需要的位置:

' alpha.aaa_xxx'

而不是

alpha.aaa_xxx

有什么方法吗?

2 个答案:

答案 0 :(得分: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

我希望它有所帮助。

答案 1 :(得分:0)

感谢您的帮助!

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

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] ******************************