我有一个包含新行的列表,我喜欢将其转换为数组,例如
JAN
FEB
MAR
APR
MAY
Run Code Online (Sandbox Code Playgroud)
成 ["JAN", "FEB", "MAR", "APR", "MAY]
任何帮助将不胜感激.谢谢
这样的东西似乎不起作用(text_file.txt包含上面的月份列表)
file = File.new("text_file.txt", "r")
while (line = file.gets)
line.chomp
list = line.split(/\n/)
puts "#{list}"
end
Run Code Online (Sandbox Code Playgroud) 我有一个字符串,并希望在不同的位置替换多个字符并打印该字符串.
例如
在这里,我喜欢用string_replace替换字符串.
string = "AGACACTTTATATGTAT"
positions = ["2", "5", "8", "10"]
string_replace = ["T", "A", "G", "G"]
Run Code Online (Sandbox Code Playgroud)
我需要的输出是=>"AGTCAATTGAGATGTAT"
我试过这个但没有成功:
positions.zip(string_replace).each do |pos, str|
string.gsub!(/#{string}[#{pos}]/, '#{str}')
puts string
end
Run Code Online (Sandbox Code Playgroud)
任何帮助将不胜感激.
我创建了一个字典,我已将列表的第一个元素指定为键.我想将其余的元素作为值附加到字典中:
file_a包含制表符分隔的字段.
a = {}
for line in file_a.readlines():
split_line = line.strip().split('\t')
a[split_line[0]] = []
a[split_line[0]].append(split_line[::-1]) # append from second to last element to the list
Run Code Online (Sandbox Code Playgroud)
:: - 1附加所有元素.我需要追加除第一个之外的所有元素(因为它用作键).任何帮助将不胜感激.
例如,如果字段是:X\t1\t2\t3我希望哈希是:
'X': ['1', '2', '3']
Run Code Online (Sandbox Code Playgroud) 如何强制特定键始终位于字典的开头.例如,如果我有这样的词典列表:
source = [{'age':39, 'name':'Homer','gender':'male', 'IQ':0}, {'age':10, 'gender':'male', 'IQ':0,'name':'Bart'}]
Run Code Online (Sandbox Code Playgroud)
需要'name'始终是dict中的第一个键:
source = [{'name':'Homer','age':39, 'gender':'male', 'IQ':0}, {'name':'Bart','age':10, 'gender':'male','IQ':0}]
Run Code Online (Sandbox Code Playgroud)
我试图使用运算符,但是按字母顺序命令名称的值,这不是我想要的.
from operator import itemgetter
newlist = source.sort(key=operator.itemgetter('name'))
Run Code Online (Sandbox Code Playgroud) 我有一个核心函数,我从我的脚本中的许多其他函数调用.问题是当我调用核心函数来运行它时我不想要每个函数.有没有办法存储核心功能的输出,以便当它被调用第二次,第三次等时它不运行?
例如
def core_func(a,b,c):
do something....
return x,y,z
def func2(a,b,c):
x,y,z = core_func(a,b,c)
do something with x,y,z
def func3(a,b,c):
x,y,z = core_func(a,b,c)
do something with x,y,z
Run Code Online (Sandbox Code Playgroud)
等等..
在func2调用它之后,func3会再次调用core_func.我怎样才能防止这种情况但同时使用core_func输出?一个可能的解决方案可能是从func2返回输出并在func3中使用(但这会有点难看).
谢谢
我试图遍历文件中的每一行,用逗号分隔并用它做一些事情.重要的是,我希望将更改写入同一文件.此代码不会出错,但会挂起并且不执行任何操作.任何建议或更好的解决方案
file='file.txt'
while IFS=',' read -ra f1 f2
do
echo "field # 1 : $f1 ==> field #2 : $f2"
done >> $file
Run Code Online (Sandbox Code Playgroud) 我编写了一个ruby脚本,使用ftp/net从ftp站点检索数据.除了尝试下载文件本身时超时的最后阶段,一切似乎都运行良好.任何想法为什么会这样?'samples'现在有一个目录用于测试目的.
ftp = Net::FTP.new('ftp.sra.ebi.ac.uk')
ftp.login
puts "connected!"
errors = []
samples = ["SRR016000"]
samples.each do |sample|
files = ftp.chdir("vol1/fastq/SRR016/#{sample}/")
puts "changed directory"
#files = ftp.list('SRR*')
begin
Timeout.timeout(20) do
ftp.getbinaryfile("#{sample}_1.fastq.gz")
end
rescue Timeout::Error
errors << "File download timed out for: #{sample}"
puts errors.last
end
end
ftp.close
puts "All done!"
Run Code Online (Sandbox Code Playgroud) python ×3
ruby ×3
list ×2
arrays ×1
bash ×1
call ×1
dictionary ×1
file-io ×1
ftp ×1
function ×1
indexing ×1
position ×1
split ×1
substitution ×1
while-loop ×1