Hướng dẫn python find number of different elements in list - python tìm số phần tử khác nhau trong danh sách

Vì vậy, tôi đang cố gắng thực hiện chương trình này sẽ yêu cầu người dùng đầu vào và lưu trữ các giá trị trong một mảng / danh sách. Sau đó, khi một dòng trống được nhập, nó sẽ cho người dùng biết có bao nhiêu giá trị đó là duy nhất. Tôi đang xây dựng điều này vì lý do thực tế và không phải là một vấn đề được đặt ra.
Then when a blank line is entered it will tell the user how many of those values are unique.
I'm building this for real life reasons and not as a problem set.

enter: happy
enter: rofl
enter: happy
enter: mpg8
enter: Cpp
enter: Cpp
enter:
There are 4 unique words!

Mã của tôi như sau:

# ask for input
ipta = raw_input["Word: "]

# create list 
uniquewords = [] 
counter = 0
uniquewords.append[ipta]

a = 0   # loop thingy
# while loop to ask for input and append in list
while ipta: 
  ipta = raw_input["Word: "]
  new_words.append[input1]
  counter = counter + 1

for p in uniquewords:

.. và đó là về tất cả những gì tôi đã nhận được cho đến nay. Tôi không chắc làm thế nào để đếm số từ duy nhất trong danh sách? Nếu ai đó có thể đăng giải pháp để tôi có thể học hỏi từ nó, hoặc ít nhất là cho tôi thấy nó sẽ tuyệt vời như thế nào, cảm ơn!
I'm not sure how to count the unique number of words in a list?
If someone can post the solution so I can learn from it, or at least show me how it would be great, thanks!

Buhtz

9.30713 Huy hiệu vàng66 Huy hiệu bạc130 Huy hiệu đồng13 gold badges66 silver badges130 bronze badges

hỏi ngày 5 tháng 9 năm 2012 lúc 13:11Sep 5, 2012 at 13:11

0

Ngoài ra, sử dụng bộ sưu tập. Chuỗi để tái cấu trúc mã của bạn:

from collections import Counter

words = ['a', 'b', 'c', 'a']

Counter[words].keys[] # equals to list[set[words]]
Counter[words].values[] # counts the elements' frequency

Output:

['a', 'c', 'b']
[2, 1, 1]

Thumper

5251 Huy hiệu vàng5 Huy hiệu bạc20 Huy hiệu đồng1 gold badge5 silver badges20 bronze badges

Đã trả lời ngày 5 tháng 9 năm 2012 lúc 19:04Sep 5, 2012 at 19:04

VidulvidulVidul

9.7912 Huy hiệu vàng17 Huy hiệu bạc20 Huy hiệu Đồng2 gold badges17 silver badges20 bronze badges

7

Bạn có thể sử dụng một tập hợp để loại bỏ các bản sao, sau đó chức năng LEN để đếm các phần tử trong tập hợp:

len[set[new_words]]

Đã trả lời ngày 5 tháng 9 năm 2012 lúc 13:14Sep 5, 2012 at 13:14

CodeboxCodeBoxcodebox

Phim thương hiệu vàng 19,5K77 gold badges60 silver badges79 bronze badges

0

from collections import Counter

words = ['a', 'b', 'c', 'a']

Counter[words].keys[] # equals to list[set[words]]
Counter[words].values[] # counts the elements' frequency
4

Chi tiết hơn

import numpy as np

words = ['b', 'a', 'a', 'c', 'c', 'c']
values, counts = np.unique[words, return_counts=True]

Hàm numpy.unique trả về các yếu tố duy nhất được sắp xếp của danh sách đầu vào cùng với số lượng của chúng:sorted unique elements of the input list together with their counts:

['a', 'b', 'c']
[2, 1, 3]

Đã trả lời ngày 8 tháng 7 năm 2018 lúc 4:06Jul 8, 2018 at 4:06

James Hirschornjames HirschornJames Hirschorn

6.1954 Huy hiệu vàng40 Huy hiệu bạc51 Huy hiệu đồng4 gold badges40 silver badges51 bronze badges

