Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/amazon-s3/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python:分割和连接这些字符串的最快方法是什么?_Python_Split_Concatenation - Fatal编程技术网

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

Python:分割和连接这些字符串的最快方法是什么?,python,split,concatenation,Python,Split,Concatenation,我有一个脚本,其中一个全局常量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 k = [x.split('.')[

我有一个脚本,其中一个全局常量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
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]
我想使用list、split和join方法来获得这些输出,但我遇到了一个错误:

当我尝试列出泽塔时,我希望:

a, l, p, h, a, ., a, a, a, _, x, x, x
我得到:

TypeError: 'int' object is not iterable
我明白为什么。。。但这确实给我的计划带来了麻烦。想法

谢谢

预计到达时间:

因此,如果我要求输入用引号括起来,我想我可以达到我需要的位置:

“阿尔法aaa_xxx”

而不是

alpha.aaa_xxx


有什么办法吗?

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

sample = ['alpha.111_222', 'alpha.222_444', 'alpha.433_cvx'] # for example
您可以将文件读入列表,但,生成器的优点是,您不必这样做,您可以轻松地为读取文件构建一个生成器

据我所知,你想剪下阿尔法部分,按。第一 我们可以用发电机来做这件事

[y.split('.') for y in sample] # list of [['alpha', 'xxx_yyy'], ...]
现在,我们希望获得每个列表的第二个成员,并将其按“\u1”拆分。发电机也是这样

[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', ...]
或者,如果您不喜欢嵌套生成器,可以编写如下代码:

beta.aaa, beta.xxx
beta.bbb, beta.yyy
beta.abc, beta.xyz
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

希望能有帮助。

谢谢你们的帮助

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

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

发布代码对于获得修复帮助几乎是必不可少的。查看原始输入和输入之间的差异。再一次,我们真的需要查看代码以提供帮助。听起来您使用输入来请求此值,这会立即忘记用户键入的变量的名称,并且只存储输入表达式的结果。相反,首先使用raw_输入获取名称,然后调用eval来提取值事实上,输入几乎是evalraw_输入。