Python 如何计算嵌套元组列表中字符串的出现次数?

Python 如何计算嵌套元组列表中字符串的出现次数?,python,tuples,Python,Tuples,我在python中有一个嵌套的元组列表: myList = [(12345, "John Doe"), (345678, 'Marie Doe'), (434566, 'Marie Doe'), (665533, 'Marie Doe'), (23456, 'John Doe'), (657332211, 'Amanda Doe')] 并尝试将名称作为两个单独的列表生成引用: occurrences = [2, 3, 1] names = ['John Doe', 'Mar

我在python中有一个嵌套的元组列表:

myList = [(12345, "John Doe"), (345678, 'Marie Doe'), (434566, 'Marie Doe'), (665533, 'Marie Doe'), (23456, 'John Doe'), (657332211, 'Amanda Doe')]
并尝试将名称作为两个单独的列表生成引用:

occurrences = [2, 3, 1]
names = ['John Doe', 'Marie Doe', 'Amanda Doe']
到目前为止,这一个给了我嵌套元组中的项目总数,但不确定如何生成上述内容:

Total = len(myList)

有人能帮我吗?提前谢谢你

您可以创建一个
dict
,其中键是名称,值是它们在
myList
中的计数

 myList = [(12345, "John Doe"), (345678, 'Marie Doe'), (434566, 'Marie Doe'), (665533, 'Marie Doe'), (23456, 'John Doe'), (657332211, 'Amanda Doe')]

# Initializing a dict with key: name and value: 0.
dict_name_count = {name: 0 for _, name in myList}

# Iterating over tuples in `myList`, picking name (the second entry in tuple) ignoring the number.
for _, name in myList:
    dict_name_count[name] += 1

print(dict_name_count)
# Results in: {'John Doe': 2, 'Marie Doe': 3, 'Amanda Doe': 1}

# To get separate lists:
names = list(dict_name_count.keys())
occurrences = list(dict_name_count.values())


您可以做的是创建一个
dict
,其中键是名称,值是它们在
myList
中的计数

 myList = [(12345, "John Doe"), (345678, 'Marie Doe'), (434566, 'Marie Doe'), (665533, 'Marie Doe'), (23456, 'John Doe'), (657332211, 'Amanda Doe')]

# Initializing a dict with key: name and value: 0.
dict_name_count = {name: 0 for _, name in myList}

# Iterating over tuples in `myList`, picking name (the second entry in tuple) ignoring the number.
for _, name in myList:
    dict_name_count[name] += 1

print(dict_name_count)
# Results in: {'John Doe': 2, 'Marie Doe': 3, 'Amanda Doe': 1}

# To get separate lists:
names = list(dict_name_count.keys())
occurrences = list(dict_name_count.values())


以下是我将使用熊猫做的事情:

将熊猫作为pd导入
df=pd.DataFrame(myList).groupby(1,as_index=False).count()
引用次数=df[0]。到_列表()
name=df[1]。发送至_列表()
产出:

names : ['Amanda Doe', 'Joe Doe', 'John Doe', 'Marie Doe']
occurrences : [1, 1, 1, 3]

以下是我将使用熊猫做的事情:

将熊猫作为pd导入
df=pd.DataFrame(myList).groupby(1,as_index=False).count()
引用次数=df[0]。到_列表()
name=df[1]。发送至_列表()
产出:

names : ['Amanda Doe', 'Joe Doe', 'John Doe', 'Marie Doe']
occurrences : [1, 1, 1, 3]

您可以将名称解压到列表中,然后只运行该列表的计数:

all_names = [elem[1] for elem in myList]
names = []
occurrencs = []
for name in set(all_names):
    names.append(name)
    occurrences.append(all_names.count(name))

您可以将名称解压到列表中,然后只运行该列表的计数:

all_names = [elem[1] for elem in myList]
names = []
occurrencs = []
for name in set(all_names):
    names.append(name)
    occurrences.append(all_names.count(name))
试试这个

    myList = [(12345, "John Doe"),
          (345678, 'Marie Doe'),
          (434566, 'Marie Doe'),
          (665533, 'Marie Doe'),
          (23456, 'John Doe'),
          (657332211, 'Amanda Doe')
          ]
namelist=[name[1] for name in myList]
names=[]
for name in namelist:
    if name not in names:
        names.append(name)

occurrences=[namelist.count(name) for name in names]
print(occurrences)
print(names)
试试这个

    myList = [(12345, "John Doe"),
          (345678, 'Marie Doe'),
          (434566, 'Marie Doe'),
          (665533, 'Marie Doe'),
          (23456, 'John Doe'),
          (657332211, 'Amanda Doe')
          ]
namelist=[name[1] for name in myList]
names=[]
for name in namelist:
    if name not in names:
        names.append(name)

occurrences=[namelist.count(name) for name in names]
print(occurrences)
print(names)

您的问题有点不清楚,为了澄清这一点,您希望迭代一个集合并维护两个列表,一个用于频率,一个用于实际字符串?是集合导入计数器中的“Joe Doe”的拼写错误吗;计数器(myList中x的x[1])@puffin,是的,很抱歉我上面的请求不清楚。“我正在找那个。”奥雷尔,我刚刚编辑过。这是我的错,对不起。现在它显示正确了。您的问题有点不清楚,为了澄清,您希望迭代一个集合并维护两个列表,一个用于频率,一个用于实际字符串?是集合导入计数器中的“Joe Doe”的拼写错误吗;计数器(myList中x的x[1])@puffin,是的,很抱歉我上面的请求不清楚。“我正在找那个。”奥雷尔,我刚刚编辑过。这是我的错,对不起。现在它显示正确了。这看起来不错。但是,如果可能的话,我需要将姓名和号码放在两个单独的列表中。你能帮我吗?补充了那部分。谢谢你,先生!非常有帮助!这看起来不错。但是,如果可能的话,我需要将姓名和号码放在两个单独的列表中。你能帮我吗?补充了那部分。谢谢你,先生!非常有帮助!