我读取数据并使用以下代码对其进行处理:
data = pd.read_csv('Step1_output.csv')
data = data.sample(frac=1).reset_index(drop=True)
data1 = pd.DataFrame(data, columns=['Res_pair'])
# creating instance of labelencoder
labelencoder = LabelEncoder()
# Assigning numerical values and storing in another column
import os
import string
os.chdir('C:\Python27')
x=os.listdir('C:\Python27')
y=[f for f in os.listdir(dirname)
if os.path.isfile(os.path.join(dirname, f))]
for k in y:
fileName, fileExtension = os.path.splitext(k)
print fileName,fileExtension
现在,我想按扩展名对文件进行排序。
我相应地列出了一千个文件: 1_r.txt
2_r.txt
3_r.tet
...
999_r.txt 其中r是随机长度的随机字母数字。我需要用python按数字顺序遍历这些文件。我想使用类似这样的东西: count = 1
for i in os.listdir():
if i.startswith(str(count) + '_') == True:
# do stuff
print(i) 但是,上面的方法不会遍历字典。我想使用sorted方法,但是我的文件并不是只按编号排序的。请帮帮忙。
我正在写一个Python程序,我需要计算文本文件中每个单词的数量。
def count_words(word,d):
for l in word:
if l in d:
d[l] += 1
else:
d[l] = 1
return d
def count_letters():
d = dict()
word_file = open('w.txt')
for line in word_file:
word = line.strip()
我希望使用python按字母顺序对给定的字符串数组进行排序,但是小写单词应该首先出现。
举个例子:
#!/usr/local/bin/python2.7
arr=['A','e','a','D','f','B']
arr.sort()
for s in arr: print s
输入:
A
e
a
D
f
B
产出(当前):
A
B
D
a
e
f
产出(应该是):
a
e
f
A
B
D
我有一个Python (3.7)脚本,它获取目录中所有文件的列表,并在for循环中遍历它们。
我要做的是确保只列出XML文件,并按照创建日期/时间或文件名中的编号对文件列表进行排序。
目前使用:
files = [f for f in os.listdir(self.config['report_directory']) if os.path.isfile(os.path.join(self.config['report_directory'], f))]
上面的方法可以很好地获得目录中的文件列表,但是我还无法按文件创建日期或文件名中的数字来列出它。
文件名示例
为了学习Python 3,我正在Hackerrank中做一些练习。
在任务中,您将得到一个只包含小写英语字符的字符串,您需要在该字符串中找到前三个最常见的字符。
我遇到了一些问题。
我对这个问题的解决办法如下:
#!/bin/python3
import sys
if __name__ == "__main__":
s = input().strip()
ch_dict = {}
for ch in s:
if ch in ch_dict : ch_dict[ch] +=1
else: ch_dict[ch] = 1
result =
如何在python中打印运行时输入的两个数字之间的列表中的数字。
例如:
lst = [23, 5, 7, 8, 90, 67, 90, 0]
first = int(input('Enter Your Number:'))
second = int(input('Enter Your Number:'))
first = 7
second = 67
答案是8,23
lst = [23, 5, 7, 8, 90, 67, 90, 0]
first = int(input('Enter Your Number:'))
second = i
你好,我试图将列表中的所有零推送到一边,而不更改其余的:
[0,2,0,0,9] -> [0,0,0,2,9]
[3,4,0,1,0] -> [0,0,3,4,1]
我的解决方案:
def getNonZeros(self, tup):
for i in tup:
if i != 0:
yield i;
def pushZeros(self, tup, side):
if side==Side.End:
return [i for i in self.getNonZeros(tup)] + [0]*tup.c