Python 我有一些清单。我需要用它编一本字典。下面的例子

Python 我有一些清单。我需要用它编一本字典。下面的例子,python,Python,这是我的列表: list1=[['in', 'comparison', 'to', 'dogs', ',', 'cats', 'have', 'not', 'undergone', 'major', 'changes', 'during', 'the', 'domestication', 'process', '.'], ['as', 'cat', 'simply', 'catenates', 'streams', 'of', 'bytes', ',', 'it', 'can', 'be', '

这是我的列表:

list1=[['in', 'comparison', 'to', 'dogs', ',', 'cats', 'have', 'not', 'undergone', 'major', 'changes', 'during', 'the', 'domestication', 'process', '.'], ['as', 'cat', 'simply', 'catenates', 'streams', 'of', 'bytes', ',', 'it', 'can', 'be', 'also', 'used', 'to', 'concatenate', 'binary', 'files', ',', 'where', 'it', 'will', 'just', 'concatenate', 'sequence', 'of', 'bytes', '.'], ['a', 'common', 'interactive', 'use', 'of', 'cat', 'for', 'a', 'single', 'file', 'is', 'to', 'output', 'the', 'content', 'of', 'a', 'file', 'to', 'standard', 'output', '.'], ['cats', 'can', 'hear', 'sounds', 'too', 'faint', 'or', 'too', 'high', 'in', 'frequency', 'for', 'human', 'ears', ',', 'such', 'as', 'those', 'made', 'by', 'mice', 'and', 'other', 'small', 'animals', '.']] 
dict1={in: 0,
comparison: 1,
to: 2,
dogs: 3,
...
}
{'in': 0, 'comparison': 1, 'to': 2, 'dogs': 3, ',': 4, 'cats': 5, 'have': 6, 'not': 7, 'undergone': 8, 'major': 9, 'changes': 10, 'during': 11, 'the': 12, 'domestication': 13, 'process': 14, '.': 15} {'as': 0, 'cat': 1, 'simply': 2, 'catenates': 3, 'streams': 4, 'of': 24, 'bytes': 25, ',': 17, 'it': 19, 'can': 9, 'be': 10, 'also': 11, 'used': 12, 'to': 13, 'concatenate': 22, 'binary': 15, 'files': 16, 'where': 18, 'will': 20, 'just': 21, 'sequence': 23, '.': 26} {'a': 16, 'common': 1, 'interactive': 2, 'use': 3, 'of': 15, 'cat': 5, 'for': 6, 'single': 8, 'file': 17, 'is': 10, 'to': 18, 'output': 20, 'the': 13, 'content': 14, 'standard': 19, '.': 21} {'cats': 0, 'can': 1, 'hear': 2, 'sounds': 3, 'too': 7, 'faint': 5, 'or': 6, 'high': 8, 'in': 9, 'frequency': 10, 'for': 11, 'human': 12, 'ears': 13, ',': 14, 'such': 15, 'as': 16, 'those': 17, 'made': 18, 'by': 19, 'mice': 20, 'and': 21, 'other': 22, 'small': 23, 'animals': 24, '.': 25}
字典:

list1=[['in', 'comparison', 'to', 'dogs', ',', 'cats', 'have', 'not', 'undergone', 'major', 'changes', 'during', 'the', 'domestication', 'process', '.'], ['as', 'cat', 'simply', 'catenates', 'streams', 'of', 'bytes', ',', 'it', 'can', 'be', 'also', 'used', 'to', 'concatenate', 'binary', 'files', ',', 'where', 'it', 'will', 'just', 'concatenate', 'sequence', 'of', 'bytes', '.'], ['a', 'common', 'interactive', 'use', 'of', 'cat', 'for', 'a', 'single', 'file', 'is', 'to', 'output', 'the', 'content', 'of', 'a', 'file', 'to', 'standard', 'output', '.'], ['cats', 'can', 'hear', 'sounds', 'too', 'faint', 'or', 'too', 'high', 'in', 'frequency', 'for', 'human', 'ears', ',', 'such', 'as', 'those', 'made', 'by', 'mice', 'and', 'other', 'small', 'animals', '.']] 
dict1={in: 0,
comparison: 1,
to: 2,
dogs: 3,
...
}
{'in': 0, 'comparison': 1, 'to': 2, 'dogs': 3, ',': 4, 'cats': 5, 'have': 6, 'not': 7, 'undergone': 8, 'major': 9, 'changes': 10, 'during': 11, 'the': 12, 'domestication': 13, 'process': 14, '.': 15} {'as': 0, 'cat': 1, 'simply': 2, 'catenates': 3, 'streams': 4, 'of': 24, 'bytes': 25, ',': 17, 'it': 19, 'can': 9, 'be': 10, 'also': 11, 'used': 12, 'to': 13, 'concatenate': 22, 'binary': 15, 'files': 16, 'where': 18, 'will': 20, 'just': 21, 'sequence': 23, '.': 26} {'a': 16, 'common': 1, 'interactive': 2, 'use': 3, 'of': 15, 'cat': 5, 'for': 6, 'single': 8, 'file': 17, 'is': 10, 'to': 18, 'output': 20, 'the': 13, 'content': 14, 'standard': 19, '.': 21} {'cats': 0, 'can': 1, 'hear': 2, 'sounds': 3, 'too': 7, 'faint': 5, 'or': 6, 'high': 8, 'in': 9, 'frequency': 10, 'for': 11, 'human': 12, 'ears': 13, ',': 14, 'such': 15, 'as': 16, 'those': 17, 'made': 18, 'by': 19, 'mice': 20, 'and': 21, 'other': 22, 'small': 23, 'animals': 24, '.': 25}

您可以这样尝试:

list1=[['in', 'comparison', 'to', 'dogs', ',', 'cats', 'have', 'not', 'undergone', 'major', 'changes', 'during', 'the', 'domestication', 'process', '.'], ['as', 'cat', 'simply', 'catenates', 'streams', 'of', 'bytes', ',', 'it', 'can', 'be', 'also', 'used', 'to', 'concatenate', 'binary', 'files', ',', 'where', 'it', 'will', 'just', 'concatenate', 'sequence', 'of', 'bytes', '.'], ['a', 'common', 'interactive', 'use', 'of', 'cat', 'for', 'a', 'single', 'file', 'is', 'to', 'output', 'the', 'content', 'of', 'a', 'file', 'to', 'standard', 'output', '.'], ['cats', 'can', 'hear', 'sounds', 'too', 'faint', 'or', 'too', 'high', 'in', 'frequency', 'for', 'human', 'ears', ',', 'such', 'as', 'those', 'made', 'by', 'mice', 'and', 'other', 'small', 'animals', '.']]


dict = {v:i for l in list1 for i,v in enumerate(l)}
print(dict)
输出:

list1=[['in', 'comparison', 'to', 'dogs', ',', 'cats', 'have', 'not', 'undergone', 'major', 'changes', 'during', 'the', 'domestication', 'process', '.'], ['as', 'cat', 'simply', 'catenates', 'streams', 'of', 'bytes', ',', 'it', 'can', 'be', 'also', 'used', 'to', 'concatenate', 'binary', 'files', ',', 'where', 'it', 'will', 'just', 'concatenate', 'sequence', 'of', 'bytes', '.'], ['a', 'common', 'interactive', 'use', 'of', 'cat', 'for', 'a', 'single', 'file', 'is', 'to', 'output', 'the', 'content', 'of', 'a', 'file', 'to', 'standard', 'output', '.'], ['cats', 'can', 'hear', 'sounds', 'too', 'faint', 'or', 'too', 'high', 'in', 'frequency', 'for', 'human', 'ears', ',', 'such', 'as', 'those', 'made', 'by', 'mice', 'and', 'other', 'small', 'animals', '.']] 
dict1={in: 0,
comparison: 1,
to: 2,
dogs: 3,
...
}
{'in': 0, 'comparison': 1, 'to': 2, 'dogs': 3, ',': 4, 'cats': 5, 'have': 6, 'not': 7, 'undergone': 8, 'major': 9, 'changes': 10, 'during': 11, 'the': 12, 'domestication': 13, 'process': 14, '.': 15} {'as': 0, 'cat': 1, 'simply': 2, 'catenates': 3, 'streams': 4, 'of': 24, 'bytes': 25, ',': 17, 'it': 19, 'can': 9, 'be': 10, 'also': 11, 'used': 12, 'to': 13, 'concatenate': 22, 'binary': 15, 'files': 16, 'where': 18, 'will': 20, 'just': 21, 'sequence': 23, '.': 26} {'a': 16, 'common': 1, 'interactive': 2, 'use': 3, 'of': 15, 'cat': 5, 'for': 6, 'single': 8, 'file': 17, 'is': 10, 'to': 18, 'output': 20, 'the': 13, 'content': 14, 'standard': 19, '.': 21} {'cats': 0, 'can': 1, 'hear': 2, 'sounds': 3, 'too': 7, 'faint': 5, 'or': 6, 'high': 8, 'in': 9, 'frequency': 10, 'for': 11, 'human': 12, 'ears': 13, ',': 14, 'such': 15, 'as': 16, 'those': 17, 'made': 18, 'by': 19, 'mice': 20, 'and': 21, 'other': 22, 'small': 23, 'animals': 24, '.': 25}

示例不是规范。例子不是“例子”。一个局部示例(甚至没有通过第一个子列表,让我们猜测您想要从其余子列表中得到什么)算不上什么示例。您需要计算列表中输入的出现次数?@ChristianKönig:
计数器
有什么帮助?dict1似乎只包含索引。@aramaki:no。请参见
0
。基于@Eric Duminil的回答:
{v:i代表列表1中的l代表i,v代表枚举(l)}
或:
[{v:i代表i,v代表枚举(l)}代表列表1中的l]
@AndrewKonstantinov欢迎您的光临:事实上,这不能用口述来解决,因为有些单词出现不止一次。元组列表最接近OP的要求:
[(v,i)表示列表1中的l表示枚举(l)中的i,v)]