小编Mas*_*shy的帖子

如果 Python 字符串是不可变的,为什么当我使用 += 附加到它时它会保持相同的 id?

Python 中的字符串是不可变的,这意味着值不能更改。但是,当附加到以下示例中的字符串时,由于 id 保持不变,因此原始字符串内存看起来已被修改:

>>> s = 'String'
>>> for i in range(5, 0, -1):
...     s += str(i)
...     print(f"{s:<11} stored at {id(s)}")
... 
String5     stored at 139841228476848
String54    stored at 139841228476848
String543   stored at 139841228476848
String5432  stored at 139841228476848
String54321 stored at 139841228476848
Run Code Online (Sandbox Code Playgroud)

相反,在以下示例中,id 发生变化:

>>> a = "hello"
>>> id(a)
139841228475760
>>> a = "b" + a[1:]
>>> print(a)
bello
>>> id(a)
139841228475312
Run Code Online (Sandbox Code Playgroud)

python string reference string-concatenation immutability

102
推荐指数
2
解决办法
6958
查看次数

Python while 循环条件检查字符串

在 Codeacademy 中,我运行了这个简单的 Python 程序:

choice = raw_input('Enjoying the course? (y/n)')

while choice != 'y' or choice != 'Y' or choice != 'N' or choice != 'n':  # Fill in the condition (before the colon)
    choice = raw_input("Sorry, I didn't catch that. Enter again: ")
Run Code Online (Sandbox Code Playgroud)

我在控制台输入 y 但循环从未退出

所以我以不同的方式做到了

choice = raw_input('Enjoying the course? (y/n)')

while True:  # Fill in the condition (before the colon)
    if choice == 'y' or choice == 'Y' or choice == 'N' or choice == …
Run Code Online (Sandbox Code Playgroud)

python while-loop

6
推荐指数
1
解决办法
2万
查看次数

从python数据帧获取唯一日期

我的数据框的日期格式为:dd-mm-yy hh:mm:ss

例如

15-14-2016 08:05:10 
15-14-2016 08:15:30 
15-14-2016 10:45:22 
18-14-2016 06:23:10 
18-14-2016 07:37:30 
18-14-2016 12:48:22
Run Code Online (Sandbox Code Playgroud)

大约有 1000 行和

我使用下面的代码来获取唯一的日期

print pd.unique((df.Date).dt.strftime('%d-%m-%y'))
Run Code Online (Sandbox Code Playgroud)

但我的输出似乎是 2 个列表,而不仅仅是一个:

['15-04-16' '18-04-16' '19-04-16' '20-04-16' '21-04-16' '22-04-16']    
['15-04-16' '18-04-16' '19-04-16' '20-04-16' '21-04-16' '22-04-16']
Run Code Online (Sandbox Code Playgroud)

谁能帮忙

python datetime dataframe pandas

3
推荐指数
1
解决办法
7428
查看次数