create list of increasing number of repeated characters [closed] - python

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I'm trying to create this kind of output in Python
["k", "kk", "kkk", "kkkk", ...]
["rep", "reprep", "repreprep", ...]
That is a list of n elements, made of the same character (or small group of characters) repeated X times, X being increased by one for each element.
I can't find a way to do this easily, without loops..
Thanks,

Here you have a generator using itertools.count, remember the property of "multiplying" strings in python by a number, where they will be replicated and concatenated nth times, where for example "a"*3 == "aaa" :
import itertools
def genSeq(item):
yield from (item*i for i in itertools.count())
Here you have a live example

repeating_value = "k" #Assign the value which do you want to be repeated
total_times=5 #how many times do you want
expected_list=[repeating_value*i for i in range(1,total_times+1)]
print(expected_list)

character = 'k'
_range = 5
output = [k*x for k in character for x in range(1, _range + 1)]
print(output)
I would multiple my character by a specified number in the range, and then I would simply iterate through the range in a list comprehension. We add 1 to the end of the range in order to get the full range.
Here is your output:
['k', 'kk', 'kkk', 'kkkk', 'kkkkk']

The following is by far the easiest which I have built upon the comment by the user3483203 which eliminates initial empty value.
var = 'rep'
list = [var * i for i in range(1,x,1)]
print(list)

Related

Python: Help me understand how it can be possible [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed last year.
Improve this question
Please help me understand how key variable can have a value a or b?
from collections import defaultdict
dict = defaultdict(list)
group_a_count, group_b_count = map(int, input().split())
for i in range(1, group_a_count + 1):
dict[input()].append(i)
for i in range(1, group_b_count + 1):
key = input()
print(key)
INPUT:
5 2
a
a
b
a
b
a
b
OUTPUT:
a
b
All input values was appended to dictonary in first cycle. And how second cycle understood where to get keys from the dictonary?
It's a bit messy code. The only thing the second loop does, is asking group_b_count times an input, which it then prints and continues with the next iteration... So the output is a and b because of this piece of code:
key = input()
print(key)
which just prints what you gave as input
What's the purpose of this program actually?
And if i use for cycle with this parameters:
for i in range(1, 5):
key = input()
print(key)
in STDOUT will be still a and b. A full purpose of program: "In this challenge, you will be given 2 integers, n and m. There are n words, which might repeat, in word group A. There are m words belonging to word group B. For each m words, check whether the word has appeared in group A or not. Print the indices of each occurrence of m in group A. If it does not appear, print -1."
I misunderstood task from HackerRank and I expected another output.

Python - How to keep smallest number (converting from string to int) in list? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
If I have a list of elements such as:
items = ["058529-08704-200280", "058529-08704-230330", "058529-08704-140200", "058529-08704-290390",
"058529-08705-140200", "058529-08705-230330", "058529-08704-170240", "058529-08705-290390",
"058529-08705-170240"]
I want to keep the elements with the smallest number after the second " - ". However, they must be compared with the elements which have the same first two numbers in the string.
For e.g. the strings which start with 058529-08704, the smallest number is 058529-08704-140200 and for 058529-08705, the smallest number is 058529-08705-140200
So the final list must end up with ["058529-08704-140200", "058529-08705-140200"].
What is the most pythonic way to achieve this instead of having to write multiple ifs or using string manipulation?
items = ["058529-08704-200280", "058529-08704-230330", "058529-08704-140200", "058529-08704-290390",
"058529-08705-140200", "058529-08705-230330", "058529-08704-170240", "058529-08705-290390",
"058529-08705-170240"]
lst_3th_num = []
for item in items:
lst_3th_num.append(int(item.split('-')[2]))
result = []
for item in items:
if int(item.split('-')[2]) == min(int(s) for s in lst_3th_num):
result.append(item)
print(result)

find 'x' examples of match string in list [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
I try to find best way to find x examples of string in long list:
List = [123xyz, 456xyz, 678xyz, 123abc, 123ok, 123yes, 456abc, 456ok, noyes, yesno, yes123]
and i want to find all pair with '123'* or all pair of *'abc' but minimum pair 'x'
example '123'* x=3 : 123xyz, 123abc, 123ok
example *'abc' x =2 : 123abc, 456abc
someone have idea or examples of code to find this on list?
Your examples suggest that you're looking for a maximum of x number of matches (not a minimum).
If you're only looking to match on prefixes and suffixes, you could use the startswith() and endswith() functions.
In order to limit the result to a specific number of matches, you can simply use a range index
for example:
# match "123*" x=3
result = [ s for s in List if s.startswith("123") ][:3]
# match "*abc" x=2
result = [ s for s in List if s.endswith("abc") ][:2]

Printing and array two times in python [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I have a list a=['abc','cdv','fasdf'] and also have a constant n which says the amount of time print each elements two times.
For example, n=2 should return a=['abc','abc','cdv','cdv']; or n=4 will return a=['abc','abc','cdv','cdv','fasdf','fasdf','abc','abc'].
Here is one way using itertools.chain and a generator comprehension:
from itertools import chain
a = ['abc','cdv','fasdf']
n = 4
res = list(chain.from_iterable([a[i % len(a)]]*2 for i in range(n)))
# ['abc', 'abc', 'cdv', 'cdv', 'fasdf', 'fasdf', 'abc', 'abc']
it looks like you'll need to recycle elements if n is larger than the length of the list. An easy way to deal with this is to duplicated the array as many times as needed.
import math
n_over = math.ceil(len(a)/n)
n_reps = 1 + n_over
a_long = a * n_reps
we can iterate over the new array to build the new one
a_rep = []
for e in a_long[0:n]:
a_new += [e]*n

Python names in a list [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
So I'm kinda new to python and was given a problem as follows:
Given the list names , find the largest element in the list and swap it with the last element. For example, the list ["Carlton", "Quincy" "Adam", "Bernard"] would become ["Carlton", "Bernard", "Adam", "Quincy"] . Assume names is not empty
I thought about doing list Comprehension but I don't know how I would write that out in code
EDIT: Largest in this case would be the length of the string (sorry for not clarifying!!!))
names = [foo, fooooo, bar, baaar]
a, b = i.index(max(name, key=len)), -1
i[b], i[a] = i[a], i[b]
Courtesy of this.
If by largest, you mean longest, then you could iterate over the list to find the longest name, and then swap them.
maxLen = 0
maxI = 0
for i in range(0, len(names)):
if len(names[i]) > maxLen:
maxLen = len(names[i])
maxI = i
temp = names[-1]
names[-1] = names[maxI]
names[maxI] = temp
This is an overly convoluted way of doing it, but it's so drawn out to make it more obvious as to what's going on. With that said, you really should be more specific about what "largest" means.

Categories