如果是输入
round_robin(range(5), "hello")
Run Code Online (Sandbox Code Playgroud)
我需要o/p as
[0, 'h', 1, 'e', 2, 'l', 3, 'l', 4, 'o']
Run Code Online (Sandbox Code Playgroud)
我试过了
def round_robin(*seqs):
list1=[]
length=len(seqs)
list1= cycle(iter(items).__name__ for items in seqs)
while length:
try:
for x in list1:
yield x
except StopIteration:
length -= 1
pass
Run Code Online (Sandbox Code Playgroud)
但它给出了错误
AttributeError: 'listiterator' object has no attribute '__name__'
Run Code Online (Sandbox Code Playgroud)
如何修改代码以获得所需的o/p
如何返回前n个最常出现的字符及其各自的计数#例如'aaaaaabbbbcccc'
,2
应该[('a', 6), ('b', 4)]
在python中返回
我试过这个
def top_chars(input, n):
list1=list(input)
list3=[]
list2=[]
list4=[]
set1=set(list1)
list2=list(set1)
def count(item):
count=0
for x in input:
if x in input:
count+=item.count(x)
list3.append(count)
return count
list2.sort(key=count)
list3.sort()
list4=list(zip(list2,list3))
list4.reverse()
list4.sort(key=lambda list4: ((list4[1]),(list4[0])), reverse=True)
return list4[0:n]
pass
Run Code Online (Sandbox Code Playgroud)
但它不适用于输入("aabc",2)它应该给出的输出是
[('a', 2), ('b', 1)]
Run Code Online (Sandbox Code Playgroud)
但我得到的输出是
[('a', 2), ('c', 1)]
Run Code Online (Sandbox Code Playgroud) 我有一个近1500行的文件,其中包含")(()(&& ^%&^ a%@%^%*&^"等符号以及整个文件中的一些两个或三个字母.
如何在此类文件中搜索这些字母并在o/p屏幕上显示找到的字母.