Use list slicing and extend().
Hint:
mylist = [0,1,2,3,4,5,6,7,8,9]
ix = 3
t1 = mylist[0:ix]
t2 = mylist[ix+1:]
# now use list extend()
t2.extend(t1)
print(t2) # [4, 5, 6, 7, 8, 9, 0, 1, 2]
Use list slicing and extend().
Hint:
mylist = [0,1,2,3,4,5,6,7,8,9]
ix = 3
t1 = mylist[0:ix]
t2 = mylist[ix+1:]
# now use list extend()
t2.extend(t1)
print(t2) # [4, 5, 6, 7, 8, 9, 0, 1, 2]
Python has all the tools to do such a thing.
Use slice and extend(), for example:
mylist1 = [1,2,3]
mylist2 = ['a','b','c']
# slice mylist into two temporary lists at index 2
t1 = mylist1[0:2]
t2 = mylist1[2:]
# now use list extend()
t1.extend(mylist2)
t1.extend(t2)
print(t1) # [1, 2, 'a', 'b', 'c', 3]
Split your large project into a series of small projects.
It's Oktoberfest time, the time for a good bratwurst, grilled chicken, radish, potato salad, all washed down with a stein full of beer.
Variable daniel will have to an instance of class Radiobutton.
check withprint(type(daniel))
If you are not very handsome, things get tough. How about a sports car to impress her with?
Steve Ballmer is stepping down because he is running MS into the ground with a lot of me-too ware and a management style of forced ranking. He most likely was not aware of the spy portion of Windows8.
Here is a hint:
sum3 = 0
for n in range(0, 1000, 3):
#print(n) # test
sum3 += n
print(sum3)
What value does vbNullString represent?
I guess you can find good engineers in any country, also the bad ones.
I would use the length of string.
Works well in most languages.
Let's not forget that WxPython is just the wrapper for WxWindows which is written in C++.
The wrapper seems to ignore the C++ error handling. It could have been done, but wasn't.
I don't think there is any GUI toolkit that is written in pure Python, bummer.
On the other hand Python is very good glue language for other languages. It's easy to make wrappers.
Quick looky shows an indentation problem.
Give us some code!
Here is one example:
import pprint as pp
mydic = {
'a123': [1, 2, 2, 4],
'a124': [1, 2, 3, 4],
'a125': [1, 2, 2, 4],
'a126': [1, 2, 4, 5]
}
newdic = {}
val_list = []
for key, val in mydic.items():
if val not in val_list:
newdic[key] = val
val_list.append(val)
pp.pprint(newdic, width=50)
''' output -->
{'a123': [1, 2, 2, 4],
'a124': [1, 2, 3, 4],
'a126': [1, 2, 4, 5]}
'''
cx_Freeze works with both versions of Python
http://www.blog.pythonlibrary.org/2010/08/12/a-cx_freeze-tutorial-build-a-binary-series/
Snapping turtle is sometimes called snapper.
I use Microsoft's SkyDrive and they give you 7GB free. Works well with the MS file manager.
Since you are not passing any args, this will do:self.bd4 = Tkinter.Button(self.frame, text="D4", command=self.roll_dice)
also
self.sticky_all = 'ewns'
Unfortunately the two bombers were Muslims, so there will be quite a bit of hatred going around.
Koljik can you give us one meaningful reason why you want to use the goto statement?
If you ever have to maintain somebody else's program code, then goto's are one horror.
Also be aware that in Python2 you might get an integer division unless you force a float:
def average(q):
''' cast a float for Python2 '''
return float(sum(q))/len(q)
q = [1, 2, 3, 4]
avg = average(q)
print(avg) # 2.5
entropic310500 you need to show us some of your code!
Python packs a lot of power, take a close look at this:
from string import punctuation
from collections import Counter
data = """\
I love the python programming
How love the python programming?
We love the python programming
Do you like python for kids?
I like Hello World Computer Programming for Kids and Other Beginners.
"""
# convert all upper case leeters to lower case
data = data.lower()
print(data) # test
print('-'*70)
# remove punctuation marks like .,!?
data = "".join(c for c in data if c not in punctuation)
print(data) # test
print('-'*70)
# create a list of (word, frequency) tuples
q = [(word, freq) for word, freq in Counter(data.split()).items()]
# show q sorted by word (default is element index 0)
for word, freq in sorted(q):
print("{:12} {}".format(word, freq))
print('-'*20)
# show q sorted by frequency (element index 1)
for word, freq in sorted(q, key=lambda x: x[1]):
print("{:3d} {}".format(freq, word))
Same problem here:
http://www.daniweb.com/software-development/python/threads/451832/i-dont-know-how-to-use-python..-please-help#post1957570
and here:
http://forums.devshed.com/python-programming-11/i-don-t-know-how-to-use-python-help-me-943069.html
and here:
http://forums.devshed.com/python-programming-11/how-do-you-write-a-program-for-this-in-python-943070.html
Your individual lists have to line up properly to match name and data:
name = ['Frank', 'Bill']
salary = [1234.0, 1535.0]
remark = [3, 2]
employee_list = zip(name, salary, remark)
print(employee_list)
'''
[('Frank', 1234.0, 3), ('Bill', 1535.0, 2)]
'''
# sort by remark which is at tuple index 2
employee_list_sorted = sorted(employee_list, key=lambda x: x[2])
print(employee_list_sorted)
'''
[('Bill', 1535.0, 2), ('Frank', 1234.0, 3)]
'''
The approach vegaseat used avoids potential match problems.
Sets will be ordered by hash order, so you loose the relationship of word and frequency data.
It could be easier to use collections.Counter() on each of your text files and go from there.
According to the prohibitionists here, bear would be better than beer.
If the words in your list/file are unique, you can do something like this:
import pprint
# write a test file with unique words
names = '''\
paul
peter
sally
frank
jim
Sandra
quito
'''
fname = "names.txt"
with open(fname, 'w') as fout:
fout.write(names)
name_dict = {}
# read in the names line by line
for line in open(fname):
word = line.strip()
#print(word)
name_dict[word] = len(word)
pprint.pprint(name_dict)
''' my output -->
{'Sandra': 6,
'frank': 5,
'jim': 3,
'paul': 4,
'peter': 5,
'quito': 5,
'sally': 5}
'''
You need to check the effects of sample size.
In most cases you can simply use the print() function, then it will work in both newer Python versions.
Print rightfully should be a function and not a statement, this was corrected in the newer versions.
If you work with Python on a scientific project, then Spyder is the way to go. It is written in Python and Pyside (PyQT). I like the pleasant layout of the IDE and its many tools and options.
I would say C++ gets the youngsters used to the rigors of programming.
Using the Tkinter GUI toolkit that comes with Python you can use the password option this way:
# Tkinter, explore the entry field for passwords!
from Tkinter import *
def showme():
# clear the label
label2.config(text='')
# get the enter1 text and display in label
label2.config(text=enter1.get())
root = Tk()
# create a data entry widget
enter1 = Entry(root)
# forces Entry to echo as char "*"
enter1["show"] = "*"
# create a label widgets
label1 = Label(root, text='Type password here:')
label1.pack(side='top', fill=X) # topmost
# start enter1 empty
enter1.insert(0, '')
enter1.pack(side='top', fill=X) # below label1
label2 = Label(root)
label2.pack(side='top', fill=X) # below entry1
# cursor in enter1 field
enter1.focus()
# get entry text on Enter-key
enter1.bind('<Return>', (lambda event: showme()))
# or get entry text clicking on button
btn1 = Button(root, text='Get Text', command=showme)
btn1.pack(side='left')
root.mainloop()
You have to use the player instance and not the class name, also make player global since you create it in main():
import pygame
pygame.init()
class Player(pygame.sprite.Sprite):
def __init__(self):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.image.load("evin.png")
self.image = self.image.convert()
tranColor = self.image.get_at((1, 1))
self.image.set_colorkey(tranColor)
self.rect = self.image.get_rect()
self.x = 300
self.y = 300
self.rect.center = (self.x, self.y)
def update(self):
keys = pygame.key.get_pressed()
if keys[pygame.K_UP]:
self.y -= 5
if keys[pygame.K_DOWN]:
self.y += 5
if keys[pygame.K_LEFT]:
self.x -= 5
if keys[pygame.K_RIGHT]:
self.x += 5
self.rect.center = (self.x, self.y)
class Enemy(pygame.sprite.Sprite):
def __init__(self):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.image.load("enemy.png")
self.image = self.image.convert()
tranColor = self.image.get_at((1, 1))
self.image.set_colorkey(tranColor)
self.rect = self.image.get_rect()
self.x = 300
self.y = 200
self.rect.center = (self.x, self.y)
def update(self):
global player
if self.x < player.x:
self.x += 1
self.rect.center = (self.x, self.y)
elif self.x > player.x:
self.x -= 1
self.rect.center = (self.x, self.y)
if self.y < player.y:
self.y += 1
self.rect.center = (self.x, self.y)
elif self.y > player.y:
self.y -= 1
self.rect.center = (self.x, self.y)
def main():
global player
'Create the screen'
screen = pygame.display.set_mode((640, 480))
'Set the background'
background = pygame.Surface(screen.get_size())
background.fill((0, 0, 0))
'call the object'
player = Player()
enemy = Enemy()
'add object to group'
allSprites = pygame.sprite.Group(player)
enemySprites = pygame.sprite.Group(enemy)
clock = pygame.time.Clock()
keepGoing = True
while keepGoing:
clock.tick(30)
' handle events'
for event in pygame.event.get():
if event.type == pygame.QUIT:
keepGoing = False
allSprites.clear(screen, background)
enemySprites.clear(screen, background)
allSprites.update()
enemySprites.update()
allSprites.draw(screen)
enemySprites.draw(screen)
pygame.display.flip()
if __name__ == "__main__":
main()
Before enumerate() came around, you would have incremented the index this way:
def count_data_types(a_list):
answer = [0,0,0]
for x in a_list:
ind = 0
for t in (int, float, str):
if t == type(x):
answer[ind] += 1
ind += 1
return answer
print(count_data_types(['v', 3.0,1, 'etc']))
'''
[1, 1, 2]
'''
You can also use a dictionary approach as this example shows:
# create multiple Tkinter buttons using a dictionary:
import Tkinter as tk
def text_update(animal):
text.delete(0, tk.END)
text.insert(0, animal)
root = tk.Tk()
text = tk.Entry(root, width=35, bg='yellow')
text.grid(row=0, column=0, columnspan=5)
btn_dict = {}
col = 0
words = ["Dog", "Cat", "Pig", "Cow", "Rat"]
for animal in words:
# pass each button's text to a function
action = lambda x = animal: text_update(x)
# create the buttons and assign to animal:button-object dict pair
btn_dict[animal] = tk.Button(root, text=animal, command=action)
btn_dict[animal].grid(row=1, column=col, pady=5)
col += 1
# run the GUI event loop
root.mainloop()
Here is example ...
# one look at the Tkinter Text widget
# use ctrl+c to copy, ctrl+x to cut selected text,
# ctrl+v to paste, and ctrl+/ to select all
# text area can be scrolled with mouse wheel
import Tkinter as tk
root = tk.Tk()
# create the text widget
# width=width in chars, height=lines of text
text = tk.Text(root, width=50, height=5, wrap='none', bg='yellow')
text.pack()
abc = "hello world"
# show result in text widget
text.insert('insert', abc)
# start cursor in the text widget
text.focus()
root.mainloop()
s = 'abcdefg'
print(s[-2:])
print(s[:-2])
print(s[::-2])
Malacca sweet rum on ice.
Looks like you are really lacking the fundamentals of Python.
I would recommend Python 2.7 for you to learn. It does have elements of Python 3 in it as options.
Hint ...
str_list = ['1','2.4','-5.3','7']
# use list comprehension
num_list = [float(item) for item in str_list]
print(num_list) # [1.0, 2.4, -5.3, 7.0]
# test PySide widgets
# get widget's size
from PySide.QtCore import *
from PySide.QtGui import *
app = QApplication([])
# ----- start your widget test code ----
label = 'x'*20
button = QPushButton(label)
width = button.sizeHint().width()
height = button.sizeHint().height()
button.show()
print("button width=%d height=%d (in pixels)" % (width, height))
# ---- end of widget test code -----
app.exec_()
I am experimenting with overlapping transparent images using wxPython. Here is something I came up with (used one of vegaseat's images):
# use wx.Image() to create two overlapping transparent images
import wx
class ImagePanel(wx.Panel):
def __init__(self, parent):
wx.Panel.__init__(self, parent, wx.ID_ANY)
self.parent = parent
# pick 2 image files you have in the working folder
# (or give the full path)
image_file1 = 'Duck_right.jpg'
image_file2 = 'Duck_left.jpg'
# default is type=wx.BITMAP_TYPE_ANY
self.image1 = wx.Image(image_file1)
self.image2 = wx.Image(image_file2)
# need this ...
self.image1.InitAlpha()
self.image2.InitAlpha()
# call on_paint() to create the canvas
wx.EVT_PAINT(self, self.on_paint)
def on_paint(self, event):
dc = wx.PaintDC(self)
# reduces flicker
#dc = wx.BufferedDC(dc)
# set the canvas background color to a light grey
dc.SetBackground(wx.Brush("#D0D0D0"))
#dc.SetBackground(wx.Brush("white")) # if you like
dc.Clear()
# adjust r g b and alpha (transparency) of image
# AdjustChannels(fr, fg, fb, fa)
# adjust alphas from 0.0 to 1.0 to see the effects
alpha1 = 0.5
alpha2 = 0.7
if self.image1.HasAlpha():
image1 = self.image1.AdjustChannels(1.0, 1.0, 1.0, alpha1)
image2 = self.image2.AdjustChannels(1.0, 1.0, 1.0, alpha2)
else:
image1 = self.image1
image2 = self.image2
self.parent.SetTitle("no alpha")
# required to draw images on canvas
bmp1 = wx.BitmapFromImage(image1)
bmp2 = wx.BitmapFromImage(image2)
# now draw the images
dc.DrawBitmap(bmp2, 180, 10, True)
dc.DrawBitmap(bmp1, 20, 70, True)
app = wx.App(0)
mytitle = "wx.Image transparent overlapping images"
mysize = (500, 400)
frame = wx.Frame(None, wx.ID_ANY, title=mytitle, size=mysize)
ImagePanel(frame)
frame.Show(True)
app.MainLoop()