一个python新手问题:
我想用带有参数列表的 c 格式在 python 中打印:
agrs = [1,2,3,"hello"]
string = "This is a test %d, %d, %d, %s"
Run Code Online (Sandbox Code Playgroud)
如何使用 python 打印:
这是一个测试 1, 2, 3, 你好
谢谢。
我试图使用itertools.groupby帮助我按正或负属性对整数列表进行分组,例如:
输入
[1,2,3, -1,-2,-3, 1,2,3, -1,-2,-3]
Run Code Online (Sandbox Code Playgroud)
将返回
[[1,2,3],[-1,-2,-3],[1,2,3],[-1,-2,-3]]
Run Code Online (Sandbox Code Playgroud)
但是,如果我:
import itertools
nums = [1,2,3, -1,-2,-3, 1,2,3, -1,-2,-3]
group_list = list(itertools.groupby(nums, key=lambda x: x>=0))
print(group_list)
for k, v in group_list:
print(list(v))
>>>
[]
[-3]
[]
[]
Run Code Online (Sandbox Code Playgroud)
但是,如果我没有list()groupby对象,它将可以正常工作:
nums = [1,2,3, -1,-2,-3, 1,2,3, -1,-2,-3]
group_list = itertools.groupby(nums, key=lambda x: x>=0)
for k, v in group_list:
print(list(v))
>>>
[1, 2, 3]
[-1, -2, -3]
[1, 2, 3]
[-1, -2, -3]
Run Code Online (Sandbox Code Playgroud)
我不明白的是,groupby对象是由一对键和_grouper对象组成的迭代器,对list()groupby对象的调用不应该消耗该_grouper对象吗?
即使消耗掉了,我又如何[-3]从第二个元素中得到呢?
我想使用write()函数将struct对象写入文件.它必须是那个功能.
我在终端的输入是:./ main.c output.dat John Doe 45
当我运行程序并打开output.dat时,有一堆字母没有意义.请帮我.
我在output.dat文件中想要的输出是:John Doe 45
我的代码:
struct Person{
char* name;
char* lastName;
char* age;
};
int main(int argc, char** argv){
struct Person human;
/* get the argument values and store them into char* */
char* fileName = argv[1];
char* name = argv[2];
char* lastName = argv[3];
char* age = argv[4];
/* set the values of human object */
human.name = name;
human.lastName = lastName;
human.age = age;
/* open the file */ …Run Code Online (Sandbox Code Playgroud) 首先,举一个例子来说明我的问题背后的道德:下面的代码将无法编译,因为 std::basic_ostream::operator<< 是const不合格的。(https://gcc.gnu.org/onlinedocs/libstdc++/libstdc++-html-USERS-3.4/ostream-source.html显示操作符const不合格。)
我使用 GNU g++ 6.4.0 编译器编译,并启用了 --std=c++11 标志。
#ifndef TEST_H
#define TEST_H
#include<string>
#include<iostream>
using namespace std;
class ChessPiece{
const string name;
const int side;
public:
ChessPiece(const string&,const int);
void printPiece(const ostream&) const;
};
#endif // TEST_H
Run Code Online (Sandbox Code Playgroud)
...和test.cpp。
#include"test.h"
ChessPiece::ChessPiece(const string& s,const int bw): name{s}, side{bw} {}
void ChessPiece::printPiece(const ostream& S=cout) const{
S << "a " << (side==1?"white ":"black ") << name << endl;
}
int main(){
ChessPiece p{string("pawn"),-1}; // a black …Run Code Online (Sandbox Code Playgroud) 我需要清理一些文本,如下面的代码所示:
import re
def clean_text(text):
text = text.lower()
#foction de replacement
text = re.sub(r"i'm","i am",text)
text = re.sub(r"she's","she is",text)
text = re.sub(r"can't","cannot",text)
text = re.sub(r"[-()\"#/@;:<>{}-=~|.?,]","",text)
return text
clean_questions= []
for question in questions:
clean_questions.append(clean_text(question))
Run Code Online (Sandbox Code Playgroud)
并且这段代码必须给我一个questions干净的列表,但我得到了干净的questions空。我重新打开了 spyder,列表已满,但没有被清理,然后重新打开它,我把它清空了.. 控制台错误说:
In [10] :clean_questions= []
...: for question in questions:
...: clean_questions.append(clean_text(question))
Traceback (most recent call last):
File "<ipython-input-6-d1c7ac95a43f>", line 3, in <module>
clean_questions.append(clean_text(question))
File "<ipython-input-5-8f5da8f003ac>", line 16, in clean_text
text = re.sub(r"[-()\"#/@;:<>{}-=~|.?,]","",text)
File "C:\Users\hp\Anaconda3\lib\re.py", line 192, in sub
return …Run Code Online (Sandbox Code Playgroud) 我希望能够将值添加到列表中。例如,我有以下两个列表:
alist = [1,3,5,7,9]
blist = [0]
Run Code Online (Sandbox Code Playgroud)
我想向中插入一个值blist,该值是alist之前具有的所有值alist。例如,blist将是以下内容:
blist = [0,1,4,9,25]
Run Code Online (Sandbox Code Playgroud)
由于1是第一个值,alist它保持不变,所以我执行1 + 3 = 4、1 + 3 + 5 = 9等。但是,我对如何实现它非常困惑。我有以下代码:
list1 = [1,2,3,4,5]
list2 = [0]
x = 0
while x < len(list1):
blist.append(alist[0])
Run Code Online (Sandbox Code Playgroud)
这会将第0个位置的第一个值附加到list2,并添加list2 = [0,1]。但是,我不知道如何以我需要的方式添加值。
我有一个非常简单的示例类,使用C ++ 17编译。
#include <string>
#include <iostream>
struct name {
std::string first, middle, last;
name() = default;
name(name const&) = default;
name(name &&) = default;
name& operator=(name o) {
std::swap(*this, o); // bad!
return *this;
}
};
int main() {
name n1{"mr", "john", "smith"};
name n2 = std::move(n1);
name n3 = n2;
std::cout << n3.first << " " << n3.middle << " " << n3.last;
}
Run Code Online (Sandbox Code Playgroud)
使用这种价值语义,将移动分配捆绑在一起,我故意将其称为合格交换,而不是using std::swap; swap(*this, o);。无论如何,我没有提供交换。考虑到STL将交换实现为移动构造和一系列移动分配,因此我认为此实现将无限递归,交换调用move和移动调用swap。被std::swap改变成成员明智的掉期或类似的东西?
版本是Python 3.7。我刚刚发现python有时会将字符ñ存储在具有多种表示形式的字符串中,而我完全不知道为什么或如何处理它。
我不确定显示此问题的最佳方法,所以我将仅显示一些代码输出。
我有两个字符串s1和s2都设置为相等 'Dan Pen?a'
它们都是字符串类型。
我可以运行代码:
print(s1 == s2) # prints false
print(len(s1)) # prints 8
print(len(s2)) # prints 9
print(type(s1)) # print 'str'
print(type(s2)) # print 'str'
for i in range(len(s1)):
print(s1[i] + ", " + s2[i])
Run Code Online (Sandbox Code Playgroud)
循环的输出为:
D, D
a, a
n, n
,
P, P
e, e
n?, n
a, ~
Run Code Online (Sandbox Code Playgroud)
那么,是否有任何python方法来处理这些不一致问题,或者至少有一些关于python什么时候使用哪种表示形式的规范?
很高兴知道Python为什么会选择以这种方式实现。
编辑:
一个字符串从Django数据库中检索,另一个字符串从解析列表目录调用中的文件名获得的字符串中。
from app.models import Model
from django.core.management.base import BaseCommand
class Command(BaseCommand):
def handle(self, *args, **kwargs):
load_dir = "load_dir_name"
save_dir = "save_dir" …Run Code Online (Sandbox Code Playgroud) friends = my[:] #1
friends[:] = my #2
Run Code Online (Sandbox Code Playgroud)
两个变量都可以是列表或字符串,即可迭代的。我正在学习蟒蛇。
我在 linux 上使用 python3.6,遇到了一个非常明显的 abs() 函数失败。我的变量x最终变成了一个非常大的负数(可能是-inf),但绝对值abs()函数仍然返回一个负数,这应该是不可能的。我通过在abs()but....的输入中添加 0.1 来快速修复我的代码,我是否误解了abs()应该如何使用?
$> x
-9223372036854775808
$> abs(x)
-9223372036854775808
$> np.abs(x)
-9223372036854775808
$> abs(x+.1)
9.223372036854776e+18
$> np.abs(x+.1)
9.223372036854776e+18
Run Code Online (Sandbox Code Playgroud)
编辑:在下面解决了,但归结x为一个numpy.int64而不仅仅是int我不知道的。