4

Sử dụng một bộ:

words = ['a', 'b', 'c', 'a']
unique_words = set[words]             # == set[['a', 'b', 'c']]
unique_word_count = len[unique_words] # == 3

Được trang bị điều này, giải pháp của bạn có thể đơn giản như:

words = []
ipta = raw_input["Word: "]

while ipta:
  words.append[ipta]
  ipta = raw_input["Word: "]

unique_word_count = len[set[words]]

print "There are %d unique words!" % unique_word_count

Đã trả lời ngày 5 tháng 9 năm 2012 lúc 13:15Sep 5, 2012 at 13:15

Linus Thiellinus ThielLinus Thiel

38K9 Huy hiệu vàng105 Huy hiệu bạc102 Huy hiệu Đồng9 gold badges105 silver badges102 bronze badges

1

aa="XXYYYSBAA"
bb=dict[zip[list[aa],[list[aa].count[i] for i in list[aa]]]]
print[bb]
# output:
# {'X': 2, 'Y': 3, 'S': 1, 'B': 1, 'A': 2}

Đã trả lời ngày 14 tháng 8 năm 2019 lúc 18:05Aug 14, 2019 at 18:05

MadjayhawkmadjayhawkMadJayhawk

1872 Huy hiệu bạc8 Huy hiệu Đồng2 silver badges8 bronze badges

2

Đối với Ndarray, có một phương pháp Numpy gọi là duy nhất:

# ask for input
ipta = raw_input["Word: "]

# create list 
uniquewords = [] 
counter = 0
uniquewords.append[ipta]

a = 0   # loop thingy
# while loop to ask for input and append in list
while ipta: 
  ipta = raw_input["Word: "]
  new_words.append[input1]
  counter = counter + 1

for p in uniquewords:
0

Examples:

# ask for input
ipta = raw_input["Word: "]

# create list 
uniquewords = [] 
counter = 0
uniquewords.append[ipta]

a = 0   # loop thingy
# while loop to ask for input and append in list
while ipta: 
  ipta = raw_input["Word: "]
  new_words.append[input1]
  counter = counter + 1

for p in uniquewords:
1

Đối với một chuỗi có một hàm gọi giá trị_counts []:

# ask for input
ipta = raw_input["Word: "]

# create list 
uniquewords = [] 
counter = 0
uniquewords.append[ipta]

a = 0   # loop thingy
# while loop to ask for input and append in list
while ipta: 
  ipta = raw_input["Word: "]
  new_words.append[input1]
  counter = counter + 1

for p in uniquewords:
2

Tatlar

2.9862 Huy hiệu vàng30 Huy hiệu bạc38 Huy hiệu Đồng2 gold badges30 silver badges38 bronze badges

Đã trả lời ngày 8 tháng 11 năm 2017 lúc 13:21Nov 8, 2017 at 13:21

user78692user78692user78692

611 Huy hiệu bạc2 Huy hiệu đồng1 silver badge2 bronze badges

Nếu bạn muốn có một biểu đồ của các giá trị độc đáo thì đây là Oneliner

# ask for input
ipta = raw_input["Word: "]

# create list 
uniquewords = [] 
counter = 0
uniquewords.append[ipta]

a = 0   # loop thingy
# while loop to ask for input and append in list
while ipta: 
  ipta = raw_input["Word: "]
  new_words.append[input1]
  counter = counter + 1

for p in uniquewords:
3

Đã trả lời ngày 24 tháng 11 năm 2020 lúc 17:22Nov 24, 2020 at 17:22

Evalds Urtansevalds UrtansEvalds Urtans

6.1961 Huy hiệu vàng40 Huy hiệu bạc 30 Huy hiệu Đồng1 gold badge40 silver badges30 bronze badges

Bạn nghĩ thế nào về:

# ask for input
ipta = raw_input["Word: "]

# create list 
uniquewords = [] 
counter = 0
uniquewords.append[ipta]

