【问题标题】:Python Loop to List Print Index or ArrayPython循环列出打印索引或数组
【发布时间】:2019-10-14 19:33:14
【问题描述】:

下午好, 蟒蛇新手在这里。我正在尝试返回从文本文件中抓取的客户信息列表。我需要输出采用名称、帐号、日期格式。我最初的想法是,

抓取数据 创建列表 按 index_number 打印,例如(姓名 1,帐号 1,日期 1)

很遗憾,这不起作用,因为列表将打印出所有姓名,然后是所有帐号,然后是日期。我需要将列表打印为姓名、帐号、日期。

我很确定这是因为我运行循环的方式。下面是我一直在处理的代码。

   import re

fin = open(destFileLoc,"r")
text = fin.read()


nameMatch = re.findall(r'\n\w+\s+\w+\s\w+', text)
# for i in range(len(nameMatch)):
#     name = nameMatch # print("Name: " + nameMatch[i])

acctMatch = re.findall(r'\s{4}\d{8}', text)
# for i in range(len(acctMatch)):
#     account = acctMatch  ##print("Account Num: " + acctMatch[i])

dateMatch = re.findall(r'(\d+/\d+/\d+)', text)
# for i in range(len(dateMatch)):
#     date = dateMatch  ## print("Date of Service : " + dateMatch[i])

patList = [[nameMatch], [acctMatch], [dateMatch]]
for i in range(patList):
    print("====== Name     Account Number       Date ======\n" + str(nameMatch[i]), str(acctMatch[i]), str(dateMatch[i]))

【问题讨论】:

  • “zip”功能可以帮助将列表重新组织成一个可以打印的元组列表。

标签: python arrays list loops


【解决方案1】:

您可以尝试使用 zip 组合多个列表

for name, acctNum, date in zip(nameMatch, acctMatch, dateMatch):
    print(str(name), str(acctNum), str(date))

【讨论】:

    【解决方案2】:

    试试...

    另见The format module

    nameMatch =["Smith", "Jones", "Thompson"]
    acctMatch =["12345", "54321", "22333"]
    dateMatch =["2019-10-1", "2019-10-2", "2019-10-3"]
    
    a = list(zip(
        nameMatch,
        acctMatch, 
        dateMatch
    ))
    
    print(a)
    
    for _list in a:
        print("=========================")
        print("Printing the list itself: ", _list)
        print ('Printing the items in _list:', _list[0], _list[1], _list[2])
    
    print("=========================")
    print ('Printing the items with format and columns:')
    for _list in a:
        print( '{0:.<10}{1:.<10}{2:.<10}'.format(_list[0], _list[1], _list[2]))
    

    输出:

    [('Smith', '12345', '2019-10-1'), ('Jones', '54321', '2019-10-2'), ('Thompson', '22333', '2019-10-3')]
    =========================
    Printing the list itself:  ('Smith', '12345', '2019-10-1')
    Printing the items in _list: Smith 12345 2019-10-1
    =========================
    Printing the list itself:  ('Jones', '54321', '2019-10-2')
    Printing the items in _list: Jones 54321 2019-10-2
    =========================
    Printing the list itself:  ('Thompson', '22333', '2019-10-3')
    Printing the items in _list: Thompson 22333 2019-10-3
    =========================
    Printing the items with format and columns:
    Smith.....12345.....2019-10-1.
    Jones.....54321.....2019-10-2.
    Thompson..22333.....2019-10-3.
    

    【讨论】:

    • 这看起来可行。我仍然需要验证数据,但感谢您的建议。另外,有没有办法返回(下一行)并在 print() (打印)时去掉单引号?所以看起来像 Name1 Acct1 Date1 Name2 Acct2 Date2 Name3 Acct3 Date3 等。再次感谢您的帮助!
    • 我相信我找到了每一行的新行。 print('\n'.join(map(str, patList))) 这将返回我想看到的内容,但是,我仍然需要去掉单引号。
    • 我认为您看到引号是因为您正在打印列表对象本身,而不是列表中的项目。我将更新我的答案以显示不同的打印选项。
    • 这看起来很棒!谢谢,感谢您的帮助!
    • 我的荣幸。如果有帮助,请不要忘记将其标记为答案,和/或添加赞成票 :) Stack Exchange 鼓励对任何有用的帖子进行此操作(即使它只是您搜索的帖子,而不是您提出的问题: ) )
    猜你喜欢
    • 2011-10-07
    • 1970-01-01
    • 2020-12-02
    • 1970-01-01
    • 2022-01-05
    • 2013-12-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多