python


How can I merge these two lines of code into one (Python 3.X)?


text = ''.join(sorted([x for x in input()]))
text = text.replace('+', '', text.count('+'))
I just it love it when you can do all sort of things in one line with Python.
text = ''.join(sorted(input())).replace('+', '')
OR
text = ''.join(sorted(input().replace('+', '')))
You don't need to use list comprehension. Just pass the input() to sorted(); sorted support any iterable.
the 3rd argument to str.replace() is redundant. Because the code is replacing all occurences of +.
Ok, this is not exactly the same code, but in this case result is similar:
text = ''.join(sorted([x for x in input() if x != '+']))
Instead of creating whole string and then replacing one character, you can simply remove it in first list comperhesion.

Related Links

Recursive function with one argument that does something to each element
Storing and Graphing results of API queried via Python. [on hold]
While handling Exceptions, how to print any error statement that occurs? See below for more details
Having trouble getting my calculator app to print the result I want
Why am I getting this ValueError?
How to recognize single digit in string to insert leading zero?
How can I configure a test environment with Falcon
Computing the gradient for a custom TensorFlow op with py_func()
get all files in drive via REST API
Setting an image as a tkinter window background
Displaying A File Using StringVar() // Tkinter Python 3.5
How are features ranked in RFECV in scikit learn(sklearn)?
Solr requests hang when started via python subprocess
How to extract internet email headers from outlook emails?
Tensorboard get blank page
Python Matplotlib Streamplot providing start points

Categories

HOME
tensorflow
vhdl
warnings
google-tag-manager
jbehave
json-ld
visualization
ruby-on-rails-3
bibtex
sbt-assembly
rfc
recyclerview
webdav
defragmentation
algorithmic-trading
wysiwyg
simple-injector
netezza
mule-studio
phpbb
rhapsody
android-toolbar
yeoman-generator
jogl
pycrypto
avplayeritem
delete-file
tflearn
jflex
shopping-cart
kong
codelite
reactiveui
forum
expand
rhomobile
npm-shrinkwrap
reactivemongo
text-files
redhat-datavirt
readfile
sonarqube-scan
active-model-serializers
cross-entropy
intellij-idea-2016
homekit
dojox.grid.datagrid
uiviewpropertyanimator
gauss
spell-checking
dwarf
openh264
festival
emgu
gradle-script-kotlin
spring-ioc
aescryptoserviceprovider
spoofing
multifile-uploader
database-backups
kendo-combobox
dbscan
dac
amazon-kcl
sourcegear-vault
oai
anti-patterns
viper-architecture
twitter-rest-api
debian-based
pgm
web-component-tester
magento-1.12
start-job
orientation-changes
kallithea
cpu-speed
grunt-express
java-collections-api
funq
bullet
picat
config-files
execvp
surveyor-gem
graph-coloring
socketstream
autostart
n-layer
symfony-2.0
msgbox
hinstance
dashcode
communicationexception
front-controller
longjmp
writing

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App