将列表转换为字典问题 [英] Convert list to dictionary problem
本文介绍了将列表转换为字典问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在文本文件中有以下内容:
1,a
1 ,b
1,b
2,a
2,c
2,
2,c
等....
我有以下代码打开文本文件,从里面的数据创建一个列表。我正在尝试创建一个字典,如:
{[1:a],[1:b],[1:b],[2:a],[2:c ],[2:a],[2:c]}
我使用以下内容:
Hi,
I have the following in a text file:
1,a
1,b
1,b
2,a
2,c
2,a
2,c
etc....
I have the following code to open the text file create a list from the data inside. I am trying to create a dictionary like:
{[1:a], [1:b], [1:b], [2:a], [2:c], [2:a], [2:c]}
I am using the following:
展开 | 选择 | Wrap | 行号
推荐答案
不推荐使用string.split。
使用< string> .split()代替。
例如
string.split is deprecated.
use <string>.split() instead.
eg
展开 | 选择 | 换行 | 行号
谢谢 - 如果它们需要是唯一的,我如何导入以便保留唯一键但是分配多个相关值以便我得到:
{[1 :a,b],[2:a,c]}
再次感谢..
Thanks - if they need to be unique how do i import so that I keep the unique key but assign the multiple associated values so that I get:
{[1:a,b], [2:a,c]}
thanks again..
我的朋友ghostdog74是正确的。鉴于这些数据,你最终得到一个非常小的字典:
My friend ghostdog74 is correct. Given that data, you''d end up with a very small dictionary:
展开 | 选择 | Wrap | 行号
这篇关于将列表转换为字典问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文