python-3.x - 在一行python3中搜索追加和打印
问题描述
我有这样的数据:
ice_cream 0.00013286998380339406
hot_chocolate 0.0002134821993051205
ice_cream -7.833574001019025e-05
hot_chocolate -0.0001492061779651939
我想像这样打印出来:
ice_cream 0.00013286998380339406 -7.833574001019025e-05
hot_chocolate 0.0002134821993051205 -0.0001492061779651939
我尝试了一些东西,但不能正常工作。我怎样才能在python3中做到这一点?
解决方案
最好的方法可能是为每个变量创建一个列表并将值附加到它。
# Creating the lists
ice_cream =[]
hot_chocolate = []
# Appending the values to the lists
# In practise you would use a loop here, that reads your source file and
# then appends the value to the respective list
ice_cream.append(value1)
ice_cream.append(value2)
hot_chocolate.append(value3)
hot_chocolate.append(value4)
# printing out the values
print("ice_cream",ice_cream)
print("hot_chocolate",hot_chocolate)
它应该返回如下内容:
ice_cream [0.00013286998380339406, -7.833574001019025e-05]
hot_chocolate [0.0002134821993051205, -0.0001492061779651939]
如果输出没有方括号和逗号非常重要,您可以将列表转换为字符串,然后使用其他方法将其删除。
print("hot_chocolate", " ".join(hot_chocolate))
>>> hot_chocolate 0.0002134821993051205 -0.0001492061779651939
推荐阅读
- excel - 对文件夹中的所有文件运行代码,仅适用于一个
- node.js - 使用 socket.io 进行实时聊天时如何解决“代理错误:无法将请求从 localhost:3000 代理到 http://localhost:5000/”
- python - 获取模板找到的对象的坐标
- sqlite - 颤动的sqllite不工作
- arrays - Swift:递归循环和展平数组
- asp.net-mvc - Web 请求转发到另一个域
- node.js - GraphQL 和从 Oracle DB 中提取数据:查询以结果集响应并且不进入 Graphql 解析器窗口
- apache-nifi - Nifi 中的 JsonQueryElasticSearch 处理器
- javascript - 如何在 Cesium.js 中创建始终靠近折线的标签?
- java - Java中巨大的Json String unescape的最佳方法是什么