a = 0   # loop thingy
# while loop to ask for input and append in list
while ipta: 
  ipta = raw_input["Word: "]
  new_words.append[input1]
  counter = counter + 1

for p in uniquewords:
4

Nó trả về số lượng giá trị duy nhất trong danh sách

Đã trả lời ngày 31 tháng 7 năm 2020 lúc 10:58Jul 31, 2020 at 10:58

1

Bạn có thể sử dụng phương thức

from collections import Counter

words = ['a', 'b', 'c', 'a']

Counter[words].keys[] # equals to list[set[words]]
Counter[words].values[] # counts the elements' frequency
5:

# ask for input
ipta = raw_input["Word: "]

# create list 
uniquewords = [] 
counter = 0
uniquewords.append[ipta]

a = 0   # loop thingy
# while loop to ask for input and append in list
while ipta: 
  ipta = raw_input["Word: "]
  new_words.append[input1]
  counter = counter + 1

for p in uniquewords:
5

Output:

# ask for input
ipta = raw_input["Word: "]

# create list 
uniquewords = [] 
counter = 0
uniquewords.append[ipta]

a = 0   # loop thingy
# while loop to ask for input and append in list
while ipta: 
  ipta = raw_input["Word: "]
  new_words.append[input1]
  counter = counter + 1

for p in uniquewords:
6

Đã trả lời ngày 13 tháng 7 năm 2021 lúc 18:00Jul 13, 2021 at 18:00

# ask for input
ipta = raw_input["Word: "]

# create list 
uniquewords = [] 
counter = 0
uniquewords.append[ipta]

a = 0   # loop thingy
# while loop to ask for input and append in list
while ipta: 
  ipta = raw_input["Word: "]
  new_words.append[input1]
  counter = counter + 1

for p in uniquewords:
7

Đã trả lời ngày 5 tháng 9 năm 2012 lúc 13:20Sep 5, 2012 at 13:20

user1590499user1590499user1590499

9013 Huy hiệu vàng10 Huy hiệu bạc17 Huy hiệu đồng3 gold badges10 silver badges17 bronze badges

Mặc dù một tập hợp là cách dễ nhất, bạn cũng có thể sử dụng Dict và sử dụng

from collections import Counter

words = ['a', 'b', 'c', 'a']

Counter[words].keys[] # equals to list[set[words]]
Counter[words].values[] # counts the elements' frequency
6 để điền vào từ điển chỉ có các khóa và giá trị duy nhất.

Giả sử bạn đã điền

from collections import Counter

words = ['a', 'b', 'c', 'a']

Counter[words].keys[] # equals to list[set[words]]
Counter[words].values[] # counts the elements' frequency
7 với đầu vào từ người dùng, tạo một bản đồ Dicting các từ duy nhất trong danh sách thành một số:

# ask for input
ipta = raw_input["Word: "]

# create list 
uniquewords = [] 
counter = 0
uniquewords.append[ipta]

a = 0   # loop thingy
# while loop to ask for input and append in list
while ipta: 
  ipta = raw_input["Word: "]
  new_words.append[input1]
  counter = counter + 1

for p in uniquewords:
8

Đã trả lời ngày 10 tháng 6 năm 2016 lúc 17:54Jun 10, 2016 at 17:54

Phương pháp khác bằng cách sử dụng gấu trúc

# ask for input
ipta = raw_input["Word: "]

# create list 
uniquewords = [] 
counter = 0
uniquewords.append[ipta]

a = 0   # loop thingy
# while loop to ask for input and append in list
while ipta: 
  ipta = raw_input["Word: "]
  new_words.append[input1]
  counter = counter + 1

for p in uniquewords:
9

Sau đó, bạn có thể xuất kết quả ở bất kỳ định dạng nào bạn muốn

Đã trả lời ngày 14 tháng 8 năm 2019 lúc 13:52Aug 14, 2019 at 13:52

HazimoRa3dHazimoRa3dHazimoRa3d

5074 Huy hiệu bạc12 Huy hiệu Đồng4 silver badges12 bronze badges

