improve your python code(10)

1. 深拷贝与浅拷贝

  • Python中对象的赋值都是进行对象引用(内存地址)传递
  • 使用copy.copy(),可以进行对象的浅拷贝,它复制了对象,但对于对象中的元素,依然使用原始的引用.
  • 如果需要复制一个容器对象,以及它里面的所有元素(包含元素的子元素),可以使用copy.deepcopy()进行深拷贝
  • 对于非容器类型(如数字、字符串、和其他’原子’类型的对象)没有被拷贝一说
  • 如果元祖变量只包含原子类型对象,则不能深拷贝。

2.使用Counter进行计数统计

#!/usr/bin/env python
# encoding: utf-8


"""
@python version: python3.6.1
@author: XiangguoSun
@contact: sunxiangguodut@qq.com
@site: http://blog.youkuaiyun.com/github_36326955
@software: PyCharm
@file: counter.py
@time: 5/7/2017 7:32 PM
"""
from collections import Counter

data = ['a', '2', 2, 4, 5, '2', 'b', 4, 7, 'a', 5, 'd', 'a', 'z']
print(Counter(data))
print(list(Counter(data).elements())) """Counter(data).elements()获取Counter中的key值,是一个chain的迭代器"""
print(Counter(data).most_common(2)) 
"""找出前n个出现频率最高的元素以及他们对应的次数"""
print(Counter(data)['y'])   
"""访问不存在的元素时,返回0,而不是KeyError异常"""
"""
output:
Counter({'a': 3, '2': 2, 4: 2, 5: 2, 2: 1, 'b': 1, 7: 1, 'd': 1, 'z': 1})
['a', 'a', 'a', '2', '2', 2, 4, 4, 5, 5, 'b', 7, 'd', 'z']
[('a', 3), ('2', 2)]
0
"""



"""
update()用于被统计对象元素的更新,原有Count计数器对象与新增元素的统计计数值相加。
subtract()用于实现计数器对象中统计值相减,允许有负值。
"""
c = Counter("success")
print(c)
c.update("successfully")
print(c)
c.subtract("successzz")
print(c)
"""
output:
Counter({'s': 3, 'c': 2, 'u': 1, 'e': 1})
Counter({'s': 6, 'c': 4, 'u': 3, 'e': 2, 'l': 2, 'f': 1, 'y': 1})
Counter({'s': 3, 'u': 2, 'c': 2, 'l': 2, 'e': 1, 'f': 1, 'y': 1, 'z': -2})
"""

a = Counter(a=1,b=2,c=3,d=1)
print(a)        # output Counter({'c': 3, 'b': 2, 'a': 1, 'd': 1})

3.

### Python Code Examples for Expressing Love Certainly! Below are some creative and thoughtful ways to express love through Python code: #### Simple Heart Shape Using Print Statements A straightforward way to show affection is by printing a heart shape. ```python print(" *** ") print(" ***** *****") print(" *********** ") print("*** ***") print("* *") ``` This creates a visual representation of a heart using asterisks[^1]. #### Personalized Message Generator Creating personalized messages can add an extra layer of sentimentality. ```python def generate_love_message(name): message = f"Dear {name},\n\nYou mean the world to me." return message partner_name = "Alice" print(generate_love_message(partner_name)) ``` The function `generate_love_message` takes a name as input and returns a heartfelt message directed at that person[^2]. #### Interactive Love Calculator (for Fun) An interactive program where users can enter their names along with their partner's name to get a fun score indicating compatibility. ```python import random def calculate_love_score(your_name, partner_name): combined_names = your_name + partner_name score = sum(ord(char.lower()) - ord('a') + 1 for char in combined_names if char.isalpha()) final_score = score % 100 + 1 return min(final_score, 100) your_name = input("Enter your name: ").strip() partner_name = input("Enter your partner's name: ").strip() score = calculate_love_score(your_name, partner_name) print(f"\nThe love score between you and {partner_name} is {score}%!") if score >= 85: print("That’s incredibly high! True love might be found here.") elif score >= 60: print("It looks promising!") else: print("Keep nurturing this relationship; it has potential.") ``` In this script, user inputs two names which then go into calculating a pseudo-love percentage based on character values from ASCII codes. The result provides playful feedback about the likelihood of true love existing between those named individuals[^3]. --related questions-- 1. How do I modify these scripts to include more personal details? 2. Can such programs help improve communication within relationships? 3. What other types of symbolic patterns could one create using Python print statements besides hearts? 4. Is there any library specifically designed for generating romantic content via programming languages like Python?
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值