小编Kee*_*nan的帖子

Python:TypeError:不可用类型:'list'

我正试图拿一个看起来像这样的文件

AAA x 111
AAB x 111
AAA x 112
AAC x 123
...
Run Code Online (Sandbox Code Playgroud)

并使用字典,以便输出看起来像这样

{AAA: ['111', '112'], AAB: ['111'], AAC: [123], ...}
Run Code Online (Sandbox Code Playgroud)

这就是我尝试过的

file = open("filename.txt", "r") 
readline = file.readline().rstrip()
while readline!= "":
    list = []
    list = readline.split(" ")
    j = list.index("x")
    k = list[0:j]
    v = list[j + 1:]
    d = {}
    if k not in d == False:
        d[k] = []
    d[k].append(v)
    readline = file.readline().rstrip()
Run Code Online (Sandbox Code Playgroud)

我一直在接受TypeError: unhashable type: 'list'.我知道字典中的键不能是列表,但我试图将我的值变成列表而不是键.我想知道我是否在某处犯了错误.

python dictionary list typeerror

76
推荐指数
4
解决办法
40万
查看次数

Python:列表中出现次数最多的值

我有两个列表如下

x = ['a','a','b','c','b','a']
Run Code Online (Sandbox Code Playgroud)

x = ['a','a','b','c','c','d']
Run Code Online (Sandbox Code Playgroud)

我试图找出每个列表中出现最多的值.这就是我尝试过的.

def unique_values(output,input):
    for i in input:
        if i not in output:
            output.append(i)
k = []
for i in k:
    unique_values(k,x)
    y.remove(i)
Run Code Online (Sandbox Code Playgroud)

我已经做到这一点,但我无法弄清楚如何for i in k:在它删除列表中的所有值之前停止.

python list

12
推荐指数
1
解决办法
3万
查看次数

Python:在列表中搜索int

说我有这个清单

x = [1,2,3,1,5,1,8]
Run Code Online (Sandbox Code Playgroud)

有没有办法找到1列表中的每个索引?

python search list

3
推荐指数
1
解决办法
125
查看次数

标签 统计

list ×3

python ×3

dictionary ×1

search ×1

typeerror ×1