【问题标题】:Keys and values print separate in Python but want attached to string键和值在 Python 中单独打印,但要附加到字符串
【发布时间】:2019-02-07 02:50:09
【问题描述】:

所以我希望这段代码以不同的方式打印:

Python 代码:

employees = {123: 'Bob', 124: 'Susan', 128: 'Abby', 125: 'Henry', 126: 'Edward', 127: 'James'}

for key in employees.keys():
    print("Employees Numbers: ", key)

for value in employees.values():
    print("Employee Names: ", value)

打印为:

Employees Numbers:  123                      
Employees Numbers:  124             
Employees Numbers:  128                         
Employees Numbers:  125                
Employees Numbers:  126            
Employees Numbers:  127              
Employee Names:  Bob                
Employee Names:  Susan               
Employee Names:  Abby              
Employee Names:  Henry             
Employee Names:  Edward             
Employee Names:  James  

希望它打印为:

Employee Numbers: 123, 124, 128, 125, 126, 127                     
Employee Names: Bob, Susan, Abby, Henry, Edward            

【问题讨论】:

  • print(', '.join(employees))

标签: python python-3.x list dictionary printing


【解决方案1】:

您可以像这样遍历键:

print('Employee Numbers: {}'.format(', '.join(str(num) for num in employees.keys())))

然后类似的名称:

print('Employee Names: {}'.format(', '.join(name for name in employees.values())))

【讨论】:

  • 对于employees.values() 中的值:SyntaxError: invalid syntax it is pointing the ':' at end as to where error was occurred
  • 确保关闭括号。这段代码应该可以工作。
【解决方案2】:

您可以遍历字典并使用格式化字符串进行显示

>>> employees = {123: 'Bob', 124: 'Susan', 128: 'Abby', 125: 'Henry', 126: 'Edward', 127: 'James'}
>>> print('Employee Numbers: {}'.format(', '.join(str(key) for key in employees.keys())))
Employee Numbers: 128, 123, 124, 125, 126, 127
>>> print('Employee Names: {}'.format(', '.join(value for value in employees.values())))
Employee Names: Abby, Bob, Susan, Henry, Edward, James

【讨论】:

  • 你已经非常接近工作了......它每次循环 6 次,因此它打印员工编号:128、123、124、125、126、127 6 次,而不是在第一次之后停止时间
  • 但是您现在需要什么?
  • 我需要打印一次,但使用您的代码打印:员工编号:123、124、128、125、126、127 员工编号:123、124、128、125、126、127 员工编号:123、124、128、125、126、127 员工编号:123、124、128、125、126、127 员工姓名:Bob、Susan、Abby、Henry、Edward、James 员工姓名:Bob、Susan、Abby、 Henry、Edward、James 员工姓名:Bob、Susan、Abby、Henry、Edward、James 员工姓名:Bob、Susan、Abby、Henry、Edward、James
  • 它的打印结果是一样的......我正在从一个实际的 python 文件而不是 shell 运行数据,所以这可能是问题吗?
  • 所以,检查实际数据。如果你有像你的问题这样的字典,它会很好用
【解决方案3】:

首先将keysvalues 存储到单独的lists 中,然后使用join 运算符将它们填充到由comma 分隔的string 中。

对于员工编号,您需要执行一个额外步骤,将数字转换为strings(因为join 仅在string 上运行)

employees = {123: 'Bob', 124: 'Susan', 128: 'Abby', 125: 'Henry', 126: 'Edward', 127: 'James'}

numbers = list(employees.keys())
names = list(employees.values())

print("Employee Keys : ",', '.join([str(i) for i in numbers]))
print("Employee Names : ",', '.join(names))

【讨论】:

    【解决方案4】:
    #!/usr/bin/env python
    
    
    def main():
        employees = {123: 'Bob', 124: 'Susan', 128: 'Abby', 125: 'Henry', 126: 'Edward', 127: 'James'}
    
        print("employees ids:", ", ".join([str(x) for x in employees]))
        print("employees names:", ", ".join([str(x) for x in employees.values()]))
    
    
    if __name__ == '__main__':
        main()
    

    输出:

    employees ids: 123, 124, 128, 125, 126, 127
    employees names: Bob, Susan, Abby, Henry, Edward, James
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-10-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-02-13
      • 1970-01-01
      相关资源
      最近更新 更多