小编hja*_*mig的帖子

MATLAB - 创建2d独特对数组的最佳方法?

在matlab中创建10x2矩阵的最佳方法是什么,其中每个元素是1-5之间的随机int,因此这个数组中只有唯一的元素对?我知道randperm可以给我一个随机的唯一数字,但我不确定是否有可能使用randperm给出独特的对?我能想到的另一种方法是使用:

randi([1 5], 10, 2);
Run Code Online (Sandbox Code Playgroud)

在一个带有if语句的循环中,检查所有对是否都是唯一的.我想要的数据示例如下:

4 5
1 3
2 2
1 4
3 3
5 1
5 5
2 1
3 1
4 3
Run Code Online (Sandbox Code Playgroud)

注意:元素的顺序无关紧要,例如,4,5和5,4都是有效的.

arrays random matlab

7
推荐指数
1
解决办法
311
查看次数

sort:字符串比较失败无效或不完整的多字节或宽字符

我正在尝试在文本文件上使用以下命令:

$ sort <m.txt | uniq -c | sort -nr >m.dict 
Run Code Online (Sandbox Code Playgroud)

但是,我收到以下错误消息:

sort: string comparison failed: Invalid or incomplete multibyte or wide character
sort: Set LC_ALL='C' to work around the problem.
sort: The strings compared were ‘enwedig\r’ and ‘mwy\r’.
Run Code Online (Sandbox Code Playgroud)

我在Windows 7上使用Cygwin,并且在编辑m.txt时遇到麻烦,将文件中的每个单词放在一个新行上.请参阅:

使用AWK将每个单词放在新行的文本文件中

我不确定我是否因此而收到这些错误,或者因为m.txt包含来自威尔士语字母的字符(当我在Python中使用威尔士文本时,我需要将编码更改为'Latin-1 ").

我尝试按照错误消息的建议并更改LC_ALL ='C'然而这没有帮助.任何人都可以详细说明我收到的错误,并提供有关如何尝试解决此问题的任何建议.

更新:

尝试使用dos2unix时,会在某些行显示有关无效字符的错误.事实证明这些不是威尔士人物,而是其他奇怪的人物(箭头等).我查看了删除这些字符的文本文件,直到我能够无错误地使用dos2unix命令.但是,在使用dos2unix命令后,所有文本都连接在一起(没有空格/换行符或任何内容,而应该是这样,文件中的每个单词都在单独的行上)然后我使用unix2dos并且文本文件恢复正常.如何在每个单词的各个单词上使用sort命令而不会给出错误的'\ r'字符?

unix sorting string cygwin uniq

6
推荐指数
2
解决办法
4444
查看次数

Python - 减小文件大小的程序正在增加文件大小

对于大学,我正在研究压缩技术.我正在尝试执行的一项实验是用一个字符替换某些威尔士语字母(有双向图).

我认为用一个字符替换两个字符会减小文件大小(无论多少),或者最坏的情况是保持文件大小相同.我已经制作了一个Python脚本来实现这一点,但实际上它增加了文件大小.我测试过的原始文件是~74,400KB,输出程序是~74,700KB.

这是我的Python代码:

replacements = {
        'ch':'ƒ',
        'Ch':'†',
        'CH':'‡',
        'dd':'Œ',
        'Dd':'•',
        'DD':'œ',
        'ff':'¤',
        'Ff':'¦',
        'FF':'§',
        'ng':'±',
        'Ng':'µ',
        'NG':'¶',
        'll':'º',
        'Ll':'¿',
        'LL':'Æ',
        'ph':'Ç',
        'Ph':'Ð',
        'PH':'×',
        'rh':'Ø',
        'Rh':'Þ',
        'RH':'ß',
        'th':'æ',
        'Th':'ç',
        'TH':'ð',
        }
print("Input file location: ")
inLoc = input("> ")
print("Output file location: ")
outLoc = input("> ")

with open(inLoc, "r",encoding="Latin-1") as infile, open(outLoc, "w", encoding="utf-8") as outfile:
for line in infile:
    for src, target in replacements.items():
        line = line.replace(src, target)
    outfile.write(line)
Run Code Online (Sandbox Code Playgroud)

当我在一个非常小的文本文件上测试它几行时,我查看输出,它是按预期的.

INPUT.TXT:

Lle wyt ti heddiw?

Ddoe es i …
Run Code Online (Sandbox Code Playgroud)

python

3
推荐指数
1
解决办法
622
查看次数

Python-迭代和更改列表的元素

我刚刚开始学习python,但是在迭代和if语句方面遇到了麻烦。

我有一个清单d

d = ['s','u','p','e','r','c','a','l','i','f','r','a','g','i','l','i','s','t','i','c','e','x','p','i','a','l','i','d','o','c','i','o','u','s']
Run Code Online (Sandbox Code Playgroud)

我需要遍历该列表,检查每一个元素是否等于一元音(aeiou)。如果是元音,则必须将该元素替换'vowel'为字母前有单词的子列表。例如,如果'a'检测到,它将替换为['vowel', 'a']

到目前为止,尽管我知道这是错误的,但这是我设法想到的:

for items in d:
    if items == 'a':
        d[items:items] = ['vowel', 'a']
Run Code Online (Sandbox Code Playgroud)

python loops list

2
推荐指数
1
解决办法
289
查看次数

从列表中删除元素,使用另一个列表作为索引

我有一个列表primeList和另一个列表ls.primeList是一个完整的列表,我需要删除ls索引所在的值primeList.

例如,如果primelist = [1, 3 , 5]ls = [1, 2, 3, 4, 5, 6, 7],则应删除索引1,3和5 ls,制作ls = [1, 3, 5, 7]

目前我正在尝试使用这段代码:

primeList = list(getPrimes())
    ls = list(ls)
    for i in primeList:
        del ls[i]
    return ls
Run Code Online (Sandbox Code Playgroud)

这给了我以下错误:

Traceback (most recent call last):
File "C:/Python34/My Scripts/NLP lab2/exec2.py", line 26, in <module>
otherList = delPrimes(myList)
File "C:/Python34/My Scripts/NLP lab2/exec2.py", line 18, in delPrimes
del ls[i]
IndexError: list assignment …
Run Code Online (Sandbox Code Playgroud)

python loops

1
推荐指数
1
解决办法
1190
查看次数

Python - 检查字符串是否包含列表中的任何元素

我需要检查字符串是否包含列表的任何元素.我目前正在使用这种方法:

engWords = ["the", "a", "and", "of", "be", "that", "have", "it", "for", "not"]
engSentence = "the dogs fur is black and white"

print("the english sentence is: " + engSentence)

engWords2 = []
isEnglish = 0

for w in engWords:
    if w in engSentence:
        isEnglish = 1
        engWords2.append(w)

if isEnglish == 1:
    print("The sentence is english and contains the words: ")
    print(engWords2)
Run Code Online (Sandbox Code Playgroud)

这个问题是它给出了输出:

the english sentence is: the dogs fur is black and white
The sentence is english and contains the …
Run Code Online (Sandbox Code Playgroud)

python regex

0
推荐指数
1
解决办法
911
查看次数

标签 统计

python ×4

loops ×2

arrays ×1

cygwin ×1

list ×1

matlab ×1

random ×1

regex ×1

sorting ×1

string ×1

uniq ×1

unix ×1