我试图理解Python字符串何时相同(也就是共享相同的内存位置).但是在我的测试中,当两个相等的字符串变量共享相同的内存时似乎没有明显的解释:
import sys
print(sys.version) # 3.4.3
# Example 1
s1 = "Hello"
s2 = "Hello"
print(id(s1) == id(s2)) # True
# Example 2
s1 = "Hello" * 3
s2 = "Hello" * 3
print(id(s1) == id(s2)) # True
# Example 3
i = 3
s1 = "Hello" * i
s2 = "Hello" * i
print(id(s1) == id(s2)) # False
# Example 4
s1 = "HelloHelloHelloHelloHello"
s2 = "HelloHelloHelloHelloHello"
print(id(s1) == id(s2)) # True
# Example 5
s1 = "Hello" * …Run Code Online (Sandbox Code Playgroud) 我想按“ pos”键对字典列表进行排序。但是,如果字典中缺少“ pos”,则我想保留项目的顺序,并假定“ pos”是列表中该项目从1开始的索引。
只要所有列表项都不同,就可以正常工作:
L = [
{ "id": "1" }, # assume pos: 1
{ "id": "2" }, # assume pos: 2
{ "id": "3" }, # assume pos: 3
{ "id": "4" }, # assume pos: 4
{ "id": "ZZZ" }, # assume pos: 5
{ "id": "AAA" }, # assume pos: 6
{ "id": "ABC", "pos": 3.2 },
{ "id": "XYZ", "pos": 3.1 },
]
s = sorted(L,key=lambda i:i.get("pos",L.index(i)+1))
print(s)
Run Code Online (Sandbox Code Playgroud)
输出:
[{'id': '1'}, {'id': '2'}, {'id': …Run Code Online (Sandbox Code Playgroud) 我有一个这样的元组
t = (1, '0076', 'AU', '9927016803A', '9927013903B', '0010', 'VO')
Run Code Online (Sandbox Code Playgroud)
我想提取前 6 个值作为元组(按顺序)和最后一个值作为字符串。
下面的代码已经工作,但我想知道是否有一个“单行”来实现我正在寻找的东西。
# works, but it's not nice to unpack each value individually
cid,sc,ma,comp,mat,step,alt = t
t_new = (cid,sc,ma,comp,mat,step,)
print(t_new, alt) # (1, '0076', 'AU', '9927016803A', '9927013903B', '0010') VO
Run Code Online (Sandbox Code Playgroud)
这与我正在寻找的非常接近,但它将第一个值作为列表而不是元组返回:
# works, but returns list
*t_new,alt = t
print(t_new, alt) # [1, '0076', 'AU', '9927016803A', '9927013903B', '0010'] VO
Run Code Online (Sandbox Code Playgroud)
我已经尝试了以下方法,但没有成功:
tuple(*t_new),alt = t # SyntaxError
(*t_new),alt = t # still a list
(*t_new,),alt = t # ValueError
Run Code Online (Sandbox Code Playgroud)
如果没有其他方法,我可能会进行第二次尝试并将列表转换为元组。
我的问题是我有两个函数,其中一个返回值是以下列方式的两个字典:
def fnct1():
return dict1, dict2
Run Code Online (Sandbox Code Playgroud)
所以它返回到我的另一个函数,返回值是前一个函数的两个字典,也是一个新的字典,所以像这样的东西
def fnct2():
return dict3, fnct(1)
Run Code Online (Sandbox Code Playgroud)
这个问题是有以下结果:
({dict3},({dict1},{dict2})
Run Code Online (Sandbox Code Playgroud)
但我希望它看起来如下:
({dict3},{dict1},{dict2})
Run Code Online (Sandbox Code Playgroud) 我发现这个例子非常令人困惑,因为他们使用两个不同的参数为什么?
class Person:
def __init__(mysillyobject, name, age):
mysillyobject.name = name
mysillyobject.age = age
def myfunc(abc):
print("Hello my name is " + abc.name)
p1 = Person("John", 36)
p1.myfunc()
Run Code Online (Sandbox Code Playgroud) 我试图学习在Python(3.6.5)中使用这个简单的生日单行,但是我得到了SyntaxError:语法无效,有人可以帮我弄清楚我出错的地方:
print map(lambda x: "Happy Birthday to " + ("you" if x != 2 else "dear Name"),range(4))
Run Code Online (Sandbox Code Playgroud)
谢谢!
from random import randrange
n = int(input("Enter the number of throws: "))
throw1 = []
throw2 = []
for i in range(n):
throw1.append(int(randrange(1,7)))
throw2.append(int(randrange(1, 7)))
final_throw = sum(throw1, throw2)
print(throw1,throw2)
Run Code Online (Sandbox Code Playgroud)
我想将 throw1 和 throw2 加在一起,但我不知道如何(这不起作用)。
我的问题很容易解决,但作为初学者,我看不到解决方案。请问你能帮我吗?
def target(heart):
age = input("Enter your age: ")
rest_heart = input("Enter your resting heart rate: ")
intensity = input("Enter your heart intensity: ")
max_heart = 220 - age
reserve = max_heart - rest_heart
return rest_heart + intensity * reserve
print(target(heart))
Run Code Online (Sandbox Code Playgroud)
错误信息:
Traceback (most recent call last):
File "<string>", line 9, in <module>
NameError: name 'heart' is not defined
Run Code Online (Sandbox Code Playgroud)
如果有人能够指出错误,那将非常感激
python ×8
python-3.x ×2
constructor ×1
dictionary ×1
list ×1
memory ×1
return ×1
sorting ×1
string ×1
tuples ×1