两个矩阵,A和B:
A = [1 2 3
9 7 5
4 9 4
1 4 7]
B = [1 2 3
1 4 7]
Run Code Online (Sandbox Code Playgroud)
矩阵B中的所有行矩阵A的成员我想删除一个从A共同行和B不排序.
我尝试过setdiff(),但这会对输出进行排序.
对于我的特定问题(蛋白质结构中的原子坐标),保持行的有序完整性很重要.
我希望与不等长的嵌套列表进行比较.我只对每个子列表的第一个元素之间的匹配感兴趣.如果存在匹配,我希望将匹配添加到另一个列表,以便随后转换为制表符分隔文件.以下是我正在使用的示例:
x = [['1', 'a', 'b'], ['2', 'c', 'd']]
y = [['1', 'z', 'x'], ['4', 'z', 'x']]
match = []
def find_match():
for i in x:
for j in y:
if i[0] == j[0]:
match.append(j)
return match
Run Code Online (Sandbox Code Playgroud)
返回:
[['1', 'x'], ['1', 'y'], ['1', 'x'], ['1', 'y'], ['1', 'z', 'x']]
Run Code Online (Sandbox Code Playgroud)
重新处理列表以删除重复项是一种好的做法还是可以以更简单的方式完成?
另外,为了进行比较,使用元组和/或元组元组是否更好?
任何帮助是极大的赞赏.
此致,Seafoid.
假设您有一个定义了多个功能的程序.每个函数都在一个单独的for循环中调用.是否可以通过命令行指定应该调用哪个函数?
例:
python prog.py -x <<<filname>>>
Run Code Online (Sandbox Code Playgroud)
其中-x告诉python转到特定的for循环然后执行for循环中调用的函数?
谢谢,Seafoid.
我目前正在努力从python脚本调用非python程序.
我有一个〜1000个文件,当通过这个C++程序时将生成~1000个输出.每个输出文件必须具有不同的名称.
我希望运行的命令是以下形式:
program_name -input -output -o1 -o2 -o3
Run Code Online (Sandbox Code Playgroud)
到目前为止,我尝试过:
import os
cwd = os.getcwd()
files = os.listdir(cwd)
required_files = []
for i in file:
if i.endswith('.ttp'):
required_files.append(i)
Run Code Online (Sandbox Code Playgroud)
所以,我有一系列必要的文件.我的问题 - 我如何迭代数组和每个条目,将其作为参数传递给上面的命令(program_name)并为每个文件指定唯一的输出ID?
我有一个带制表符分隔格式的文件,带有尾随换行符,例如,
123 abc
456 def
789 ghi
Run Code Online (Sandbox Code Playgroud)
我希望编写函数将文件的内容转换为嵌套列表.到目前为止,我尝试过:
def ls_platform_ann():
keyword = []
for line in open( "file", "r" ).readlines():
for value in line.split():
keyword.append(value)
Run Code Online (Sandbox Code Playgroud)
和
def nested_list_input():
nested_list = []
for line in open("file", "r").readlines():
for entry in line.strip().split():
nested_list.append(entry)
print nested_list
Run Code Online (Sandbox Code Playgroud)
.
前者创建一个嵌套列表,但包含\n和\ t字符.后者不是一个嵌套列表,而是没有\n和\ t字符的许多等效列表.
有人帮吗?
问候,S ;-)
我有一个包含约2000万行(约1.5GB)的文件。每行的格式为:
entry_1 entry_2 entry_3 ......... entry_5
Run Code Online (Sandbox Code Playgroud)
该文件包含重复项,但格式为:
entry_2 entry_1 entry_3 ......... entry_5
Run Code Online (Sandbox Code Playgroud)
某些行的内容相同,但是前两个元素经常(可能总是)切换。
有人对如何从这种大小的文件中删除这种性质的副本有任何建议吗?
谢谢。
假设一个矩阵:
> a <- matrix(c(100, 90, 80, 20), 2, 2)
> a
[,1] [,2]
[1,] 100 80
[2,] 90 20
Run Code Online (Sandbox Code Playgroud)
假设我想将矩阵的元素转换为rank:
>rank.a <- rank(a)
> rank.a
[1] 4 3 2 1
Run Code Online (Sandbox Code Playgroud)
这返回一个向量,即矩阵结构丢失.是否可以对矩阵进行排序,使输出具有以下形式:
[,1] [,2]
[1,] 4 2
[2,] 3 1
Run Code Online (Sandbox Code Playgroud) 如何忽略文件中的行?
例:
如果您知道文件中的第一行将以say,a或b开头,其余行以c结尾,那么如何解析文件以便忽略以a或b开头的行,并将结束c的行转换为a嵌套列表?
到目前为止我所拥有的:
fname = raw_input('Enter file name: ')
z = open(fname, 'r')
#I tried this but it converts all lines to a nested list
z_list = [i.strip().split() for i in z]
Run Code Online (Sandbox Code Playgroud)
我猜我需要一个for循环.
for line in z:
if line[0] == 'a':
pass
if line[0] == 'b':
pass
if line[-1] == 'c':
list_1 = [line.strip().split()]
Run Code Online (Sandbox Code Playgroud)
以上是一般的想法,但我是制作死代码的专家!如何渲染它不死?
谢谢,Seafoid.
我在下面的代码中遇到了一些问题:
输入:li是嵌套列表,如下所示:
li = [['>0123456789 mouse gene 1\n', 'ATGTTGGGTT/CTTAGTTG\n', 'ATGGGGTTCCT/A\n'], ['>9876543210 mouse gene 2\n', 'ATTTGGTTTCCT\n', 'ATTCAATTTTAAGGGGGGGG\n']]
Run Code Online (Sandbox Code Playgroud)
使用下面的函数,我所需的输出只是'>'后的第2到第9位,条件是整个子列表中的'/'数> 1.
相反,我的代码给出了所有条目的数字.而且,它给了他们多次.因此,我认为我的计数器和我的for循环有问题.我无法弄清楚这一点.
任何帮助,非常感谢.
import os
cwd = os.getcwd()
def func_one():
outp = open('something.txt', 'w') #output file
li = []
for i in os.listdir(cwd):
if i.endswith('.ext'):
inp = open(i, 'r').readlines()
li.append(inp)
count = 0
lis = []
for i in li:
for j in i:
for k in j[1:] #ignore first entry in sublist
if k == '/':
count += 1
if count > …Run Code Online (Sandbox Code Playgroud) 我有一个包含n行和4列的数组.行上的四个条目中的每一个都是整数,即
X = [
111 112 432 2
6 9 115 111
112 432 111 2
];
Run Code Online (Sandbox Code Playgroud)
每行代表四面体的顶点.这些顶点因此没有方向性,在上述情况下,由X(1,:)和X(3,:)表示的四面体是等价的.
我希望从X中删除重复的四面体,但是不能完全理解如何将顺序独立性合并到我的代码中.
我尝试了UNIQUE()函数,但这会返回一个唯一整数的(nx1)数组,即
Y = UNIQUE(X);
Y = [
2
6
9
111
112
115
432
]
Run Code Online (Sandbox Code Playgroud)
有人建议以合理有效的方式完成这项任务吗?
谢谢,S :-)