0

可能重复:
如何将字符串的元素放入具有某种行为的列表中

假设我有两个列表:

list1 = [[(1, 1), (1, 2), (1, 3), (1, 4)]]

list2 = ['A']

现在我需要类似的东西

dic1 = {'A': len(list1)} 

现在len(list1)应该是4因为我需要计算元组的数量,在这种情况下是 4。

4

4 回答 4

1

dic1 = {'A': len(list1[0])}

现在你得到的长度是list1, 但list1它是一个包含另一个列表的列表。使用它,您正在访问列表中的元组列表。

于 2012-07-31T15:38:45.127 回答
0

Adding some wild guessing on what you are actually trying to do:

>>> list1 = [[(1, 1), (1, 2), (1, 3), (1, 4)], [(2, 1), (2, 2)]]
>>> list2 = ['A', 'B']
>>> dic1 = {key: len(tuples) for (key, tuples) in zip(list2, list1)}
>>> dic1
{'A': 4, 'B': 2}
于 2012-07-31T15:46:18.883 回答
0

The problem is that list1 only has one item - another list. What you're trying to get is the length of the list in list1.

So use len(list1[0]) instead of len(list1) and you should be good.

于 2012-07-31T15:46:54.300 回答
0
dic1 = {list2[0]: len(list1[0])} 
于 2012-07-31T15:41:40.203 回答