我正在维护旧的Perl代码,需要在所有模块中启用严格的pragma.我在传递文件句柄作为模块和subs之间的引用时遇到问题.我们有一个公共模块负责打开日志文件,该文件作为typeglob引用传递.在其他模块中,run函数首先从公共模块调用open_log(),然后将此文件句柄传递给其他subs.
在这里,我写了一个简单的测试来模拟这种情况.
#!/usr/bin/perl -w
use strict;
$::STATUS_OK = 0;
$::STATUS_NOT_OK = 1;
sub print_header {
our $file_handle = @_;
print { $$file_handle } "#### HEADER ####"; # reference passing fails
}
sub print_text {
my ($file_handle, $text)= @_;
print_header(\$file_handle);
print { $$file_handle } $text;
}
sub open_file_handle {
my ($file_handle, $path, $name) = @_;
my $filename = $path."\\".$name;
unless ( open ($$file_handle, ">".$filename)) {
print STDERR "Failed to open file_handle $filename for writing.\n";
return $::STATUS_NOT_OK;
}
print STDERR "File $filename …Run Code Online (Sandbox Code Playgroud) 因此,我正在尝试创建一个程序来计算字符串中的字符数量,该程序是从用户处获得的,但我想要排除用户输入的任何空格.
def main():
full_name = str(input("Please enter in a full name: ")).split(" ")
for x in full_name:
print(len(x))
main()
Run Code Online (Sandbox Code Playgroud)
为此,我得到的只是字符的数字,没有空格,但我不知道如何将这些数字加在一起并打印出来.
我正在尝试使用Python的sorted函数对列表进行排序.在Python 3中,cmp删除了关键字参数.不幸的是,我似乎无法使用key关键字参数实现我的算法,因为我需要两个对象来比较数据.
示例排序数据
59 59
59 3 1 1
59 4 3 3
61 1
61 10
61 237
61 1 1 1
Run Code Online (Sandbox Code Playgroud)
比较功能
NUM_RE = re.compile("[\d]+")
def compare(x, y):
# Aggregate our data into integer arrays
x_result = [int(x) for x in NUM_RE.findall(x)]
y_result = [int(y) for y in NUM_RE.findall(y)]
# Return if there is a non-zero difference in the first element
statement_diff = x_result[0] - y_result[0]
if statement_diff != 0:
return statement_diff …Run Code Online (Sandbox Code Playgroud) 下面的代码完成了我想要的.它打印列表并在不连续的行的末尾添加星号,例如,如果您从1跳到3或从3跳到5.
use strict;
use warnings;
#note: thanks to all who helped with formatting issues.
#note: I recognize a hash would be a much better option for what I want to do.
my @printy = ("1 -> this",
"5 -> that",
"3 -> the other",
"6 -> thus and such");
@printy = sort {num($a) <=> num($b)} @printy;
my $thisID = 0;
my $lastID = 0;
#print out (line)* if initial number is >1 more than previous, or just (line) otherwise
for …Run Code Online (Sandbox Code Playgroud) 我有一列包含9个字符的数字。我需要对该列中的所有值执行一些操作以达到12的长度。这是原始数据:
493 123456789
494 123456789
496 115098765
497 123456789
498 987654321
499 987654321
Run Code Online (Sandbox Code Playgroud)
现在,我需要对数字进行一些修改:
理想的解决方案是:
493 120234056789
494 120234056789
496 120150098765
497 120234056789
498 920876054321
499 920876054321
Run Code Online (Sandbox Code Playgroud)
我怎样才能做到这一点?提前致谢。
这个函数的输入是两个字符串,目的是判断这两个字符串是否是字谜.例如,"qwerty"和"qetyrw"是anagram,这意味着重新排列第一个字符串中的字符可以得到第二个字符串.并且不要求区分大小写."qwerty"和"QWerTY"也是字谜.我只是混淆了我的功能,什么也没有返回.我的功能如下:
def is_anagram(string_a,string_b):
"""returns True if the strings are anagrams of each other
str, list -> boolean"""
new_a=string_a.lower()
new_b=string_b.lower()
i=0
if len(string_a)!=len(string_b):
return False
else:
while i<=len(new_a)-1:
if new_a[i] in new_b:
list(new_b).remove(new_a[i])
i=i+1
break
if len(list(new_b))==0:
return True
else:
return False
Run Code Online (Sandbox Code Playgroud) 我想加入一个字符串,但在偶数和奇数情况下有 2 个不同的分隔符。
我有这个清单:
l = [1,2,3,4,5,6,7,8,9]
Run Code Online (Sandbox Code Playgroud)
我需要像这样加入它:
1 || 2 || \n 3 || 4 || \n 5 || 6 || \n 7 || 8 || \n 9
Run Code Online (Sandbox Code Playgroud)
所以在每两个元素之后添加一个额外的换行符。
我正在编写一些代码来从 Excel 文件复制数据,但无法让它工作。
任何帮助将不胜感激。
下面使用的代码不起作用:
pyautogui.hotkey('ctrl', 'shift', 'end')
Run Code Online (Sandbox Code Playgroud)
或者
pyautogui.press('ctrl')
pyautogui.press('shift')
pyautogui.press('end')
pyautogui.release('ctrl')
pyautogui.release('shift')
pyautogui.release('end')
Run Code Online (Sandbox Code Playgroud)
还
pyautogui.keyDown('ctrl')
pyautogui.keyDown('shift')
pyautogui.keyDown('end')
pyautogui.keyUp('ctrl')
pyautogui.keyUp('shift')
pyautogui.keyUp('end')
Run Code Online (Sandbox Code Playgroud) 我想反转列表的中间部分:
ls = [1,2,3,4,5,6,7,8,9,10]
[1,2,3,7,6,5,4,8,9,10]
Run Code Online (Sandbox Code Playgroud)
如您所见,开始时三个元素[1,2,3]都不会颠倒,而在结尾[8,9,10]中三个元素也都不会颠倒。
但是我只是想不出如何用“优雅”来做到这一点。我尝试了很多组合,例如:
ls2 = ls[:4] + ls[::-1] + ls[:-4]
Run Code Online (Sandbox Code Playgroud)
但我正在重复。任何帮助,将不胜感激!
python ×7
python-3.x ×3
perl ×2
anagram ×1
arrays ×1
list ×1
pandas ×1
python-2.7 ×1
sorting ×1