Randomly change character in python list [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 1 year ago.
Improve this question
I have sequences generated by python code like this:
TTTTTAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
I would like to iterate through them, and change every character with certain % to N, like 0.2%, otherwise leave it to the original character. How can i do it?

You can use random int for this, like this:
import random
for i in range(len(your_list)):
if random.randint(0,1000)<2: #0.2%chance
your_list[i] = 'N'

Use random.random() in a generator expression and join back to a string.
import random
s = "TTTTTAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
p = 0.002
s2 = "".join("N" if random.random() <= p else c for c in s)

Related

sum integer part of the alphanumeric list 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 6 days ago.
Improve this question
I have a list like below
l1 = ['a10','b2','a2','c1','b4','c5']
and I want to sum of numeric and output like below
l2 = ['a12','b6','c7'].
Note: do not use in-built function
from collections import Counter
l1 = ['a10','b2','a2','c1','b4','c5']
l2 = [f'{k}{v}' for (k, v) in Counter(''.join(i[0]*int(i[1:]) for i in l1)).items()]
# ['a12', 'b6', 'c6']
Figuring this out is left as an exercise to the reader.
It also assumes the alpha part is always just the first character.

Turn a string of a list to an integer 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 1 year ago.
Improve this question
I have an output of a = "[1,2,3]", how do I convert it to an array a = [1,2,3] from the string in Python?
Thanks!
Try this:
import json
result = json.loads("[1,2,3]")
print(result)
Use ast and then ast.literal_eval()
import ast
print(ast.literal_eval(x))
You can do it without importing stuff
lst = []
for index in range(len(a)):
if a[index].isdigit():
lst.append(int(a[index]))
Then lst will be a list containing all the integers in string a. And if you want to keep the variable name "a", you can
a = lst [::]

How to use multiple instances of a variable next to each other? [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 5 years ago.
Improve this question
I don't really know how to describe it any better, but I need to write a program that answers this question:
Write a loop that computes the value of a + aa + aaa + aaaa with a given digit as the value of a.
Any advice?
Shortest I could think of
digit = "1"
result = sum([int("{}".format(digit * x)) for x in [1,2,3]])
print(result)
# 123
This repeats the string (sic!) x times, converts the result to an integer and sums the parts up to result.
Here's an approach using a (slightly complicated) list comprehension:
number = 1
length = 4
sum(int(str(number) * i) for i in range(1,length+1))

Python - Split list of coordinates from one big String [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 5 years ago.
Improve this question
I have a .txt file containing a list of lists, each list is a set of coordinates:
[[151.22999572753906, -33.84008789062494], [..., ...],... [..,..]]
I want to know how I can read this file as an array and not as a string so I can easily extract all the coordinates I need.
My code so far:
import re
d = '[[151.22999572753906, -33.84008789062494], [151.22999572753906, -33.84008789062494][151.22999572753906, -33.84008789062494]]'
##l = re.split('[\[\]]', d)
l = re.split('\]\[', d)
print(l)
>>>['[[151.22999572753906, -33.84008789062494], [151.22999572753906, -33.84008789062494', '151.22999572753906, -33.84008789062494]]']
It looks like the contents of your file happen to be valid JSON. If you're sure the format isn't going to vary, you can just use json.load
import json
json.loads('[[151.22999572753906, -33.84008789062494]]')
# or
json.load(open('/path/to/your.txt'))
[[151.22999572753906, -33.84008789062494]]
Start with
PSEUDOCODE
with open(.txt)
n = 0
while True
line = readline
n += 1
print(n , line)
How many Lines did you read?

How do I convert string to list 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 6 years ago.
Improve this question
How do I convert def stringParametre(x) x to a list so that if x is hello then it will become a list like ["H","E","l","l","O"] . Capitals don't matter .
Note that you do not have to convert to a list if all you want to do is to iterate over the characters of the string:
for c in "hello":
# do something with c
works
Building on #idjaw's comment:
def stringParametre(x):
return list(x)
Of course this will have an error if x is not a string (or other sequence type).
list(x)
OR
mylist = []
for c in x:
mylist.append(c)

Categories

Resources