从少于4位数的列表中提取数字,并在每个数字的开头和结尾加上一个字符串

2024-04-19 23:48:45 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个包含几个数字的文件。你知道吗

如果数字长度小于4位,我们需要提取它并在开头加上0,+后缀,然后再加上主列表。你知道吗

DF=[1,23,333,4444]

应该是

DF=[0001.hk,0023.hk,0333.hk,4444.hk]

下面的代码工作,并帮助我完成上述任务。你知道吗

Master_List = [Here is where all tickers should be store for some further processing]

def prework1():
    file = 'Path/to/document'
    tickers = []
    read = pd.read_csv(file, names =['IB_Symbol', 'Description', 'Symbol', 
    'Currency'])
    tickers = read['Symbol'].tolist()

    ticker_list = []

    for ticker in tickers:
        if len(ticker) == 1:
            ticker_list.append(ticker)

    ticker_list1 = []

    for ticker in ticker_list:
        string = '000'
        string1 = '.hk'
        tickers1 = [string + ticker + string1]
        ticker_list1.append(tickers1)

    ticker_list2 = []

    for sublist in ticker_list1:
        for item in sublist:
            ticker_list2.append(item)

    return ticker_list2



def prework2():
    file = 'Path/to/document'
    tickers = []
    read = pd.read_csv(file, names =['IB_Symbol', 'Description', 'Symbol', 'Currency'])
    tickers = read['Symbol'].tolist()

ticker_list = []

    for ticker in tickers:
        if len(ticker) == 2:
            ticker_list.append(ticker)

    ticker_list1 = []

    for ticker in ticker_list:
        string = '00'
        string1 = '.hk'
        tickers1 = [string + ticker + string1]
        ticker_list1.append(tickers1)

    ticker_list3 = []

    for sublist in ticker_list1:
        for item in sublist:
            ticker_list3.append(item)

    return ticker_list3


def prework3():
    file = 'Path/to/document'
    tickers = []
    read = pd.read_csv(file, names =['IB_Symbol', 'Description', 'Symbol', 
    'Currency'])
    tickers = read['Symbol'].tolist()

    ticker_list = []

    for ticker in tickers:
        if len(ticker) == 3:
            ticker_list.append(ticker)

    ticker_list1 = []

    for ticker in ticker_list:
        string = '0'
        string1 = '.hk'
        tickers1 = [string + ticker + string1]
        ticker_list1.append(tickers1)

    ticker_list4 = []

    for sublist in ticker_list1:
        for item in sublist:
            ticker_list4.append(item)

    return ticker_list4



test1 = prework1()
test2 = prework2()
test3 = prework3()

print(test1)
print(test2)
print(test3)

上述方法有几个问题。你知道吗

使用上面的代码,它将给我3个列表,但结果应该只有1个列表,这样我可以做一些进一步的处理/任务。你知道吗

而且,我觉得这看起来很奇怪,很重复。它会做你想做的,但是有没有办法让它变得更好一点呢?你知道吗

感谢所有的帮助!!你知道吗


Tags: inforreadstringsymbollistfileticker
3条回答

您可以在list comprehension中使用zfill

DF = [1, 23, 333, 4444]


def fill(lst, end='.hk'):
    return [s.zfill(4) + end for s in map(str, lst)]


print(fill(DF))

输出

['0001.hk', '0023.hk', '0333.hk', '4444.hk']

上述列表理解相当于:

def fill(lst, end='.hk'):
    result = []
    for s in map(str, lst):
        result.append(s.zfill(4) + end)
    return result

从文档中,zfill

Return a copy of the string left filled with ASCII '0' digits to make a string of length width.

因此,当代码调用s.zfill(4)时,它将在字符串的开头追加'0',直到字符串的长度为4。你知道吗

Python有一个zfill()函数,它将在字符串的前面添加零,直到字符串具有所需的长度(如果数字较少):

>>> '23'.zfill(4)
'0023'
>>> '1234'.zfill(4)
'1234'

所以你可以做:

>>> DF = [ 1, 23, 333, 4444]
>>> D = [ str(i).zfill(4) + '.hk' for i in DF ]
>>> D
['0001.hk', '0023.hk', '0333.hk', '4444.hk']

最简单的方法是:

>>> result = [f'{i:04}.hk' for i in DF]
>>> result
['0001.hk', '0023.hk', '0333.hk', '4444.hk']

阅读介绍格式字符串的PEP 498文档中的更多内容。你知道吗

相关问题 更多 >