+ 1
replace values from list.
# Input : [2,1,2,3,0,2] and 3 # Output : [33,3,33,333,0,33] n = 3 Output = [ ] for item in input: Output.append(int(str(item)*n)) print (Output)
4 odpowiedzi
+ 3
Assuming the list is named <lst> and the number is <num>
- Read <num> as string
- Iterate through the items in list <lst> using enumerate() function
- For each element <el> in iteration ...
- Create a new string <res> a result from multiplying <num> by <el>
- Convert <res> to integer by int() if you want the list items as numbers
- Update <el> value by <res>
Go give it a try 👍
You can also use list comprehension to accomplish this
+ 3
Manoj Bhaagam,
That was pretty close 👍
lst = [ 2, 1, 2, 3, 0, 2 ]
n = 3
Output = []
for item in lst:
if item != 0:
Output.append( int( str( n ) * item ) )
else:
Output.append( item )
print(Output)
+ 2
numlist = [2,1,2,3,0,2]
n = 3
multiple = [int(i*str(n)) if i else i for i in numlist]
print(multiple)
0
Where's your try?