Sau đây nên hoạt động. Hàm Lambda lọc ra các từ trùng lặp.

from collections import Counter

words = ['a', 'b', 'c', 'a']

Counter[words].keys[] # equals to list[set[words]]
Counter[words].values[] # counts the elements' frequency
0

Đã trả lời ngày 5 tháng 9 năm 2012 lúc 13:26Sep 5, 2012 at 13:26

John Wangjohn WangJohn Wang

4.4648 Huy hiệu vàng34 Huy hiệu bạc 50 Huy hiệu Đồng8 gold badges34 silver badges50 bronze badges

Tôi sẽ sử dụng một tập hợp bản thân, nhưng đây là một cách khác:

from collections import Counter

words = ['a', 'b', 'c', 'a']

Counter[words].keys[] # equals to list[set[words]]
Counter[words].values[] # counts the elements' frequency
1

Đã trả lời ngày 5 tháng 9 năm 2012 lúc 13:31Sep 5, 2012 at 13:31

Nicola Musattinicola MusattiNicola Musatti

17.4K2 Huy hiệu vàng47 Huy hiệu bạc53 Huy hiệu đồng2 gold badges47 silver badges53 bronze badges

from collections import Counter

words = ['a', 'b', 'c', 'a']

Counter[words].keys[] # equals to list[set[words]]
Counter[words].values[] # counts the elements' frequency
2

Đã trả lời ngày 5 tháng 9 năm 2012 lúc 13:31Sep 5, 2012 at 13:31

Nicola Musattinicola MusattiCurious

17.4K2 Huy hiệu vàng47 Huy hiệu bạc53 Huy hiệu đồng1 gold badge29 silver badges40 bronze badges

Tò mò

from collections import Counter

words = ['a', 'b', 'c', 'a']

Counter[words].keys[] # equals to list[set[words]]
Counter[words].values[] # counts the elements' frequency
3

2.6481 Huy hiệu vàng29 Huy hiệu bạc40 Huy hiệu đồngSep 2, 2021 at 12:44

2

Làm thế nào để bạn đếm nhiều mục trong danh sách Python?

Nếu bạn muốn đếm nhiều mục trong danh sách, bạn có thể gọi Count [] trong một vòng lặp. Tuy nhiên, cách tiếp cận này đòi hỏi một đường chuyền riêng biệt trong danh sách cho mỗi cuộc gọi đếm []; mà có thể là thảm họa cho hiệu suất. Thay vào đó, sử dụng phương thức couter [] từ các bộ sưu tập lớp.call count[] in a loop. This approach, however, requires a separate pass over the list for every count[] call; which can be catastrophic for performance. Use couter[] method from class collections , instead.

Làm thế nào để bạn đếm các loại yếu tố trong một danh sách trong Python?

Cách đơn giản nhất để có được số lượng các phần tử trong danh sách là sử dụng chức năng tích hợp Python Len [].use the Python built-in function len[] .

Làm thế nào để bạn đếm số lượng các giá trị duy nhất trong một danh sách trong Python?

Sử dụng Numpy nhập khẩu của Python, các yếu tố duy nhất trong mảng cũng thu được.Trong bước đầu tiên, hãy chuyển đổi danh sách thành xconvert the list to x=numpy. array[list] and then use numpy. unique[x] function to get the unique values from the list.

Làm thế nào để bạn đếm các giá trị duy nhất trong một danh sách?

Bạn có thể sử dụng kết hợp các hàm tổng và đếm để đếm các giá trị duy nhất trong Excel.Cú pháp cho công thức kết hợp này là = sum [if [1/Countif [dữ liệu, dữ liệu] = 1,1,0]].Ở đây, công thức đếm số đếm số lần mỗi giá trị trong phạm vi xuất hiện.use the combination of the SUM and COUNTIF functions to count unique values in Excel. The syntax for this combined formula is = SUM[IF[1/COUNTIF[data, data]=1,1,0]]. Here the COUNTIF formula counts the number of times each value in the range appears.

Bài Viết Liên Quan

Chủ Đề