我有我朋友的一些代码。他运转平稳,但我遇到
module **scipy.misc** has no attribute *imresize*
我正在搜索,已安装枕头(PIL),scipy,scikit等..但是不起作用
我问我的朋友,但他忘了安装了什么。
我可以做一个赋值解构:
a, b = s.split(' ', 1)
Run Code Online (Sandbox Code Playgroud)
对于s包含多个单词的字符串。
我们如何使用 Python 3.8 中引入的最新赋值表达式(是否可能有多个目标)在 an ifor 中做同样的事情elif?
我试过:
if some_thing:
# some code.
elif (a, b := s.split(' ', 1)) and some_func(a) and some_func(b):
# some code probably using a and b as well.Run Code Online (Sandbox Code Playgroud)
我收到以下错误:
elif (a, b := s.split(' ', 1)) and some_func(a) and some_func(b):
NameError: name 'a' is not defined
Run Code Online (Sandbox Code Playgroud)
我想要这个的原因是因为如果我的第一个条件得到满足,我不想不必要地拆分我的字符串。
我想使用f字符串格式化具有相同宽度的数字数组。数字可以是正数或负数。
最低工作示例
import numpy as np
arr = np.random.rand(10) - 0.5
for num in arr:
print(f"{num:0.4f}")
Run Code Online (Sandbox Code Playgroud)
结果是
0.0647
-0.2608
-0.2724
0.2642
0.0429
0.1461
-0.3285
-0.3914
Run Code Online (Sandbox Code Playgroud)
由于带有负号,因此无法以相同的宽度打印数字,这很烦人。如何使用F弦获得相同的宽度?
我能想到的一种方法是将数字转换为字符串并打印字符串。但是有没有比这更好的方法了?
for num in a:
str_ = f"{num:0.4f}"
print(f"{str_:>10}")
Run Code Online (Sandbox Code Playgroud) 我有一个包含 122 列的数据集,如下所示:
train.head()
SK_ID_CURR TARGET NAME_CONTRACT_TYPE CODE_GENDER FLAG_OWN_CAR FLAG_OWN_REALTY CNT_CHILDREN AMT_INCOME_TOTAL AMT_CREDIT AMT_ANNUITY ... FLAG_DOCUMENT_18 FLAG_DOCUMENT_19 FLAG_DOCUMENT_20 FLAG_DOCUMENT_21 AMT_REQ_CREDIT_BUREAU_HOUR AMT_REQ_CREDIT_BUREAU_DAY AMT_REQ_CREDIT_BUREAU_WEEK AMT_REQ_CREDIT_BUREAU_MON AMT_REQ_CREDIT_BUREAU_QRT AMT_REQ_CREDIT_BUREAU_YEAR
0 100002 1 Cash loans M N Y 0 202500.0 406597.5 24700.5 ... 0 0 0 0 0 0 0 0 0 1
1 100003 0 Cash loans F N N 0 270000.0 1293502.5 35698.5 ... 0 0 0 0 0 0 0 0 0 0
2 100004 0 Revolving loans M Y Y 0 …Run Code Online (Sandbox Code Playgroud) 我有一个 20GB、100k x 100k 'float16' 2D 数组作为数据文件。我将其加载到内存中,如下所示:
\n\nfp_read = np.memmap(filename, dtype='float16', mode='r', shape=(100000, 100000))\nRun Code Online (Sandbox Code Playgroud)\n\n然后我尝试从中读取切片。我需要采取的垂直切片实际上是随机的,但性能非常差,或者我做错了什么?
\n\n分析:
\n\n我与其他形式的横截面切片进行了比较,尽管我不知道为什么应该这样,但它要好得多:
\n\n%timeit fp_read[:,17000:17005] # slice 5 consecutive cols\n1.64 \xc2\xb5s \xc2\xb1 16.4 ns per loop (mean \xc2\xb1 std. dev. of 7 runs, 1000000 loops each)\n\n%timeit fp_read[:,11000:11050:10]\n1.67 \xc2\xb5s \xc2\xb1 21 ns per loop (mean \xc2\xb1 std. dev. of 7 runs, 1000000 loops each)\n\n%timeit fp_read[:,5000:6000:200]\n1.66 \xc2\xb5s \xc2\xb1 27.3 ns per loop (mean \xc2\xb1 std. dev. of 7 runs, 1000000 loops each)\n\n%timeit …Run Code Online (Sandbox Code Playgroud) 我有两个列表,其中包含未知数量的元素。一个包含名称的列表,另一个包含种姓
'''像这样'''
names = ['name1','name2',........'name99']
castes = ['cast1','cast2',........'cast99']
Run Code Online (Sandbox Code Playgroud)
我想这样打印:
Hello Name1 cast1.
Hello Name2 Cast2.
.
.
.
.
.
Hello Name99 Cast99
Run Code Online (Sandbox Code Playgroud)
我已经尝试过了,但是没有用。
for i in names:
for j in castes:
print('Hello '+ i + j)
Run Code Online (Sandbox Code Playgroud)
但是它随机打印。喜欢...
Hello Name1 Cast1
Hello Name1 Cast2
.
.
Hello Name1 Cast99
.
.
.
Hello Name2 Cast1
Hello Name2 Cast2
.
.
Hello Name3 Cast99
.
.
.
Run Code Online (Sandbox Code Playgroud) 我可以编写一个def函数来完成此任务。但是,我想知道是否有任何方法可以像下面这样在一行中执行此任务string.replace('a',' ')
input = "This is a regular text"
output = "This is_a regular_text"
Run Code Online (Sandbox Code Playgroud) 我目前有一个名为items的列表。我曾经items.sort()以升序获取它们,但我想要所需的输出。有没有简单但容易的方法在python中做到这一点?
items = ['hat1', 'mat3', 'bat2', 'bat1', 'hat2', 'mat4', 'hat5', 'hat6', 'mat1']
Run Code Online (Sandbox Code Playgroud)
当前O / P-
bat1, bat2, hat1, hat2, hat5, hat6, mat1, mat3, mat4
Run Code Online (Sandbox Code Playgroud)
所需的O / P-
bat1, bat2
hat1, hat2, hat5, hat6
mat1, mat3, mat4
Run Code Online (Sandbox Code Playgroud) 我有一个字典列表:
list = [
{'i': 1, 'j': 1, 'diff': 39},
{'i': 1, 'j': 1, 'diff': 27},
{'i': 1, 'j': 1, 'diff': 18},
{'i': 1, 'j': 1, 'diff': 33},
...
]
Run Code Online (Sandbox Code Playgroud)
由此,我需要为提取最大的条目的i和的值。jdiff
我已经看到了很多解决方案,它们只会提取出最大的结果diff,但这不是这里所需要的。而且我可以想到实现此目的的方法,该方法涉及将每个值逐个循环并与其他值进行比较,但这似乎既无效又无效。
最好的方法是什么?
我有:
mylist = [(['a', 'b'], [1, 2]), (['c', 'd'], [3])]
Run Code Online (Sandbox Code Playgroud)
我需要一个包含字母的列表和一个包含数字的列表,像这样:
(['a', 'b', 'c', 'd'], [1, 2, 3])
Run Code Online (Sandbox Code Playgroud)
我做了一些努力,但是我只能得到一个包含字母的列表,而不是两个都包含:
answer = [item for sublist in mylist for item in sublist[0]]
#returns ['a', 'b', 'c', 'd']
Run Code Online (Sandbox Code Playgroud) 我正在创建一个函数,它接受一个字符串并返回一个匹配的字符串,其中每个偶数字母都是大写,每个奇数字母都是小写。字符串只包含字母
我尝试了一个 for 循环,它使用 if 语句遍历字符串的长度,该语句检查索引是否为偶数返回该索引的大写字母,以及索引是否为奇数以返回该索引的小写字母。
def my_func(st):
for index in range(len(st)):
if index % 2 == 0:
return st.upper()
else:
return st.lower()
Run Code Online (Sandbox Code Playgroud)
我希望偶数字母大写,奇数字母小写,但我只得到整个字符串的大写。
这是我的Python 3代码:
from time import sleep
s='what is your name'
for x in s:
print(x,end='')
sleep(1)
Run Code Online (Sandbox Code Playgroud)
我期望的是,每封信在延迟一秒钟后将一一打印。但是,当我运行该程序时,需要花费len(s)几秒钟的时间,然后输出的值s。谁能向我解释实际情况。
python ×12
python-3.x ×10
numpy ×2
scikit-learn ×2
dictionary ×1
f-string ×1
list ×1
numpy-memmap ×1
pandas ×1
python-3.8 ×1
scipy ×1