Python practical skills: two different lists give similarity (int, str type)

Python data development requires two different lists to give similarity. This article implements the judgment of similarity between numeric types and string types, which is very practical!


Number Type Similarity


The cosine similarity can be used to calculate the similarity of two lists. Specific steps are as follows:

  1. Convert the two lists to vector form, that is, the elements in the list are used as the components of the vector.
  2. Computes the dot product of vectors.
  3. Computes the modulo length of a vector.
  4. Computes the cosine.
  5. Take the cosine value as the similarity.

The following is the Python code implementation:

import math

def cosine_similarity(list1, list2):
    dot_product = sum([list1[i]*list2[i] for i in range(len(list1))])
    norm1 = math.sqrt(sum([x**2 for x in list1]))
    norm2 = math.sqrt(sum([x**2 for x in list2]))
    similarity = dot_product / (norm1 * norm2)
    return similarity

# 示例
list1 = [1, 2, 3, 4, 5]
list2 = [3, 4, 5, 6, 7]
similarity = cosine_similarity(list1, list2)
print(similarity)

The output is:

0.9970544855015815

Indicates that the similarity between the two lists is high.


string type similarity



The similarity between two lists of strings can be calculated using Jaccard similarity. Jaccard similarity measures the similarity of two sets by calculating the ratio of their intersection and union. Specific steps are as follows:

  1. Convert the two lists into a collection form, that is, the elements in the list are the elements of the collection.
  2. Computes the intersection of two sets.
  3. Computes the union of two sets.
  4. Computes the ratio of intersection to union.
  5. Take the ratio as the similarity.

The following is the Python code implementation:

def jaccard_similarity(list1, list2):
    set1 = set(list1)
    set2 = set(list2)
    intersection = len(set1.intersection(set2))
    union = len(set1.union(set2))
    similarity = intersection / union
    return similarity

# 示例
list1 = ['apple', 'banana', 'orange']
list2 = ['apple', 'pear', 'peach']
similarity = jaccard_similarity(list1, list2)
print(similarity)

The output is:

0.25

Indicates that two lists of strings are less similar.

The above is the solution to the cause of this error. Welcome to leave a message in the comment area to discuss whether it can be solved.If it is useful, please like and collect the article. Thank you for your support. The blogger has the motivation to keep recording the problems encountered.!!!

Thousands of full-stack VIP Q&A group to contact bloggers to help solve errors

Due to the limited time and energy of bloggers, there are too many private messages every day, and there is no way for every fan to reply in time, so reply to VIP fans first, and you can enter the thousand-person full stack by subscribing to the time-limited 9.9 paid column "100 Days Mastering Python from Getting Started to Employment" VIP answering group, get priority answering opportunities (code guidance, remote service), free prostitution 80G learning materials spree, column subscription address: https://blog.csdn.net/yuan2019035055/category_11466020.html

  • Advantages :The author gives priority to answering opportunities (code guidance, remote service), and many bigwigs in the group can hold together to keep warm (big factory internal promotion opportunities). This column is a complete set of teaching specially prepared for students with zero foundation and those who need advanced improvement , From 0 to 100, continue to advance and deepen, and there will be practical projects in the follow-up, so you can easily deal with interviews!

  • Column benefits :Resume guidance, internal referral for recruitment, weekly delivery of physical books, 80G full-stack learning videos, 300 IT e-books: Python, Java, front-end, big data, database, algorithm, crawler, data analysis, machine learning, interview question bank, etc.

  • Note : If you want to get a timely reply, communicate and learn with the big guys, after subscribing to the column, private message the blogger to enter the VIP Q&A group with thousands of peopleinsert image description here
    insert image description here

Free information acquisition, more fan benefits, follow the official account below to obtain

insert image description here

book recommendation

"Metaverse Ⅱ: Graphical meta-technology blockchain, meta-assets and Web3.0, meta-humans and Utopia"

insert image description here

This is a brand new era: the economic system built by Web3.0, the cross-platform operation of DID identities, the atomic level mirroring of digital NFT, and the frictionless redistribution of meta-assets of DeFi... In 2022, the singularity will appear: Yuan Ren is about to be born ; meta-assets are about to be distributed; metaverse is taking shape. This set of books builds the first building of the metaverse through the three laws of the metaverse, the grand unified equation, the law of entropy increase, Web3.0, trillions of assets, metahumans and blockchain civilization. Floors 1-80: digital human booths, electronic pets, digital collections, 3D immersive tourism, DeFi. Layer 81-160: AI, VR, AR, MR, DAO, Web3.0, edge computing. Layers 161-214+: Multi-scene reading, 4K space, cross-chain licensing, dimension conversion, infinite ∞ world.

The old rules of giving books by lottery (no liking, collection and winning are invalid): Pay attention and remember to follow the blogger, otherwise you won’t know if you won the lottery! ! !

  • 1. Like and collect articles
  • 2. Leave a message in the comment area: Life is too short, I use Python! ! ! (Leave a message to enter the prize pool, and each person can leave a maximum of three messages)
  • 3. There are 5 people in the crawler draw at 8:00 on Sunday
  • If you don’t want to draw a lottery, Dangdang’s own purchase link: http://product.dangdang.com/29513251.html

book recommendation

"Understanding Quantum Mechanics from Scratch"

insert image description here

This book will systematically and rigorously introduce the basic principles and applications of quantum mechanics for the majority of science and technology enthusiasts. Readers need to be familiar with the relevant content of high school physics and mathematics, and willing to learn the scientific way of thinking. Although quantum mechanics is a modern science that has a mysterious veil, breaks the common sense of life, and subverts human cognition, as long as readers are willing to think along with this book, they will be able to clearly understand the basic concepts of quantum mechanics theory, and finally fully understand it. Role in the scientific system and contribution to modern technology.
The narrative method of this book is to explain scientific theories while introducing important experimental phenomena and the application of scientific principles. In the first part of this book, the basic concepts such as state superposition, wave-particle duality, and uncertainty principle are explained in sequence; in the second part, the application of quantum mechanics in the fields of condensed matter physics and elementary particle physics is introduced. At the same time, it also focuses on the introduction of modern electronic technology spawned by quantum mechanics.

The old rules of giving books by lottery (no liking, collection and winning are invalid): Pay attention and remember to follow the blogger, otherwise you won’t know if you won the lottery! ! !

  • 1. Like and collect articles
  • 2. Leave a message in the comment area: Life is too short, I use Python! ! ! (Leave a message to enter the prize pool, and each person can leave a maximum of three messages)
  • 3. There are 5 people in the crawler draw at 8:00 on Sunday
  • If you don’t want to draw a lottery, JD’s self-operated purchase link: https://item.jd.com/13096523.html

Guess you like

Origin blog.csdn.net/yuan2019035055/article/details/130338656