如何组合两个包含间隔的列表
我有列表 1 和 2,我想获得列表 3。如果有人可以建议 python 或 awk 脚本,那就太好了。
List 1 A 100-160 B 200-500 C 800-1500 D 1600-2000 E 2500-3000 List 2 150 600 900 1700 2400 List 3 A 100-160 150 B 200-500 C 800-1500 900 D 1600-2000 1700 E 2500-3000
I have list 1 and 2 and I would like to get list 3. If anyone can suggest python or awk script, that would be great.
List 1 A 100-160 B 200-500 C 800-1500 D 1600-2000 E 2500-3000 List 2 150 600 900 1700 2400 List 3 A 100-160 150 B 200-500 C 800-1500 900 D 1600-2000 1700 E 2500-3000
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
这是一个例子。它期望在命令行上传递两个文件名。
Here is an example. It is expecting two filenames to be passed on the command line.
您可以从列表 1 中创建一个包含间隔的字典,然后循环遍历它以查看列表 2 中是否有任何值在该范围内。例如,
You could make a dictionary from list 1 that contains the intervals then loops through it to see if any value in list 2 is inside the range. For example,
你可以在Python中做这样的事情:
如果你想要的话,很容易将数据解析成该结构(并将其打印回来),但我会等到你解释数据的格式以及你需要的格式把它放进去,因为我有预感,会有一个陷阱。
You could do something like this in Python:
It's easy to parse the data into that structure if that's what you want (and print it back out), but I'll wait until you explain what format the data comes in and what format you need it in, because I have a feeling there will be a catch to it.
只需在命令行上执行即可。到目前为止,这是唯一的 awk 解决方案:
Do it simply on the command line.So far this is the only awk solution among all:
不确定是否需要 Ruby,但这里有一个快速介绍:
Dunno for sure if Ruby is needed, but here's a quick pass at it:
您可以使用可迭代工具来编写此代码。不是太可读,但这是有效的。写起来绝对很有趣!
You can use iterable tools to write this. Not all too readable but this works. Definitely was fun to write!
这是我的看法。由于您没有指定,我只是弥补了解析和打印位。
Here's my take. I just made up the parsing and printing bit since you didn't specify.